是不是可以让nativescript-videoplayer和nativescript-audioplayer一起工作?

时间:2017-07-05 13:32:00

标签: javascript android ios nativescript nativescript-plugin

所以我目前正在使用nativescript的多平台框架构建应用程序。我有两个插件可以自己工作,但是当它们同时运行时,我希望暂停两个轨道(带有单独轨道文件的视频和音频),只有其中一个停止,而另一个继续。那是为什么?

首先,我的nativescript audioplayer代码是:

var videoViewModel = new Observable();
videoViewModel.audioPlayer = new TNSPlayer();

let audioParams = {
        audioFile: selectedVideoEntity.audioPath_,
        loop: true,
        completeCallback: function(){},
        errorCallback: function(error){console.log("failed with: ", error)}};

let audioPlayerCreatedCallback = () => {
            videoViewModel.audioPlayer.getAudioTrackDuration().then((duration) => {
                // iOS: duration is in seconds
                // Android: duration is in milliseconds
                console.log(`song duration:`, duration);
            });
        }
  //todo: change to something more meaningfull (new RegExp('^' + "https\:\/\/|http\:\/\/", 'i')).test(selectedVideoEntity.audioPath)
  if(selectedVideoEntity.audioPath.startsWith("http")){
    console.log("getting from url");
    videoViewModel.audioPlayer.playFromUrl(audioParams).then(audioPlayerCreatedCallback);
  }
  else{
    videoViewModel.audioPlayer.playFromFile(audioParams).then(audioPlayerCreatedCallback);
  }

设置完成后,我继续制作视频播放器。这是在xml中实现的。

<Page xmlns="http://schemas.nativescript.org/tns.xsd"  xmlns:VideoPlayer="nativescript-videoplayer" 
    loaded="loaded" class="page"  navigatingFrom="onNavigatingFrom" actionBarHidden="true">
    <StackLayout id="layout">
        <VideoPlayer:Video id="videoPlayer" controls="true" loop="false"
        finished="{{ videoFinished }} "
        src="{{ videoPlayerViewModel.videoPath }}" observeCurrentTime="true" muted="true" />

        <VideoPlayer:Video id="rewardVideoPlayer" controls="true"  loop="false"
          finished="{{ rewardVideoFinished }} "/>
    </StackLayout>
</Page>

然后我从关联的页面文件中抓取对象

let videoPlayer = page.getViewById("videoPlayer");

期待视频拥有它的源并且此时全部设置。 现在是问题的开始。我想要的是同时暂停视频和音频,所以我的方法是暂停音频,每当我调用 cycleVideoPlayer 的暂停功能和后记,就像这样:

videoViewModel.audioPlayer.pause();
videoViewModel.videoPlayer.pause();

此时只有其中一个停止(主要是音频),视频继续播放。有什么想法吗?

插件的链接: https://github.com/bradmartin/nativescript-audio https://github.com/bradmartin/nativescript-videoplayer

澄清情况

这只会暂停其中一首曲目并恢复暂停的曲目,而不会产生任何影响

videoViewModel.audioPlayer.pause();
videoViewModel.videoPlayer.pause();
// Wait a few seconds
videoViewModel.audioPlayer.resume();
videoViewModel.videoPlayer.play();

暂停视频并完美恢复

videoViewModel.videoPlayer.pause();
// Wait a few seconds
videoViewModel.videoPlayer.play();

同样适用于音频

videoViewModel.audioPlayer.pause();
// Wait a few seconds
videoViewModel.audioPlayer.resume();

此处给出了尝试暂停和恢复的显式代码。此代码位于exports.loaded函数中,位于我的nativescript页面.js文件中。

cycleVideoPlayer.on(gestures.GestureTypes.swipe, function(eventdata) {
        console.log("Swipe detected, with direction: " + eventdata.direction);
        if (!paused) {

          (function() {
            vm.audioPlayer.pause();
            vm.cycleVideoPlayerViewModel.pause(); //This doesnt work alongside audiopause.. why?
            paused = true;
            console.log("Paused");
          });
        } else {
          (function() {
            vm.audioPlayer.resume();
            vm.cycleVideoPlayerViewModel.play();
            paused = false;
            console.log("Resumed");
          });
        }
      });

如果有人对此事有任何想法或需要澄清,请评论:)

0 个答案:

没有答案