Skip to content
This repository has been archived by the owner on Jun 29, 2018. It is now read-only.

[#961778] Get YouTube passing all tests again. #372

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 36 additions & 38 deletions wrappers/youtube/popcorn.HTMLYouTubeVideoElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,17 @@
}

function onReady() {

addYouTubeEvent( "play", onPlay );
addYouTubeEvent( "pause", onPause );
// Set initial paused state
if( impl.autoplay || !impl.paused ) {
removeYouTubeEvent( "play", onReady );
impl.paused = false;
addMediaReadyCallback(function() {
onPlay();
if ( !impl.paused ) {
onPlay();
}
});
}

Expand All @@ -197,39 +203,41 @@
self.dispatchEvent( "canplay" );

mediaReady = true;
bufferedInterval = setInterval( monitorBuffered, 50 );

while( mediaReadyCallbacks.length ) {
mediaReadyCallbacks[ 0 ]();
mediaReadyCallbacks.shift();
}
bufferedInterval = setInterval( monitorBuffered, 50 );

// We can't easily determine canplaythrough, but will send anyway.
impl.readyState = self.HAVE_ENOUGH_DATA;
self.dispatchEvent( "canplaythrough" );
}

function onFirstPause() {
removeYouTubeEvent( "pause", onFirstPause );
if ( player.getCurrentTime() > 0 ) {
setTimeout( onFirstPause, 0 );
return;
}
addYouTubeEvent( "play", onPlay );
addYouTubeEvent( "pause", onPause );
removeYouTubeEvent( "pause", onFirstPause );

onReady();
if( impl.autoplay || !impl.paused ) {
addYouTubeEvent( "play", onReady );
player.playVideo();
} else {
onReady();
}
}

// This function needs duration and first play to be ready.
function onFirstPlay() {
removeYouTubeEvent( "play", onFirstPlay );
if ( player.getCurrentTime() === 0 ) {
setTimeout( onFirstPlay, 0 );
return;
}
removeYouTubeEvent( "play", onFirstPlay );
addYouTubeEvent( "pause", onFirstPause );

player.seekTo( 0 );
player.pauseVideo();
}
Expand Down Expand Up @@ -268,7 +276,11 @@

// paused
case YT.PlayerState.PAUSED:
dispatchYouTubeEvent( "pause" );
// Youtube fires a paused event before an ended event.
// We have no need for this.
if ( player.getDuration() !== player.getCurrentTime() ) {
dispatchYouTubeEvent( "pause" );
}
break;

// buffering
Expand All @@ -294,6 +306,11 @@
if( !( playerReady && player ) ) {
return;
}

removeYouTubeEvent( "buffering", onBuffering );
removeYouTubeEvent( "ended", onEnded );
removeYouTubeEvent( "play", onPlay );
removeYouTubeEvent( "pause", onPause );
onPause();
mediaReady = false;
loopedPlay = false;
Expand Down Expand Up @@ -477,15 +494,13 @@
}

function onPlay() {

if( impl.ended ) {
changeCurrentTime( 0 );
impl.ended = false;
}
timeUpdateInterval = setInterval( onTimeUpdate,
self._util.TIMEUPDATE_MS );
impl.paused = false;

if( playerPaused ) {
playerPaused = false;

Expand Down Expand Up @@ -514,11 +529,6 @@
};

function onPause() {
// Youtube fires a paused event before an ended event.
// We have no need for this.
if ( player.getDuration() === player.getCurrentTime() ) {
return;
}
impl.paused = true;
if ( !playerPaused ) {
playerPaused = true;
Expand Down Expand Up @@ -557,23 +567,6 @@
}
}

function setVolume( aValue ) {
impl.volume = aValue;
if( !mediaReady ) {
addMediaReadyCallback( function() {
setVolume( impl.volume );
});
return;
}
player.setVolume( impl.volume * 100 );
self.dispatchEvent( "volumechange" );
}

function getVolume() {
// YouTube has getVolume(), but for sync access we use impl.volume
return impl.volume;
}

function setMuted( aValue ) {
impl.muted = aValue;
if( !mediaReady ) {
Expand Down Expand Up @@ -681,16 +674,21 @@

volume: {
get: function() {
// Remap from HTML5's 0-1 to YouTube's 0-100 range
var volume = getVolume();
return volume / 100;
return impl.volume;
},
set: function( aValue ) {
if( aValue < 0 || aValue > 1 ) {
throw "Volume value must be between 0.0 and 1.0";
}

setVolume( aValue );
impl.volume = aValue;
if( !mediaReady ) {
addMediaReadyCallback( function() {
self.volume = aValue;
});
return;
}
player.setVolume( impl.volume * 100 );
self.dispatchEvent( "volumechange" );
}
},

Expand Down