zmxv / react-native-sound

React Native module for playing sound clips
MIT License
2.78k stars 749 forks source link

Android : Cannot Play Mp3 on Real Device's #664

Closed bobbyrinaldy closed 1 year ago

bobbyrinaldy commented 4 years ago

:beetle: Description

Hello, im trying to play mp3 from physical device (Real Device). the code working for IOS but on ANDROID it says 'Resource not found : error code -1'

:beetle: What have you tried?

the file path is like this : /storage/emulated/0/Download/bf7befd8a3abf8aa0bad9d359214fd22.mp3

i have try to add file:// like this : file:///storage/emulated/0/Download/bf7befd8a3abf8aa0bad9d359214fd22.mp3 and file://storage/emulated/0/Download/bf7befd8a3abf8aa0bad9d359214fd22.mp3

also, i have try to add content:// like this : content:///storage/emulated/0/Download/bf7befd8a3abf8aa0bad9d359214fd22.mp3 and content://storage/emulated/0/Download/bf7befd8a3abf8aa0bad9d359214fd22.mp3

but nothing work above.

:beetle: Please post your code:

play = async path => {
    let dirs = RNFetchBlob.fs.dirs;
    var pathFile = Platform.select({
      ios: dirs.DocumentDir,
      android: dirs.DownloadDir,
    });
    if (this.sound) {
      this.sound.play(this.playComplete);
      this.setState({playState: 'playing'});
    } else {
     //
      const filepath = pathFile + '/' + path.filename;

      this.sound = new Sound(filepath, null, error => {
        if (error) {
          console.log('failed to load the sound', error);
          Alert.alert('Notice', 'audio file error. (Error code : 1)');
          this.setState({playState: 'paused'});
        } else {
          this.setState({
            playState: 'playing',
            duration: this.sound.getDuration(),
          });
          this.sound.play(this.playComplete);
        }
      });
    }
  };

:bulb: Possible solution

Is your issue with...

Are you using...

Which versions are you using?

Does the problem occur on...

If your problem is happening on a device, which device?

luigbren commented 4 years ago

Hi.. Same problem in Samsung S9+ Device.. sound.mp3 cannot play

luigbren commented 4 years ago

my friends.. my solution was put the code into mountcomponent..

constructor(props) {
    super(props);
    this.state = {
     };
    sound = null
  }

componentDidMount =() => {
    this.sound = new Sound('mysound.mp3', Sound.MAIN_BUNDLE, (error) => {
      if (error) {
        console.log('failed to load the sound', error);
        return;
      }
    });
}

and then turn on when you want.... this.sound.play();

Note: Android: Save your sound clip files under the directory android/app/src/main/res/raw. Note that files in this directory must be lowercase and underscored (e.g. my_file_name.mp3) and that subdirectories are not supported by Android.

ArunKumarVallal99 commented 1 year ago

my friends.. my solution was put the code into mountcomponent..

constructor(props) {
    super(props);
    this.state = {
     };
    sound = null
  }

componentDidMount =() => {
    this.sound = new Sound('mysound.mp3', Sound.MAIN_BUNDLE, (error) => {
      if (error) {
        console.log('failed to load the sound', error);
        return;
      }
    });
}

and then turn on when you want.... this.sound.play();

Note: Android: Save your sound clip files under the directory android/app/src/main/res/raw. Note that files in this directory must be lowercase and underscored (e.g. my_file_name.mp3) and that subdirectories are not supported by Android.

What if We need to access the Download Directory? Fetch from Director and play? Could please provide any solution for this scenario?