2

Youtube iframe API には、動画の終わりを検出するためのイベントがあります。埋め込まれたプレイリストで使用すると、このイベントはすべての動画の後に発生します。プレイリストの最後のビデオの終わりだけを検出したい。

これは私の試みです:

if (event.data == YT.PlayerState.ENDED && currentIndex == playlist.length - 1 ){ 
    alert('Playlist finished.');
}

彼の問題は、これが2回トリガーされます。プレイリストの最後から 2 番目のビデオの最後で、プレーヤーの状態が ENDED になり、プレイリスト インデックスが 1 つ増えるため、トリガーが早すぎます。また、意図した唯一の結果であるプレイリストの最後のビデオの最後にもトリガーされます。

4

2 に答える 2

1

currentIndexビデオの再生が開始されたときにのみ値を設定する必要があります。これは私が同じことを達成するのを助けました:

function onPlayerStateChange(event) {

    if (event.data == YT.PlayerState.PLAYING) {
        currentIndex = event.target.getPlaylistIndex();
    }

    if (event.data == YT.PlayerState.ENDED) {
        if (currentIndex == (playlist.length-1)) {
            console.log('end playlist');
        }
    }
}
于 2016-06-13T14:53:46.997 に答える
0

これに答えるのがちょっと遅いことはわかっていますが、答えを探していて、このような不器用な方法を見つけました。

var isended=0; 
function testifover(){
if(isended==1) { // Did the state change while we delayed 5 seconds
// Whatever you wanted to do at the end of the playlist here.
}
}
function onPlayerStateChange(event) {
  switch (event.data) {
    case YT.PlayerState.UNSTARTED:
      console.log('unstarted');
      break;
    case YT.PlayerState.ENDED:
isended=1;
   var myVar = setTimeout(function(){testifover()} , 5000);  // Delay 5 seconds to make sure we aren't loading another video
// and then do whatever if we really are at the end of the playlist. 
      break;
    case YT.PlayerState.PLAYING:
    isended=0; // we started playing another vid in the playlist
      break;
    case YT.PlayerState.PAUSED:
      console.log('paused');
      break;
    case YT.PlayerState.BUFFERING:
   isended=0; // we started buffering another video in the playlist
      break;
    case YT.PlayerState.CUED:
      console.log('video cued');
      break;
      }
    }

    //     On state change fires and we wait 5 seconds and make sure that hasn't changed to buffering or playing or we kill the player.
于 2015-11-08T13:33:13.913 に答える