所以我目前正在使用nativescript的多平台框架构建一个应用程序。我有两个有问题的插件可以自己工作,但是当它们同时运行时,我希望暂停两个轨道(具有单独轨道文件的视频和音频),只有一个停止,而其他继续。这是为什么?
首先,我的 nativescript 音频播放器代码是:
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();
此处给出了尝试暂停和恢复的显式代码。此代码位于我的一个 nativescript 页面 .js 文件中的 exports.loaded 函数中。
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");
});
}
});
如果有人对此事有任何想法或需要澄清,请发表评论:)