时间:2019-05-05 来源:小程序工厂
h***:
https://developers.weixin.qq.com/blogdetail?action=get_post_info&lang=zh_CN&token=690657009&docid=000c4a5065c3d8a25166124445a400
我以前是调用背景音乐会出问题,后来改成了AudioContext才好,不知道你这里是为啥。
娇***:
麻烦给个相关的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html),我们定位下问题
如何让用户自主更改名称+微信小程序教程
E***: onMusicTap(event) {
let that = this
let node = this.data.node
let isPlayingMusic = node.isPlayingMusic
// let audioProgress = app.globalData.g_trackAudioProgress.get(node.id)
const backgroundAudioManager = app.globalData.backgroundAudioManager
node.isPlayingMusic = this.data.node.isPlayingMusic
node.stopPoint = processTotalDuration(this.data.node.listened)
backgroundAudioManager.onTimeUpdate(e => {
that.setData({
duration: processTotalDuration(backgroundAudioManager.duration),
currentTime: processTotalDuration(backgroundAudioManager.currentTime),
percent: parseInt(100 * (backgroundAudioManager.currentTime / backgroundAudioManager.duration))
})
app.globalData.g_trackAudioProgress.set(node.id, {
duration: backgroundAudioManager.duration,
stopPoint: backgroundAudioManager.currentTime
})
if (app.globalData.g_trackAudioProgress.get(node.id)) {
// 循环播放 TODO:
if (app.globalData.g_trackAudioProgress.get(node.id).stopPoint >= (app.globalData.g_trackAudioProgress.get(node.id).duration) - 3) {
app.globalData.g_trackAudioProgress.set(node.id, {
duration: backgroundAudioManager.duration,
stopPoint: 0,
})
}
}
console.log(app.globalData.g_trackAudioProgress.get(node.id))
})
// 穿透:改变数据源(course-text)里
this.triggerEvent('customevent', node, {
bubbles: true
})
// 再加事件绑定
this.setMusicMonitor()
if (isPlayingMusic) {
// 暂停回调
backgroundAudioManager.pause(e => {
console.log('will plause')
})
} else {
backgroundAudioManager.src = `${node.content}`
backgroundAudioManager.title = node.title
// 首次点击,从node中获取时间,之后从audioProgress获取
backgroundAudioManager.startTime = that.data.isFirstTap ? convertTimeToSeconds(that.data.node.listened) : app.globalData.g_trackAudioProgress.get(node.id) && app.globalData.g_trackAudioProgress.get(node.id).stopPoint
that.setData({
isFirstTap: false
})
console.log('will play')
}
},
setMusicMonitor() {
const backgroundAudioManager = app.globalData.backgroundAudioManager
let that = this
let node = this.data.node
//点击播放图标和总控开关都会触发这个函数
wx.onBackgroundAudioPlay(e => {
console.log('playing...')
node.isPlayingMusic ? null : that.onMusicTap()
})
wx.onBackgroundAudioPause(e => {
console.log('pausing...')
node.isPlayingMusic ? that.onMusicTap() : null
})
wx.onBackgroundAudioStop(e => {
if (app.globalData.g_trackAudioProgress.get(node.id)) {
// 循环播放 TODO:
if (app.globalData.g_trackAudioProgress.get(node.id).stopPoint >= app.globalData.g_trackAudioProgress.get(node.id).duration - 3) {
app.globalData.g_trackAudioProgress.set(node.id, {
duration: backgroundAudioManager.duration,
stopPoint: 0
})
}
}
console.log('stopping...', app.globalData.g_trackAudioProgress.get(node.id))
node.isPlayingMusic ? that.onMusicTap() : null
})
},
音频播放模拟机没问题,真机老是少播放2秒,求助SOS
f***:
解决了么?我也遇到了,ios10的播放音频,暂停,然后播放视频播放几秒之后就没有声音了,视频一直还在播,暂停或者拖进度之后就又有声音了,播视频的操作是把音频停止了,调用audio的stop()方法