react-native-audioplayer icon indicating copy to clipboard operation
react-native-audioplayer copied to clipboard

Stop method

Open gangstaJS opened this issue 9 years ago • 4 comments

How implement method .stop()?

gangstaJS avatar Dec 16 '15 12:12 gangstaJS

Add to AudioPlayer.js:

pause() {
    RNAudioPlayer.pause();
  },
  stop() {
    RNAudioPlayer.stop();
  }

For iOS:

RCT_EXPORT_METHOD(pause)
{
    [self.audioPlayer pause];
}

RCT_EXPORT_METHOD(stop)
{
    [self.audioPlayer stop];
} 

For Android: I'm working on that right now

puntubabu avatar Dec 24 '15 18:12 puntubabu

To add to @puntubabu's solution - thank you for this :).

If you then wish to be able to resume the paused sound you will need to add logic to handle this or a specific resume function..

add the following to AudioPlayer.js

  resume() {
    RNAudioPlayer.resume();
  }

and the following to RNAudioPlayer.m

RCT_EXPORT_METHOD(resume)
{
    [self.audioPlayer play]; // yes, play not resume.. the normal intermediary play function has no ability to call play without breaking the existing player, this gets around it.
} 

Did you get anywhere with an Android version @puntubabu ?

MossP avatar Jan 05 '16 16:01 MossP

@MossP I've written this to get resume to work in Android Here is the RNAudioPlayerModule.java modification:

public void play(String audio) {
    if (audio != null) {
      String fname = audio.toLowerCase();
      int resID = this.reactContext.getResources().getIdentifier(fname, "raw", this.reactContext.getPackageName());
      mp = MediaPlayer.create(this.reactContext, resID);
    }
    mp.start();
  }
MediaPlayer.start()

is used to start and resume audio. If we pass in a filename, then play that file name, else I am assuming that we are resuming paused audio. http://developer.android.com/reference/android/media/MediaPlayer.html

I modified AudioPlayer.js play method as so:

  play(fileName: string) {
    if (fileName === undefined) {
      RNAudioPlayer.play(null);
    } else {
      fileName = Platform.OS === 'ios' ? fileName : fileName.replace(/\.[^/.]+$/, "");
      RNAudioPlayer.play(fileName);
    }
  }

@MossP I don't think we need a resume-specific function because both iOS and Android use the same method for resume and start

puntubabu avatar Jan 06 '16 02:01 puntubabu

Ah thanks @puntubabu . That's a better way to handle it then, given that they are both the same. Strangely the stop and pause functions for iOS also appear to do the same thing as calling play() after a stop or pause both cause the audio to resume from where it was stopped (or paused).

MossP avatar Jan 06 '16 10:01 MossP