Unable to play mp3 file

#1

I’m trying to play an mp3 file. Here’s my file structure:

- app_name
   - assets
      - sounds
         - alarm.mp3

Here’s my code (only included relevant portions):

import { BarCodeScanner, Permissions, Audio, Asset } from 'expo';

componentDidMount() {
    this.playSound();
  }

  playSound = async () => {
    await Expo.Audio.setIsEnabledAsync(true);
    const sound = new Expo.Audio.Sound({
      source: require('./assets/sounds/alarm.mp3')
    });
    await sound.loadAsync();
    await sound.playAsync();
  }

I’m getting an error saying:

Possible Unhandled Promise Rejection (id: 0): Error: Cannot load null source!

What am I doing wrong? Thanks!

#2

So this got me too for half the day, check your api version (and the api version you are reading in the expo documentation). When you google expo audio, first link goes to expo v16 documentation . . .

Anyway, new api is more like this:

  playSound = async () => {
    await Audio.setIsEnabledAsync(true);
    const sound = new Audio.Sound();
    await sound.loadAsync(require('./assets/sounds/alarm.mp3'));
    await sound.playAsync();
  }
1 Like