YouTube Player API: retrieving a reference to an existing player

我只是一个虾纸丫 提交于 2019-11-28 06:41:27

问题


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?


回答1:


I was using YouTube player API before and it was working properly. Today I have issues like you, and I did not change anything in my code. It might mean that the www-widgetapi-vfljlXsRD.js has been changed and encounters bugs... I cannot help any further.




回答2:


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.



来源:https://stackoverflow.com/questions/17073962/youtube-player-api-retrieving-a-reference-to-an-existing-player

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!