视频结束时youtube api调用功能 [英] youtube api call function when video ends

查看:37
本文介绍了视频结束时youtube api调用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 javascript 找出嵌入的 youtube 视频何时结束.

I need to find out when an embedded youtube video ends via javascript.

这是我的嵌入代码:

    <object id="ytplayer" height="35" width="560"><param name="movie" value="http://www.youtube.com/v/u8C-ZTQJIkU?version=3&enablejsapi=1&playerapiid=ytplayer&rel=0&fs=0&theme=light&showinfo=0&modestbranding=1&autohide=0&color=white">
    </param>
    <param name="allowFullScreen" value="false">


    </param>
    <param name="allowscriptaccess" value="always">


    </param>
    <embed src="http://www.youtube.com/v/u8C-ZTQJIkU?version=3&rel=0&fs=0&theme=light&showinfo=0&modestbranding=1&autohide=0&color=white" type="application/x-shockwave-flash" allowfullscreen="false" width="560" height="35" allowscriptaccess="always"></embed></object>

<小时>

这是我到目前为止要找出它何时结束但警报没有出现的代码.你能帮我找出问题吗?


This is the code I have so far to find out when it has ended but the alert doesnt come up. Could you help me find whats wrong?

    <script type="text/javascript" src="http://www.youtube.com/player_api"></script>
    <script type="text/javascript">
    function onYouTubePlayerReady(playerId) {
      ytplayer = document.getElementById("ytplayer");
      ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
    }

    function onytplayerStateChange(newState) {
       alert("Player's new state: " + newState);
    }
    </script>

谢谢.

推荐答案

您似乎只显示了部分代码.object 标记应包含一个名为 id 的属性,该属性应设置为 ytplayer.这是完整的工作代码:

You seem to have shown only part of the code. The object tag should contain an attribute called id that should be set to ytplayer. Here is the fully working code:

 <object type="application/x-shockwave-flash" id="ytplayer" data="http://www.youtube.com/v/u8C-ZTQJIkU?enablejsapi=1&amp;playerapiid=ytplayer&amp;version=3" width="425" height="356"><param name="allowScriptAccess" value="always">
    <param name="allowScriptAccess" value="always">
</object>

</body>
</html>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.1/swfobject.js"></script>    

<script type="text/javascript">

    function onYouTubePlayerReady(playerId) {
        var player = document.getElementById("ytplayer");
        player.addEventListener("onStateChange", "onytplayerStateChange");
    }

    function onytplayerStateChange(newState) {
        alert("New state " + newState); 
    }

</script>

这篇关于视频结束时youtube api调用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆