How to catch Youtube player's dragging/skipping/forwarding event with API in javascript

半世苍凉 提交于 2019-12-22 12:44:11

问题


I'm looking at the documentation for onStateChange, but I don't find anything the even when user will be dragging the player timer left or right. In fact, onStateChange doesn't catch anything when I do that.

This is from the documentation:

https://developers.google.com/youtube/iframe_api_reference

This event fires whenever the player's state changes. The data property of the event object that the API passes to your event listener function will specify an integer that corresponds to the new player state. Possible values are:

-1 (unstarted)
0 (ended)
1 (playing)
2 (paused)
3 (buffering)f
5 (video cued).

When the player first loads a video, it will broadcast an unstarted (-1) event. When a video is cued and ready to play, the player will broadcast a video cued (5) event. In your code, you can specify the integer values or you can use one of the following namespaced variables:

YT.PlayerState.ENDED
YT.PlayerState.PLAYING
YT.PlayerState.PAUSED
YT.PlayerState.BUFFERING
YT.PlayerState.CUED

But Youtube somehow does it itself when you try to share a video! Go to a youtube video, and click on the share button, and then look at the Start At textfield while trying to drag the player's timer. The textfield gets updated with the player's getCurrentTime. How is Youtube doing that?


回答1:


It does appear YouTube does not have such a event listener..or at least I could not find one. So instead I'm using setInterval to continuously call getCurrentTime() API call. If there is a better approach, please let me know. thx

function onPlayerStateChange(event) {

    timeInterval = setInterval(function() { 
        setCurntTime( txtBox, event.target.getCurrentTime() ) 
         }, 1500);
}


来源:https://stackoverflow.com/questions/28359341/how-to-catch-youtube-players-dragging-skipping-forwarding-event-with-api-in-jav

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