来自EXPO React Native的音频-停止/暂停所有声音

时间:2020-02-04 10:35:15

标签: react-native audio expo

我当前正在尝试暂停或停止正在播放的声音。我正在使用博览会的“音频”。

handlePlaySound方法可以完美地工作,可以播放给定的声音,但是我无法停止播放声音。

这是我的代码:

handlePlaySound = async note => {
  const soundObject = new Audio.Sound()
  try {
    let source = xyloSounds[note]
    await soundObject.loadAsync(source)
    await soundObject
    .playAsync()
      .then(async playbackStatus => {
        setTimeout(() => {
          soundObject.unloadAsync()
        }, playbackStatus.playableDurationMillis)
      })
      .catch(error => {
        console.log(error)
      })
  } catch (error) {
    console.log(error)
  }
}

Stop = async note => {
  const soundObject = new Audio.Sound()
  try {
    let source = xyloSounds[note]
    await soundObject.stopAsync();
  } catch (error) {
    console.log(error)
  }
}




const SoundScreen = props => {

  return (
    <ScrollView>
     <TouchableOpacity style={styles.color3} 
      onPress={() => this.Stop('sound1')}>
        <Text>STOP</Text>
        </TouchableOpacity>

        <TouchableOpacity style={styles.color3} 
      onPress={() => this.handlePlaySound('sound2')}>
        <Text>PLAY</Text>
        </TouchableOpacity>

提前谢谢

0 个答案:

没有答案