详细问题描述
项目中用到了背景音频管理器 backgroundAudioManager
,遇到了一些问题
重现步骤
测试了三个网络音频文件,第一次打开正常,后面再打开音频能正常加载,但是时间不更新,回调函数 onPlay
没有调用。
另外还发现同样的音频链接,浏览器打开速度快,App打开慢,这个是什么原因呢?
期望设置了新参数能正确回调函数
IDE运行环境说明
使用HBuilderX,版本是最新的 2.2.2.20190816
uni-app运行环境说明
iOS app,Android App也有同样的问题,demo项目通过HBuilderX创建的默认工程
附件
运行log
18:11:52.946 [LOG] : App Launch at App.vue:4
18:11:52.970 [LOG] : App Show at App.vue:7
18:11:53.945 [LOG] : audio test begin... at pages/index/index.vue:25
18:11:53.969 [LOG] : init bgAudioMannager... at pages/index/index.vue:30
18:11:56.959 [LOG] : ### MUSIC_PARAMS_INIT ###, [Object] {"title":"AUDIO-01","coverImgUrl":"https://assets.hervillageclub.com/images/8bed8f41c54d4a1881816db97ab937...} at pages/index/index.vue:75
18:11:56.982 [LOG] : onWaiting at pages/index/index.vue:53
18:11:57.005 [LOG] : onPlay, [Number] 1429.368 at pages/index/index.vue:34
18:12:16.043 [LOG] : ### MUSIC_PARAMS_INIT ###, [Object] {"title":"AUDIO-02","coverImgUrl":"https://assets.hervillageclub.com/images/8bed8f41c54d4a1881816db97ab937...} at pages/index/index.vue:75
18:12:17.036 [LOG] : onWaiting at pages/index/index.vue:53
通过log可以看到后面播放音频时,回调函数只调用了
onWaiting
,音频开始播放后onPlay
没有调用
偶尔也会有JS Framework
错误提示
06:33:53.571 [ERROR] : [JS Framework] Failed to execute the callback function:
06:33:53.571 ReferenceError: Can't find variable: nan __ERROR
代码片段
<template>
<view class="content">
<view class="text-area">
<text class="title">{{title}}播放进度: {{currentTime}} / {{duration}}</text>
</view>
<button class="btn" @click="onAudioTest(0)">测试音频1</button>
<button class="btn" @click="onAudioTest(1)">测试音频2</button>
<button class="btn" @click="onAudioTest(2)">测试音频3</button>
</view>
</template>
<script>
const bgAudioMannager = uni.getBackgroundAudioManager()
export default {
data() {
return {
title: 'Hello',
currentTime: 0,
duration: 0,
}
},
mounted () {
console.log('audio test begin...')
this.initAudio()
},
methods: {
initAudio () {
console.log('init bgAudioMannager...')
let audio = bgAudioMannager
audio.onPlay(() => {
this.duration = audio.duration
console.log('onPlay', this.duration)
})
audio.onCanplay(() => {
console.log('onCanplay')
this.duration = audio.duration
})
audio.onStop(() => {
console.log('onStop')
})
audio.onEnded(() => {
console.log('onEnded')
})
audio.onPause(() => {
console.log('onPause')
})
audio.onTimeUpdate(() => {
this.currentTime = audio.currentTime
})
audio.onWaiting(() => {
console.log('onWaiting')
})
audio.onSeeking(() => {
console.log('onSeeking')
})
audio.onSeeked(() => {
this.currentTime = audio.currentTime
console.log('onSeeked', this.currentTime)
})
audio.onNext(() => {
console.log('onNext')
})
audio.onPrev(() => {
console.log('onPrev')
})
audio.onError((res) => {
console.error('audio.onError', res)
})
},
startAudio (params) {
let audio = bgAudioMannager
if (params) {
console.log('### MUSIC_PARAMS_INIT ###', params)
if (params.title) {
audio.title = params.title
}
if (params.startTime !== undefined) {
audio.startTime = params.startTime
}
// 当设置了新的 src 时,会自动开始播放
if (params.src) {
audio.src = params.src
}
if (params.coverImgUrl) {
audio.coverImgUrl = params.coverImgUrl
}
}
},
onAudioTest (index) {
if (index === 0) {
let params = {
"title": "AUDIO-01",
"coverImgUrl": "https://assets.hervillageclub.com/images/8bed8f41c54d4a1881816db97ab937bf.png",
"src": "https://cdn.hervillageclub.com/202010100837/d41e7ca519ff3368f0490cfbe5e77683/d632bff2-f67c-4893-9e9a-2a0719930102",
"startTime": 10
}
this.startAudio(params)
this.title = params.title
} else if (index === 1) {
let params = {
"title": "AUDIO-02",
"coverImgUrl": "https://assets.hervillageclub.com/images/8bed8f41c54d4a1881816db97ab937bf.png",
"src": "https://cdn.hervillageclub.com/202010100838/aca72ab091ec5cf1866cc6f7fffd4d05/ff6f9ffd-c412-467d-94da-bd415f320308",
"startTime": 20
}
this.startAudio(params)
this.title = params.title
} else if (index === 2) {
let params = {
"title": "AUDIO-03",
"coverImgUrl": "https://assets.hervillageclub.com/images/8bed8f41c54d4a1881816db97ab937bf.png",
"src": "https://cdn.hervillageclub.com/202010100838/f56edd47b82a0917f8971dac8b39289b/5965b210-df55-41b5-bcb1-a3c7772dc5fe",
"startTime": 30
}
this.startAudio(params)
this.title = params.title
}
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.text-area {
margin-top: 40upx;
display: flex;
justify-content: center;
}
.title {
font-size: 26upx;
color: #8f8f94;
}
.btn {
width: 400upx;
margin-top: 10upx;
}
</style>
另外附件是demo压缩包。