我正在尝试在nativescript-vue
中构建一个具有音频内容的应用程序。我有一个在页面为mounted()
时被调用的函数:
this.player = new TNSPlayer();
const playerOptions = {
audioFile: this.audioLink,
loop: false,
autoplay: false,
};
this.player
.initFromUrl(playerOptions)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log("something went wrong...", err);
});
this.checkInterval = setInterval(() => {
this.player.getAudioTrackDuration().then(duration => {
// iOS: duration is in seconds
// Android: duration is in milliseconds
let current = this.player.currentTime
if (isIOS) {
duration *= 1000
current *= 1000
}
this.currentTime = current;
this.totalTime = duration;
this.progress = Math.ceil(current / duration * 100);
this.isPlaying = this.player.isAudioPlaying()
});
}, 200)
我需要知道何时准备音频播放器,以便显示播放/暂停按钮。另外我还需要检查按下android的后退按钮时正在调用的事件。
当前,即使单击了android的后退按钮,我的音频仍会继续播放。我想在按下后退按钮时停止/暂停播放器。
编辑: 我尝试过:
var applicationModule = require("application");
var AndroidApplication = applicationModule.android;
var activity = AndroidApplication.foregroundActivity;
activity.onBackPressed = function () {
console.log('Back button pressed.')
};
但这会引发错误onBackPressed
。
TypeError:无法设置未定义的属性“ onBackPressed”
答案 0 :(得分:2)
使用Vuex,我会尝试将player
放在商店中,然后在后按下事件中将其停止。
app.js:
if (application.android) {
application.android.on(application.AndroidApplication.activityBackPressedEvent, function() {
// commit or dispatch stop player
);
}