我正在做一个项目,在这个项目中,我需要通过video.prototype.getCurrentFrame()方法从播放的视频中返回帧计数。我的脚本运行得很好,除了这个方法返回的数字是“未定义的”。我知道我的问题与我的变量作用域有关,但是我是一个新的javascript,而且我似乎不能让它独立工作……
在我的方法video.prototype.setUpPlayer中,我有一个函数允许我计算framcount帧,其中我更新了一个名为'timeListener'的变量;如果我试图通过video.prototype.getCurrentFrame()访问这个frame变量,它不会达到更新值。
到目前为止,我的代码如下:
var Video = function(aVideoId){
this.videoId = aVideoId;
this.frame;
this.videoContainer;
this.myPlayer;
this.timeListener;
this.progressListener;
};
Video.prototype.getCurrentFrame = function(){
return this.frame;
}
Video.prototype.setVideoContainer = function(){
videoContainer = $('<div>', {
id: this.videoId,
class: 'projekktor',
width: "100%",
height: "100%",
});
$('#innerContainer').html(videoContainer);
}
Video.prototype.setUpPlayer = function(){
videoId = this.videoId;
myPlayer = projekktor('#' + videoId, {
controls: "true",
volume: 0.5,
preload: false,
autoplay: true,
playlist: [{
0: {
src: '/' + videoId + '.mp4',
type: 'video/mp4'
},
1: {
src: '/' + videoId + '.mov',
type: 'video/mov'
},
2: {
src: '/' + videoId + '.ogv',
type: 'video/ogv'
}
}]
}, function() { // call back
myPlayer.addListener('time', timeListener);
myPlayer.addListener('progress', progressListener);
});
timeListener = function(duration) {
$('#currentTime').html(duration);
frame = Math.round(duration * 25);
$('#currentFrame').html(frame);
return this.frame = frame;
}
progressListener = function(value) {
$('#progress').html(Math.round(value))
$('#progress2').html(myPlayer.getLoadProgress());
}
}提前感谢您的帮助!
发布于 2012-07-04 19:08:33
您需要从Video的实例调用getCurrentFrame,而不是从原型本身调用:
var video = new Video;
alert(video.getCurrentFrame());使用原型检索当前帧的唯一方法是使用apply() (这也需要一个实例):
var video = new Video;
alert(Video.prototype.getCurrentFrame.apply(video));EDIT:似乎timeListener回调不是在视频实例的上下文中执行的。您可能需要显式地将回调绑定到正确的作用域:
timeListener = function()
{
// ...
this.frame = frame;
// ...
}
var video = new Video;
// binding the correct context
myPlayer.addListener('time', timeListener.bind(video));timeListener闭包中的this现在是video。
https://stackoverflow.com/questions/11327804
复制相似问题