YouTube Player API:检索对现有播放器的引用 [英] YouTube Player API: retrieving a reference to an existing player

查看:122
本文介绍了YouTube Player API:检索对现有播放器的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用内容脚本开发Google Chrome扩展程序。我想与嵌入YouTube视频播放器的网页进行互动。我已经将 www-widgetapi-vfljlXsRD.js 包含为JavaScript文件,并且YouTube名称空间在扩展沙箱内正确初始化。



我试图检索对现有iFrame播放器的引用。为了达到这个目的,我尝试了这个:

  var ytplayer = new YT.Player('ytplayer'); 
ytplayer.pauseVideo();

其中 div#ytplayer 是iFrame嵌入



我得到这个错误,告诉方法不存在:


TypeError:Object#没有方法'pauseVideo'

检索引用的正确方法是什么?现有的球员?

解决方案

好吧,我找到了我的方式与现有的球员交谈。我真的没有设法得到一个工作的参考。



我受到Rob的惊人工作的启发(参见 YouTube iframe API:如何控制一个已经在HTML中的iframe播放器?),我表示Youtube播放器正在向主窗口发送消息,如下所示:

{event:infoDelivery,info:{currentTime:3.3950459957122803,videoBytesLoaded:244,videoLoadedFraction:0.24482703466872344},id:1}


您可以通过监听message事件来观察它 window.addEventListener('message',function(event){console .log(event.data)},false); 在包含正在播放的播放器的窗口中。
$ b

infoDelivery 事件包含当前玩家的时间并在视频播放时发送。听到这个事件,我现在可以知道当前玩家的时间。



到目前为止,我还没有找到合适的解决方案,所以这一项目将会成为现在的工作。

I'm developping a Google Chrome extension, using content script. I want to interact with pages embedding a YouTube video player. I have include the www-widgetapi-vfljlXsRD.js as a JavaScript file, and YouTube namespace is correctly initialize inside the extension sandbox.

I'm trying to retrieve a reference to an existing iFrame player. To achieve that, I tried this:

var ytplayer = new YT.Player('ytplayer'); 
ytplayer.pauseVideo();

where div#ytplayeris the iFrame embedding the actual player.

I'm getting this error, telling that the method does not exist:

TypeError: Object # has no method 'pauseVideo'

What is the correct way to retrieve a reference to an existing player?

解决方案

Ok, I found my way to talk to an existing player. I have not actually managed to get a working reference to it.

I was inspired by the amazing work of Rob (cf. YouTube iframe API: how do I control a iframe player that's already in the HTML?), I remarked that the Youtube player is sending message to the main window, like this:

{"event":"infoDelivery","info":{"currentTime":3.3950459957122803,"videoBytesLoaded":244,"videoLoadedFraction":0.24482703466872344},"id":1}

You can observe it by listening to the "message" event window.addEventListener('message', function (event) {console.log(event.data)}, false); in a window containing a playing player.

This infoDelivery event contains the current player's time and is sent while the video is playing. Listening to this event I am now able to know the current player time.

I have not found a proper solution so far so this one will make the job for the moment.

这篇关于YouTube Player API:检索对现有播放器的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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