2012-07-04 2 views
0

나는 video.prototype.getCurrentFrame() 메서드를 통해 재생중인 비디오에서 framecount를 반환해야하는 프로젝트에서 작업하고 있습니다. 내 스크립트는이 메서드에 의해 반환되는 숫자가 '정의되지 않은'것입니다 꽤 많이 exept exept 작동합니다. 내 문제는 내 변수의 범위와 뭔가를 할 수 있지만 자바 스크립트에 새로운 해요 그리고 난 내 자신의 작품을 만들 수없는 것 알아요 ...프로토 타입 변수 Javascript 업데이트

내 방법에 video.prototype.setUpPlayer 나는 그 날 framcount 'timeListener' 변수를 업데이 트 프레임 수를 계산할 수 있습니다; video.prototype.getCurrentFrame() 통해이 프레임 변수에 액세스하려고하면 업데이트 된 값에 도달하지 않습니다.

여기 내 코드는 지금까지 있습니다 :

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()); 
     } 
} 

감사합니다 사전에 도움을! 당신이 프로토 타입을 사용하여 현재 프레임을 검색 할 수

var video = new Video; 
alert(video.getCurrentFrame()); 

유일한 방법은 또한 인스턴스를 요구하는 (apply()을 사용하는 것입니다 :

+1

어떻게'.getCurrentFrame()'함수를 호출할까요? 코드에'Video' 인스턴스를 표시하지 마십시오. – Esailija

+0

코드에 한 가지 주석 : 함수에 사용 된 변수의 범위를 지정하는 것이 좋습니다. . 그것은 private ('var'를 추가하거나 프로토 타입 내에서 접근 가능하게하기 위해'this'를 추가)입니다. 그렇지 않으면 전역 범위에서 많은 "가비지"를 생성하게됩니다. 이 의견은 귀하의 질문과 관련이 없습니다. –

답변

2

당신은 Video의 인스턴스가 아닌 프로토 타입 자체에서 getCurrentFrame를 호출해야) :

var video = new Video; 
alert(Video.prototype.getCurrentFrame.apply(video)); 

편집 : 응용 프로그램 그것은 비디오 인스턴스의 컨텍스트에서 timeListener 콜백이 실행되지 않습니다. timeListener 폐쇄에

timeListener = function() 
    { 
    // ... 
     this.frame = frame; 
    // ... 
    } 

var video = new Video; 

// binding the correct context 
myPlayer.addListener('time', timeListener.bind(video)); 

this 지금 video입니다 : 당신은에 명시 적으로 바인드 올바른 범위에 콜백이있을 수 있습니다.

+0

덕분에, 저는 이미 getCurrentFrame() 함수를 사용하기 위해 인스턴스를 사용합니다. 아마 내가 이것에 대해 더 정확 했어야 했어. 하지만 내 문제는 this.frame이 setUpPlayer 메소드 외부에서 변경되지 않는다는 것입니다. 그래서 내 비디오 개체 instanciate 및 getCurrentFrame 호출 할 때. 이 메소드는 처음부터 생성자에 넣어 진 값을 반환합니다 (제 경우는 아무 것도 아닙니다). 언제 내가 값을 업데이 트 반환 싶어요 – Fabax