Skip to content
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

Android: Ensure any audio files that are playing when app exits are stopped & released #87

Open
wants to merge 3 commits 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
30 changes: 30 additions & 0 deletions android/src/main/java/com/zmxv/RNSound/RNSoundModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.io.IOException;

import android.util.Log;
Expand Down Expand Up @@ -456,4 +457,33 @@ public Map<String, Object> getConstants() {
constants.put("IsAndroid", true);
return constants;
}

/**
* Ensure any audios that are playing when app exits are stopped and released
*/
@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();

Set<Map.Entry<Integer, MediaPlayer>> entries = playerPool.entrySet();
for (Map.Entry<Integer, MediaPlayer> entry : entries) {
MediaPlayer mp = entry.getValue();
if (mp == null) {
continue;
}
try {
mp.setOnCompletionListener(null);
mp.setOnPreparedListener(null);
mp.setOnErrorListener(null);
if (mp.isPlaying()) {
mp.stop();
}
mp.reset();
mp.release();
} catch (Exception ex) {
Log.e("RNSoundModule", "Exception when closing audios during app exit. ", ex);
}
}
entries.clear();
}
}