重新启动组件上的Audio()流

时间:2017-04-04 21:51:32

标签: javascript reactjs audio-streaming web-audio

我有一个包含音频元素的反应组件,它是一个直播。当组件最初加载时,它开始播放,但是如果我卸载组件然后重新安装播放按钮不起作用并且流不会启动。是否有我遗漏的东西?

更新: 这里要求的是代码:

import React from 'react'
import compose from 'recompose/compose'
import lifecycle from 'recompose/lifecycle'

const enhance = compose(
  lifecycle({
    componentDidMount() {
     this.audio = new Audio()
     this.audio.src = 'http://some-stream-url.com'
     this.audio.play()
     this.audio.muted = true
    },
    componentDidUpdate() {
     this.audio.muted = !this.props.playing
    },
    componentWillUnmount() {
     this.audio = ''
     this.audio = null
    }
 })
)

const Player = (props) => null

export default enhance(Player)

1 个答案:

答案 0 :(得分:0)

我会将您的audio.play()放在audio.src的onload事件中。好像您的组件在卸载时会在重新安装且您的src尚未完全从服务器重新加载时产生延迟。