问题
Im using swfobject to embed the vimeo video, it's pretty simple,
var vimPlayer;
function vimeo_player_loaded(id){
vimPlayer = document.getElementById(id);
vimPlayer.addEventListener('play', function(){
alert('Playing');
});
vimPlayer.api_play();
}
vimPlayer.api_play();
Does play the video just fine! However, How can I bind a listener to that function so that I can perform other tasks? The listener I have added does not fire at all.
回答1:
$('video').bind('play', function (e) {
// do something
});
try using this...
http://www.w3.org/2010/05/video/mediaevents.html
回答2:
If using Flash Embeds, where play()
is prefixed to api_play()
, the need to prefix also includes addEventListener()
:
vimPlayer.api_addEventListener('play', function () {
alert('Playing');
});
This is demonstrated under Flash Embed Code:
To add a listener for the
finish
event:document.getElementById('vimeo_player').api_addEventListener('finish', function(event) { // do stuff here });
回答3:
Adding event listener takes three params, here's an example:
function doSomething() {
alert('Image clicked');
}
var myButton = document.getElementById('my_button_id');
myButton.addEventListener('click', doSomething, false);
回答4:
I was stacking at exactly the same problem. As long as I tried countless times,
function vimeo_player_loaded(id){
vimPlayer = document.getElementById(id);
vimPlayer.api_addEventListener('play', 'vimeo_play');
vimPlayer.api_play();
}
function vimeo_play(){
console.log("play");
}
This should work fine if the player_id and id in HTML are same. English is not my first language. Sorry if it's difficult to understand.
来源:https://stackoverflow.com/questions/18841618/how-do-i-add-an-event-listener