小程序
小游戏
企业微信
微信支付
扫描小程序码分享
问题描述:
通过wx.getBackgroundAudioManager播放背景音频文件,在真机调试下,程序置于前台自动播放没有问题,将程序置于后台时,自动播放会出现此问题,再将程序切换到前台可以正常播放
2 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
问题解决了吗?
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
模板内容:
<!--page/Demo/index.wxml-->
<
button
type
=
"primary"
bindtap
"audioPlay"
>播放</
>
"audioPause"
>暂停</
"audioStop"
>停止</
"audio24"
>设置当前播放时间为24秒</
"audioStart"
>回到开头</
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
},
""
: 2
"http://book.hhdd.com/share/51681/pages/a23674c8-e4fa-4dfa-ac95-ebb1fabe301c.mp3"
: 3
"http://book.hhdd.com/share/51681/pages/98627940-d04d-4446-8b95-bc708831b944.mp3"
: 4
"http://book.hhdd.com/share/51681/pages/63c2e38f-4ae6-4c78-9ac1-5dc97d3037ff.mp3"
: 5
"http://book.hhdd.com/share/51681/pages/5e1ec76a-5872-4a8f-b427-7b87ce429bb0.mp3"
: 6
}
],
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
console.log(
"onReady"
);
// 背景音频播放器事件列表
const events = {
onCanplay:
onPlay:
onPause:
onStop:
onEnded: () => {
this
.next();
// onTimeUpdate: null,
onPrev:
onNext:
onError:
onWaiting:
};
// 获取背景音频控制器
.audioCtx = wx.getBackgroundAudioManager();
// 添加事件处理
Object.keys(events).forEach(k => {
if
(!events[k]) {
.audioCtx[k]((e) => {
console.log(k, e);
});
return
;
.audioCtx[k](events[k]);
.audioPlay();
next() {
.ind++;
(
.ind >=
.musicList.length)
audioPlay() {
const options = {
title: `${
.name}-${
.ind + 1}`,
epname: `${
.ind}-${
.totalCount}`,
singer:
'咔哒故事'
coverImgUrl:
.coverUrl,
src:
.musicList[
.ind].music
// 音频文件不存在,停留1s播放下一首
.nextTimeout && clearTimeout(
.nextTimeout);
(!options.src) {
"there is no music on the current page, wait for 1s to play the next page."
.nextTimeout = setTimeout(() => {
}, 1000);
console.log(options);
setTimeout(() => {
Object.keys(options).forEach(k => {
.audioCtx[k] = options[k];
.audioCtx.play();
audioPause() {
.audioCtx.pause();
audioStop() {
.audioCtx.stop();
audio24() {
.audioCtx.seek(24);
audioStart() {
.audioCtx.seek(0);
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
问题解决了吗?
模板内容:
<!--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);
}
});