YouTube(注入)視頻結束回調 (YouTube (injected) video ends callback)


問題描述

YouTube(注入)視頻結束回調 (YouTube (injected) video ends callback)

I inject YouTube iframe to a div in document ready and bind it to click:

jQuery(document).ready(function($) {
    jQuery('.video-thumb').click(function(){
        ...
        $('#player').html('<iframe width="761" height="421" src="http://www.youtube.com/embed/' + $(this).attr('videoid') + '?rel=0&wmode=transparent" frameborder="0" allowfullscreen></iframe>');
        ...
    }
 }

And I want to do a callback when video ends. I have read about onYouTubePlayerAPIReady, but it has to be put outside document ready.

I have tried to load video player by this construction outside document ready:

var player;
function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
      height: '421',
      width: '761',
      videoId: '',
      playerVars: { autoplay: 1, autohide: 1, showinfo: 0 },
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
}

but I got some issues:

  • showinfo:0 didn't work, still got other video thumbnails in the end
  • I don't know how to change video id (and reinit the video?)
  • more script errors than first method (injecting iframe)

I prefer using first method, but how to create video ends callback? Thanks.


參考解法

方法 1:

A working example of the below code is also on jsfiddle.net.

Some notes:

  • Uses the iframe_api, not the javascript_api
  • The YT.Player constructor is minimal because you are building the iframe yourself.
  • The "playerVars" are included as iframe URL parameters.
  • The parameter "enablejsapi=1" is set.

Example Markup

<script src="http://www.youtube.com/iframe_api"></script>

<a style="display: block;" class="video-thumb" id="HuGbuEv_3AU" href="#">
    Megadeth: Back In The Day
</a>  
<a style="display: block;" class="video-thumb" id="jac80JB04NQ" href="#">
    Judas Priest: Metal Gods
</a> 
<a style="display: block;" class="video-thumb" id="_r0n9Dv6XnY" href="#">
    Baltimora: Tarzan Boy
</a>   

<div id="container"></div>
<div id="log">--Logging enabled--</div>

The JavaScript

function log(msg) {
    jQuery('#log').prepend(msg + '<br/>');
}

function onPlayerStateChange(event) {
    switch(event.data) {
        case YT.PlayerState.ENDED:
            log('Video has ended.');
            break;
        case YT.PlayerState.PLAYING:
            log('Video is playing.');
            break;
        case YT.PlayerState.PAUSED:
            log('Video is paused.');
            break;
        case YT.PlayerState.BUFFERING:
            log('Video is buffering.');
            break;
        case YT.PlayerState.CUED:
            log('Video is cued.');
            break;
        default:
            log('Unrecognized state.');
            break;
    }
}

jQuery(document).ready(function($) {
    $('.video-thumb').click(function() {

        var vidId = $(this).attr('id');
        $('#container').html('<iframe id="player_'+vidId+
            '" width="420" height="315" src="http://www.youtube.com/embed/'+
            vidId+'?enablejsapi=1&autoplay=1&autohide=1&showinfo=0" '+
            'frameborder="0" allowfullscreen></iframe>');

        new YT.Player('player_'+vidId, {
            events: {
                'onStateChange': onPlayerStateChange
            }
        });
    });
});

(by Jeaf GilbertJoe Coder)

參考文件

  1. YouTube (injected) video ends callback (CC BY-SA 3.0/4.0)

#jquery #javascript #Callback #youtube-api #youtube






相關問題

讓 jQuery 與 Netscape 7 和 8 一起工作 (Getting jQuery to work with Netscape 7 and 8)

使用 Jquery 的 mvc3 搜索結果 (mvc3 search results with Jquery)

從嵌套的 jquery 函數返回一個值 (Return a value from nested jquery function)

Mencocokkan lebar divisi dengan jquery (Matching division widths with jquery)

無法在 jQuery AJAX 中多次生成點擊事件 (unable to generate click event more than once in jQuery AJAX)

使用雙引號格式並用逗號分隔元素數組 (Implode an element array with double quote format and separated by comma)

選擇不更新 (Select doesn't update)

chrome中帶有省略號的多行文本問題 (issue with multiline text with ellipsis in chrome)

AJAX/PHP/JS - 無法將頁面內容加載到容器中 (AJAX/PHP/JS - Cannot load page content into container)

使用 jQuery 將文本插入 textarea (Insert text into textarea with jQuery)

滾動到頁面底部,僅當用戶在 DOM 操作之前已經位於底部時 (Scroll to bottom of page, only if the user already was at the bottom before DOM manipulation)

如何設置單選按鈕的樣式,使其看起來像普通的可點擊按鈕? (How do I style a radio button to look like a normal clickable button?)







留言討論