【问题标题】:Handling android back button in nativescvript-vue在 nativescvript-vue 中处理 android 后退按钮
【发布时间】:2019-06-19 08:09:24
【问题描述】:

我正在尝试在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 not found。

TypeError: 无法设置未定义的属性“onBackPressed”

【问题讨论】:

    标签: android nativescript nativescript-vue


    【解决方案1】:

    使用 Vuex,我会尝试将 player 放在 store 中,然后在 back press 事件中停止它。

    app.js:

    if (application.android) {
        application.android.on(application.AndroidApplication.activityBackPressedEvent, function() {
            // commit or dispatch stop player
        );
    }
    

    【讨论】:

    • 感谢您的更新,我尝试使用您的概念。编译时出错。
    • 用我的答案代码替换 activity.onBackPressed 应该可以工作
    • 阅读更多关于lifecycle events on docs
    猜你喜欢
    • 2013-04-22
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 2011-04-02
    • 2017-06-20
    • 2020-12-31
    • 2023-02-17
    相关资源
    最近更新 更多