Problem with Audio.Sound()

This is my first time using the Expo SDK Audio, so I’m clueless about what I’m doing wrong. Because it’s copied from the documentation, I guess the problem is the onPress = () => part, but I don’t know how to fix it. I googled, searched in the forums and in GitHub, but didn’t find any examples of how to implement a button that plays a local file or people making this same mistake. Here’s the code:

          <Button
            title="Play a sound!"
            onPress={ () => {
                      const soundObject = new Audio.Sound();
                      try {
                        soundObject.loadAsync(require('./assets/sounds/corchea.mp3'));
                        soundObject.playAsync();
                      } catch (error) {
                        console.log(error);
                      }  
                    }}
          />

Hi! Do you think you could create a Snack with the problem you’re experiencing and describe in more detail what problem you’re having?

Sure, here’s the snack with the entire code:
snack.expo.io/@santiagopereira/app

I’m sorry that I cannot be more specific about this, I have really no clue
why this is happening. There aren’t any errors, it’s just that the sound
isn’t played.

Hey Santiago you can take a look at this snack for reference https://snack.expo.io/BkBHa2cMM works on phone but not the simulator in the snack.
Few notes my snack shows a lot of errors
IOS in snack simulator loads but android doesn’t and some how expo app works on iphone.
ignore the .setRateAsync(3, false); that makes a audio like effect on the original sound i have.
let me know if this helps

Best Shine

simulator in browser doesn’t have audio thats why the sound doesn’t work in the snack but should work on the phone.

Well, it works on the IOS simulator, but not on Android Simulator or my
phone (Android too). The error it logs is “Failed to install module
'./src/components/common”. I tried importing the Button from react-native,
but then it just changes to “Failed to install module './sounds/1.wav”. I
tried changing the path of the file, its name, the file itself, but it just
keeps doing the same. This error never showed up in my original code. Do
you know what could be going on?

Are you still on the snack ? or did you try to recreate it in your code editor?

I am still using the Snack

try recreating it in your code with the

handlePLay1 = async () => {

  const soundObject = new Expo.Audio.Sound();
    try {
      await soundObject.loadAsync(require('your file name goes here'));
    { shouldPlay: true }
      this.audioPlayer1  = soundObject;
        this.audioPlayer1.playAsync();
        this.audioPlayer1.setPositionAsync(0);
        this.audioPlayer1.setRateAsync(3, false);
     // Your sound is playing!
    } catch (error) {
    // An error occurred!
    
    } 

}

and see what happens

1 Like

I tried to implement it using the function and also directly with the
onPress, tested in both my phone and the Simulator, but the problem keeps
being the same: without showing any errors, the audio file does not play

Here’s the link to the code: https://snack.expo.io/@santiagopereira/app

I can’t create a account for login so i can’t take a look :frowning_face:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.