音樂播放過程當中,切換到應用其餘頁面時,用後臺的狀態欄切換音樂時音樂會暫停。java
華爲audio接口只能在頁面中使用,當audio所在的頁面退出後,頁面銷燬,綁定的上下文對象也隨之不存在。 此時,在通知欄上一首、下一首切換歌曲時,後臺播放服務首先暫停當前歌曲,狀態重置,可是audio接口的綁定事件找不到上下文對象,沒法觸發previous和next事件,致使上述現象。api
在應用首頁監聽audio事件,而不單單是在播放頁面添加,當用戶離開播放頁面時,依然能夠監聽到audio的每一個事件,從而去控制播放邏輯。app
注意:因爲華爲快應用引擎不支持在app.ux裏調用audio接口,因此當用戶退出了整個應用,雖而後臺依然在播放,可是快應用是不能收到audio的事件回調的。oop
如下示例demo中有兩個頁面:首頁Main和音頻Audio。爲了不重複代碼,可維護性,將audio的相關代碼獨立出來做爲一個公共的js,方便每一個頁面調用。ui
1)公共utils.js:this
import audio from '@system.audio'; export default{ listenAudio() { var that=this; console.info("util.js listenAudio "); audio.onplay = function () { console.log('audio onplay') } audio.onpause = function () { console.log('audio onpause') } audio.onended = function () { console.log('audio onended') } audio.ondurationchange = function () { console.log('util.js ondurationchange') var total = audio.duration console.log('util.js ondurationchange total=' + total) } audio.ontimeupdate = function () { var time = audio.currentTime // console.log('util.js ontimeupdate time=' + time) } audio.onprevious = function () { audio.cover = 'https://xx.jpg' audio.title = "鋼琴曲" audio.artist = "莫扎特" // Replace with NetEase cloud music resource link audio.src = 'https://xx.mp3' console.log(' util.js on previout event from notification ') } audio.onnext = function () { audio.cover = 'xx.jpg' audio.title = '輕音樂'; audio.artist = '王菲' // Replace with NetEase cloud music resource link audio.src = 'https://xx.mp3' console.log(' util.js on next event from notification ') } }, getAudioPlayState() { audio.getPlayState({ success: function (data) { console.log(`getAudioPlayState success: state: ${data.state},src:${data.src}, currentTime:${data.currentTime},autoplay:${data.autoplay},loop:${data.loop}, volume: ${data.volume},muted:${data.muted},notificationVisible:${data.notificationVisible}`); }, fail: function (data, code) { console.log('getAudioPlayState fail, code=' + code); } }); }, startPlay() { audio.play(); }, pausePlay() { audio.pause(); }, stopPlay() { audio.stop(); }, seekProress(len) { audio.currentTime = len; }, setVolume(value) { audio.volume = value; }, setMute(isMuted) { audio.muted = isMuted }, setLoop(isloop) { audio.loop = isloop }, setStreamType() { if (audio.streamType === 'music') { audio.streamType = 'voicecall' } else { audio.streamType = 'music' } console.error('audio.streamType =' + audio.streamType); }, setTitle(title) { console.info('setTitle=' + title); audio.title = title; }, setArtist(artist) { console.info('setArtist artist=' + artist) ; audio.artist = artist; }, setCover(src) { console.info('setCover src=' + src); audio.cover = src; } }
2) 在首頁Main的生命週期onShow方法中添加監聽audio事件,調用utils.js中的listenAudio,代碼以下:code
<script> import utils from '../Util/utils.js'; module.exports = { onShow(options) { utils.listenAudio(); }, } </script>
3) 在音頻Audio頁面生命週期onShow方法中添加監聽audio事件,調用utils.js中的listenAudio,因爲在播放頁面須要顯示播放進度,單獨監聽了進度回調事件,代碼以下:對象
onShow(options) { var that = this; utils.listenAudio(); audio.ondurationchange = function () { console.log('audio ondurationchange') that.total = audio.duration console.log('audio ondurationchange total=' + that.total) } audio.ontimeupdate = function () { that.time = audio.currentTime console.log('ontimeupdate time=' + that.time) } },
欲瞭解更多詳情,請參見:接口
快應用audio開發指導:生命週期
https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-api-audio
原文連接:https://developer.huawei.com/consumer/cn/forum/topic/0204411131669010359?fid=18
原做者:Mayism