I am writing a Browser Plugin and need to find a way to get the current time a YouTube Video playing on YouTube using JavaScript. I have been playing around in the Chrome JavaScript Console and haven't had any luck.
It appears that the chrome API only works with embedded video players not a video that is playing on on youtube.com. One option I looked into is in the share section of a video their is an input box for the "start at:" time that contains the current time of the video. I have tried using .value and .text on this input box and they both return undefined? Does anyone have any ideas?
Finally I found how to make it work on iOS (and Android too).
Actually, the whole youtube js api was broken for me if run on mobile browser.
Problem solved by creating player using
new YT.Player
as described in YouTube IFrame API.Please note: only creating
<iframe>
from<div>
placeholder works for mobile browsers at the time. If you try to use existing<iframe>
innew YT.Player
call, as mentioned in IFrame API, this will not work.After player created, it's possible to use
player.getCurrentTime()
orplayer.getDuration()
withplayer
instance created.Note: I had no luck calling this methods on player obtained with
player = document.getElementById(...)
(from @JosephMarikle answer).Only created
player
instance worked in mobile browsers.Useful links:
Stop at specific time youtube video and show notification box in GWD
play video and hide notification
Just FYI, There is a new iframe API for the YouTube player: https://developers.google.com/youtube/iframe_api_reference
You can use Html5 Video API on
youtube.com
Note: It's not gonna work on Youtube Iframe API because Iframes are isolated. You cannot access the context of a Youtube IFrame .
See the api
Depends on what you want
http://code.google.com/apis/youtube/js_api_reference.html