Skip to content

Commit

Permalink
feat(h5): innerAudioContext的一些优化:
Browse files Browse the repository at this point in the history
- 修复innerAudioContext.paused无效的问题
- 跳转时暂停音频
- audio api不再使用html标签
  • Loading branch information
Littly committed Apr 17, 2019
1 parent 36d3b0f commit 8a4b6c7
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions packages/taro-h5/src/api/audio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,13 @@ import { createCallbackManager } from '../utils/index'
* @property {(callback: function) => void} onWaiting(function callback) 监听音频加载中事件。当音频因为数据不足,需要停下来加载时会触发
*/

let audioEl
/**
* 创建内部 audio 上下文 InnerAudioContext 对象。
* @returns {InnerAudioContext}
*/
export const createInnerAudioContext = () => {
if (audioEl) document.body.removeChild(audioEl)

/** @type {HTMLAudioElement} */
audioEl = document.createElement('audio')
document.body.appendChild(audioEl)
let audioEl = new Audio()

/** @type {InnerAudioContext} */
const iac = {}
Expand Down Expand Up @@ -79,7 +75,7 @@ export const createInnerAudioContext = () => {
audioEl = null
}

const simpleProperties = [ 'src', 'autoplay', 'loop', 'volume', 'duration', 'currentTime', 'buffered' ]
const simpleProperties = [ 'src', 'autoplay', 'loop', 'volume', 'duration', 'currentTime', 'buffered', 'paused' ]
simpleProperties.forEach(propertyName => {
Object.defineProperty(iac, propertyName, {
get: () => audioEl[propertyName],
Expand All @@ -90,9 +86,6 @@ export const createInnerAudioContext = () => {
Object.defineProperty(iac, 'startTime', {
value: 0
})
Object.defineProperty(iac, 'paused', {
get: () => !audioEl.played
})
Object.defineProperty(iac, 'obeyMuteSwitch', {
value: true
})
Expand Down Expand Up @@ -138,6 +131,8 @@ export const createInnerAudioContext = () => {
})
})

Taro.eventCenter.on('__taroRouterChange', () => { iac.stop() })

return iac
}

Expand Down

0 comments on commit 8a4b6c7

Please sign in to comment.