YouTube 播放器 api - addEventListener() 对我不起作用? [英] YouTube player api - addEventListener() does not work for me?

查看:30
本文介绍了YouTube 播放器 api - addEventListener() 对我不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 youtube 播放器 API.我正在关注这里的文档:

http://code.google.com/apis/youtube/js_api_reference.html

在播放器上添加事件侦听器时遇到问题,它似乎卡在那里.我在后面直接放了一个警告语句,它永远不会被调用:

ytplayer.addEventListener("onStateChange", "onytplayerStateChange");alert("我从来没有被叫过...");

知道为什么会发生这种情况吗?通过 FF chrome safari 运行它,同样的事情发生在所有人身上.其他播放器功能一切正常.我正在使用 swfobject 版本.我也在服务器上实时运行.我不明白.

谢谢

解决方案

看看这个页面的源代码,也许它会让你朝着正确的方向前进.视频下方的数字是视频的状态,以您尝试实施的方式捕获和更新

var ytplayer;功能 onytplayerStateChange(newState) {document.getElementById('videoStatus').innerHTML = newState;}功能 onYouTubePlayerReady(playerId) {ytplayer = document.getElementById(playerId);ytplayer.addEventListener('onStateChange', 'onytplayerStateChange');}$(document).ready(function() {变量参数 = {允许脚本访问:总是"};var atts = {id:ytplayer1"};swfobject.embedSWF("http://www.youtube.com/v/5dWnMu490Zk?rel=0&autoplay=1&enablejsapi=1&playerapiid=ytplayer1", "popupVideoContainer1", "853", "505", "9", null, {}, params, atts);});

您需要 Flash 和 JavaScript 才能观看视频.</div><div id="videoStatus">status</div>

I'm using the youtube player api. I'm following the doc here:

http://code.google.com/apis/youtube/js_api_reference.html

having a problem adding an event listener on the player, it seems to just get stuck there. I put an alert statement directly after, which never gets called:

ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
alert("I never get called...");

any idea why this would happen? Running it through FF chrome safari, same thing happens on all. Other player features all work fine. I'm using the swfobject version. I'm running this live from a server too. I don't get it.

Thanks

解决方案

take a look at the source code of this page, may be it will get you going in the right direction. The number under the video is the state of the video caught and updated the way you are trying to implement

var ytplayer;

function onytplayerStateChange(newState) {
    document.getElementById('videoStatus').innerHTML = newState;
}

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

$(document).ready(function() {

    var params = {
        allowScriptAccess: "always"
    };

    var atts = {
        id: "ytplayer1"
    };

    swfobject.embedSWF("http://www.youtube.com/v/5dWnMu490Zk?rel=0&autoplay=1&enablejsapi=1&playerapiid=ytplayer1", "popupVideoContainer1", "853", "505", "9", null, {}, params, atts);

});

<div id="popupVideoContainer1">You need Flash and JavaScript in order to view the video.</div>
<div id="videoStatus">status</div>

这篇关于YouTube 播放器 api - addEventListener() 对我不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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