我试图在props.sound == true时播放声音,并在props.sound = false时停止播放。我不断收到未处理的承诺拒绝错误和错误未定义对象。 这是我的代码:
do { } catch { }
我已经尝试过使用道具,状态等方法,但是只有在我将playAsync放在原始函数中(例如,
constructor(props)
{super(props);
this.soundObject = new Expo.Audio.Sound();
}
stopSound = async () => {
await this.soundObject.stopAsync();
}
playSound = async () => {
try{
await this.soundObject.playAsync();
}catch(error){
this.createSound()
await this.soundObject.playAsync();
}
}
componentWillMount(){
this.createSound();
}
componentWillUnmount(){
this.stopSound();
}
createSound = async() => {
try {
await this.soundObject.loadAsync(require('soundpath'));
await this.soundObject.setVolumeAsync(0.3)
await this.soundObject.setIsLoopingAsync(true)
} catch (error) {
console.log("sound couldn't load")
}
}
componentDidMount(){
if (this.props.sound==true){
this.playSound();
} else {
this.stopSound();
}
}
答案 0 :(得分:0)
错误很简单,请使用require('soundpath.mp3)//声音扩展。