1

我想在 componentDidMount 中播放音乐,但我的代码不起作用

import React,{Component} from 'react'
import Nav from './Nav'
import Sound from 'react-native-sound'
const play = new Sound('music.mp3')

class App extends Component {
  componentDidMount(){
          play.play()
  }
  render(){ 
    return(
      <Nav />
    )
  }}
export default App
4

1 回答 1

1

根据文档,您应该在构造函数中使用回调:

import React,{Component} from 'react'
import Nav from './Nav'
import Sound from 'react-native-sound'

class App extends Component {
  componentDidMount(){
    const play = new Sound('music.mp3', (error, play) => {
      if (error) return
      else play.play(() => {
        play.release() // release when done to release resources
      })
    })
  }
  render(){ 
    return(
      <Nav />
    )
  }}
export default App
于 2019-11-15T21:14:10.170 回答