2017-05-03 4 views
2

私はvideoJsを使用してビデオを管理者 プラットフォームに組み込んで反応させています。私はここにプレーヤーを設定します。VideoJSのcurrentTime()は常に0を返します

else if (contentSource == 'arclight'){ 
    var options = { 
    height:'216', 
    sources :[{ 
    //src: this.state.video.url, 
    // type: 'video/mp4' 
    }] 
    } 
    var player = videojs('player',options,function onPlayerReady() { 
    videojs.log('Your player is ready!'); 
    }) 
    this.setState({ 
    player:player,content: contentSource 
    }); 
} 

私のビデオは、このタグに表示されます。

<video id="player" class="player" 
className={`video-js vjs-default-skin vjs-big-play-centered 
${styles.vjs}`} 
controls preload= "auto" data- 
setup='{"example_option":true}'> 
<source src={this.state.video.url} type= "video/mp4" /> 
<p className="vjs-no-js"> 
To view this video please enable JavaScript, and consider 
upgrading to a web browser that 
<a href= "http://videojs.com/html5-video-support" 
target="_blank"> supports HTML5 video </a> 
</p> 
</video> 

そして最後に、私はこの方法で再生されているビデオの現在の時刻を取得しようとしている

handleCurrentButton(inputId, event){ 
    event.preventDefault(); 
    var timeMark =0; 
    if(this.state.content == 'vimeo'){ 
    this.state.player.getCurrentTime().then(function(seconds){console.log(seconds) 
     timeMark=seconds; 
     VideosActions.updateVideoAttribute(inputId, timeMark); 
    }).catch(function(error){ 
    }); 
} 
    else if(this.state.content == 'youtube') { 
    timeMark = Math.round(this.state.player.getCurrentTime()); 
    VideosActions.updateVideoAttribute(inputId, timeMark); 
} 

else { 
// timeMark = this.state.player.currentTime(); 
    console.log(this.state.player.currentTime()) 
    VideosActions.updateVideoAttribute(inputId, timeMark); 
} 
} 

問題は、player.currentTime()呼び出しが常に0を返していることです.VimeoとYoutubeの他の2つのgetCurrentTimeは正常に動作します。これの背後にある理由は何ですか?私はあなたがそれを理解することができるように、この問題の周りに十分な文脈を与えようとしました。 事前にお手数をおかけしていただきありがとうございます。

答えて

1

問題はgetCurrentTime()が約束を返すため、約束がプロミスの.then()関数へのコールバックとして解決された時刻の値にアクセスする必要があります。

else { 

    this.state.player.currentTime().then(function(seconds){ 
    console.log(seconds) 
    timeMark=seconds; 
    VideosActions.updateVideoAttribute(inputId, timeMark); 
    }); 

} 
+0

私はそれが約束を返さないと信じています。 http://docs.videojs.com/Player.html#currentTime:ドキュメントに数値が返されると記載されていますか? –

+0

それはあなたが正しいように見えますが、私は約束のように見えるあなたのvimeoの例を見ていました。あなたはVimeoとYouTubeで違った使い方をしていますが、どちらも使えますか? –

+0

うん、vimeoとyoutubeの両方が動作します。私はvimeoとyoutubeの両方のプレーヤーをそれぞれ使用しているので、それらは異なっています。 vimeoは約束なので、 '.then'を使う必要がありますが、youtubeのgetCurrent時間は数値を返します。私が持っている3番目のプレイヤーは、vimeoやyoutubeのような独自のカスタムプレーヤーを持っていないコンテンツソース用です –

関連する問題