我想实例化一个新的MediaElementPlayer对象。当它成功创建时,我想将整个对象传递给另一个函数(my_object.attachEvents)。我的代码如下:
var options = {
success: function () {
//point 2
console.log("passing player object", local_player_instance);
my_main_object.attachEvents(local_player_instance);
}
}
//point 1
console.log('player inited', local_player_instance);
local_player_instance.video = new MediaElementPlayer('#video_player', options);
my_main_object.attachEvents = function(local_player_instance) {
local_player_instance.video.play()
}
在Firefox中,第2点的赋值在第2点的行调用attach events方法之前执行。
Im Chrome,首先评估第2点,因此当调用attach事件函数中的play方法时,它不存在。
我的问题是,如何在创建MediaElementPlayer时将其成功传递给另一个函数?
答案 0 :(得分:1)
以跨浏览器方式处理此问题的最佳方法是
// here's where you'll store a global reference to the player
var globalMediaElement = null;
var options = {
success: function (domNode, mediaElement) {
globalMediaElement = mediaElement;
doStuff();
// you can also get the the player via jQuery here
$('#video_player').player
}
}
// create MediaElement
new MediaElementPlayer('#video_player', options);
function doStuff() {
globalMediaElement.play();
}