componentDidMount 中的 react-native-sound
react-native-sound in componentDidMount
我想在 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
根据文档,您应该在构造函数中使用回调:
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
我想在 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
根据文档,您应该在构造函数中使用回调:
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