Skip to content

added android callback onEnd #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,42 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.Callback;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

public class RNAudioPlayerModule extends ReactContextBaseJavaModule {
ReactApplicationContext reactContext;
MediaPlayer mp;
ReactApplicationContext reactContext;
MediaPlayer mp;

public RNAudioPlayerModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
public RNAudioPlayerModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}

@Override
public String getName() {
return "RNAudioPlayer";
}
@Override
public String getName() {
return "RNAudioPlayer";
}

@ReactMethod
public void play(String audio) {
String fname = audio.toLowerCase();
int resID = this.reactContext.getResources().getIdentifier(fname, "raw", this.reactContext.getPackageName());
mp = MediaPlayer.create(this.reactContext, resID);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
mp = null;
}
});
}
@ReactMethod
public void play(String audio) {
String fname = audio.toLowerCase();
int resID = this.reactContext.getResources().getIdentifier(fname, "raw", this.reactContext.getPackageName());
mp = MediaPlayer.create(this.reactContext, resID);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
mp = null;
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("audioEnd", null);
}
});
}
}