forked from youtube/api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_listeners.js
58 lines (50 loc) · 1.43 KB
/
event_listeners.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var player;
// Callback for when the YouTube iFrame player is ready
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
// Set Player height and width
height: '390',
width: '640',
// Set the id of the video to be played
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady
// You can add more event listeners here
}
});
};
function onPlayerReady (){
//add onStateChange event handler
player.addEventListener("onStateChange","onPlayerStateChange");
//add your own rate listener below:
};
function onPlayerStateChange(event){
// Get current state
var currentState;
if (event.data == YT.PlayerState.ENDED){
currentState = "Ended";
}
else if (event.data == YT.PlayerState.PLAYING){
currentState = "Playing";
}
else if (event.data == YT.PlayerState.PAUSED){
currentState = "Paused";
}
else if (event.data == YT.PlayerState.BUFFERING){
currentState = "Buffering";
}
else if (event.data == YT.PlayerState.CUED){
currentState = "Cued";
} else{
currentState = "Unknown";
}
currentState += " (" + event.data + ")"
// Update video state div
document.getElementById('currentState').innerText = currentState;
};
function onPlaybackRateChange(event){
// Implment this function to display the rate of the player on the page
var currentRate;
// You code goes here
document.getElementById('currentRate').innerText = currentRate;
}