4

I am using Audio API from 'expo-av' library. On pressing the button I should hear audio but I am getting the warning Possible Unhandled Promise Rejection (id: 2): Error: Player does not exist.

On first it was running fine but after one or two tries this error kept on persisting even on removing the $setTimeout() $

    export default class App extends Component {

   async playAudio (Number){
    const SoundObject = new Audio.Sound();
    try {
      let path = AudioList[Number];
      await SoundObject.loadAsync(path);
      await SoundObject.playAsync()
       .then(async playbackStatus => {
         setTimeout(() => {
           SoundObject.unloadAsync();
         },playbackStatus.playbackDurationMillis)
       })
       .catch((error) => {
         console.log(error);
       })
    } catch (err) {
      console.log(err);
    }
  }
  render(){
    return (
      <View style={styles.container}>
        <View style={styles.grid}>
          <Image 
            source={require('./assets/logo.png')}
            style={styles.image}
          />
          <View style={styles.row}>
              <TouchableOpacity
                style={[{backgroundColor: BackgroundNumber[1]}, styles.item]}
                onPress={() => this.playAudio("one")}
              >
                <Text style={styles.itemText}>One</Text>
              </TouchableOpacity>
          </View>

        </View>
      </View>
    );
  }
}
  • The only thing you are not handling is `new Audio.Sound()` - maybe that throws an exception? – Bergi Jul 29 '19 at 21:30
  • That said, avoid [`await`ing a `.then(…)` chain or passing an `aync` function as the `then` callback](https://stackoverflow.com/a/54387912/1048572)! You can even drop the `.catch(…)` entirely as the promise is `await`ed anyway so a rejection would be handled by the `try`/`catch`. – Bergi Jul 29 '19 at 21:32
  • After doing this it works fine for the first time but after refreshing/reloading same error appears. – Vishesh_Malhotra Jul 30 '19 at 09:01

0 Answers0