getBackgroundAudioManager后台播放抛出错误信息
发布于 5 年前 作者 liangxiulan 16512 次浏览 来自 问答

问题描述:

通过wx.getBackgroundAudioManager播放背景音频文件,在真机调试下,程序置于前台自动播放没有问题,将程序置于后台时,自动播放会出现此问题,再将程序切换到前台可以正常播放

2 回复

问题解决了吗?

模板内容:

<!--page/Demo/index.wxml-->
<button type="primary" bindtap="audioPlay">播放</button>
<button type="primary" bindtap="audioPause">暂停</button>
<button type="primary" bindtap="audioStop">停止</button>
<button type="primary" bindtap="audio24">设置当前播放时间为24秒</button>
<button type="primary" bindtap="audioStart">回到开头</button>

JS示例代码:

// page/Demo/index.js
Page({
    audioCtx: null,
    ind: 0,
    coverUrl: "http://image.hhdd.com/books/cover_rec/af2ee001-d7a9-46e5-a2e3-bbc8fc13d433.jpg",
    name: "我爸爸",
    totalCount: 24,
    musicList: [
        {
            "music": "http://book.hhdd.com/share/51681/pages/54f07360-d3b4-4c16-b3b2-37c83d96eba2.mp3",
            "ind": 1
        },
        {
            "music": "",
            "ind": 2
        },
        {
            "music": "http://book.hhdd.com/share/51681/pages/a23674c8-e4fa-4dfa-ac95-ebb1fabe301c.mp3",
            "ind": 3
        },
        {
            "music": "http://book.hhdd.com/share/51681/pages/98627940-d04d-4446-8b95-bc708831b944.mp3",
            "ind": 4
        },
        {
            "music": "http://book.hhdd.com/share/51681/pages/63c2e38f-4ae6-4c78-9ac1-5dc97d3037ff.mp3",
            "ind": 5
        },
        {
            "music": "http://book.hhdd.com/share/51681/pages/5e1ec76a-5872-4a8f-b427-7b87ce429bb0.mp3",
            "ind": 6
        }
    ],
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        console.log("onReady");
        // 背景音频播放器事件列表
        const events = {
            onCanplay: null,
            onPlay: null,
            onPause: null,
            onStop: null,
            onEnded: () => {
                this.next();
            },
            // onTimeUpdate: null,
            onPrev: null,
            onNext: null,
            onError: null,
            onWaiting: null,
        };
        // 获取背景音频控制器
        this.audioCtx = wx.getBackgroundAudioManager();
        // 添加事件处理
        Object.keys(events).forEach(k => {
            if (!events[k]) {
                this.audioCtx[k]((e) => {
                    console.log(k, e);
                });
                return;
            }
            this.audioCtx[k](events[k]);
        });
 
        this.audioPlay();
    },
    next() {
        this.ind++;
        if (this.ind >= this.musicList.length) return;
        this.audioPlay();
    },
    audioPlay() {
        const options = {
            title: `${this.name}-${this.ind + 1}`,
            epname: `${this.ind}-${this.totalCount}`,
            singer: '咔哒故事',
            coverImgUrl: this.coverUrl,
            src: this.musicList[this.ind].music
        };
        // 音频文件不存在,停留1s播放下一首
        this.nextTimeout && clearTimeout(this.nextTimeout);
        if (!options.src) {
            console.log("there is no music on the current page, wait for 1s to play the next page.");
            this.nextTimeout = setTimeout(() => {
                this.next();
            }, 1000);
            return;
        }
        console.log(options);
        setTimeout(() => {
            Object.keys(options).forEach(k => {
                this.audioCtx[k] = options[k];
            });
            this.audioCtx.play();
        }, 1000);
         
    },
    audioPause() {
        this.audioCtx.pause();
    },
    audioStop() {
        this.audioCtx.stop();
    },
    audio24() {
        this.audioCtx.seek(24);
        this.audioCtx.play();
    },
    audioStart() {
        this.audioCtx.seek(0);
    }
});
回到顶部