私は基本的に私が説明した方法でそれをやった。使用したコードは次のとおりです。
started = false;
var onYouTubeIframeAPIReady = function(id) {
var player = new YT.Player('player', {
videoId: id,
events: {
'onStateChange': function (event) {
if (event.data == 1) { // The video started playing
started = true;
}
if (started && event.data == -1) {
// Video had already started playing before and is now in
// "unstarted" state so it must be a new video.
var video_url = event.target.i.videoUrl;
var video_id = video_url.replace('http://www.youtube.com/watch?v=', '').replace('&feature=player_embedded', '');
window.location = '#/view/' + video_id;
}
}
}
});
}
基本的に、ビデオの再生が開始されたら、「開始」変数を true に設定します。次に、ビデオが「開始されていない」状態になった場合、「開始済み」が true の場合、再生を開始したばかりの新しいビデオであることがわかります。その場合は、event.target オブジェクトからそのビデオ ID を取得し、必要に応じて処理します。
誰かがコンテキストを見たい場合は、私の完全なコミットがここにあります。 http://toogl.esで実際に動作しているのを見ることができます。