2015-01-08 6 views
2

Youtube iframe API에는 동영상의 끝을 감지하는 이벤트가 있습니다. 삽입 된 재생 목록과 함께 사용되는 경우이 이벤트는 모든 동영상 후에 실행됩니다. 재생 목록에서 마지막 동영상의 끝 부분 만 감지하고 싶습니다.Youtube Iframe API - 재생 목록의 끝 감지

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

문제 자신이 두 번 트리거 :

내 시도이다. 재생 목록의 두 번째부터 끝까지 동영상의 끝에서 플레이어 상태가 종료되고 재생 목록 색인이 1 씩 증가하므로 너무 일찍 트리거됩니다. 또한 재생 목록의 마지막 비디오 끝 부분에서 트리거되며 이는 유일한 의도 된 결과입니다.

답변

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. 
0

동영상 재생이 시작될 때만 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'); 
     } 
    } 
} 
관련 문제