J-bob
J-bob

Reputation: 9126

Use javascript to detect if a youtube video is having trouble loading and playing

Is it possible, using javascript, to detect if an embedded youtube video pauses playback in order to let the video buffer? I know that there are events that fire when the user presses pause, but I'm looking for an event that fires when the video pauses due to a slow connection. I'm creating a web application where it's important to have the video play through smoothly. If the video pauses due to a slow connection, I want to detect that.

Upvotes: 1

Views: 1280

Answers (3)

bash2day
bash2day

Reputation: 153

There is also a state 'buffering' being fired when the player needs to buffer more data..in that case the video stops. I guess also 'error' state might be of help.

Upvotes: 1

J-bob
J-bob

Reputation: 9126

The solution I worked out is just to use the javascript API's onStateChange callback (https://developers.google.com/youtube/js_api_reference) to detect when the player is started for the first time and when it finishes playing at the end. When the player is started, I grab the current time. When it finishes, it sees how much time has elapsed. In my application, the user cannot pause the video, so comparing the elapsed time to the video length indicates if it paused for loading.

Upvotes: 0

aaronman
aaronman

Reputation: 18761

Use this code player.getPlayerState():Number it seems like you are allowed to ask the player what status it is in so this may help you
https://developers.google.com/youtube/js_api_reference

Upvotes: 1

Related Questions