Implement disposePlayer on Android.

This commit is contained in:
Ryan Heise 2020-10-10 18:28:15 +11:00
parent 8d1375f03c
commit 70a8dbc5b7
2 changed files with 15 additions and 16 deletions

View File

@ -108,12 +108,8 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
} }
}; };
private final Runnable onDispose; public AudioPlayer(final Context applicationContext, final BinaryMessenger messenger, final String id) {
public AudioPlayer(final Context applicationContext, final BinaryMessenger messenger,
final String id, final Runnable onDispose) {
this.context = applicationContext; this.context = applicationContext;
this.onDispose = onDispose;
methodChannel = new MethodChannel(messenger, "com.ryanheise.just_audio.methods." + id); methodChannel = new MethodChannel(messenger, "com.ryanheise.just_audio.methods." + id);
methodChannel.setMethodCallHandler(this); methodChannel.setMethodCallHandler(this);
eventChannel = new EventChannel(messenger, "com.ryanheise.just_audio.events." + id); eventChannel = new EventChannel(messenger, "com.ryanheise.just_audio.events." + id);
@ -323,10 +319,6 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
Integer index = (Integer)request.get("index"); Integer index = (Integer)request.get("index");
seek(position == null ? C.TIME_UNSET : position / 1000, result, index); seek(position == null ? C.TIME_UNSET : position / 1000, result, index);
break; break;
case "dispose":
dispose();
result.success(new HashMap<String, Object>());
break;
case "concatenatingInsertAll": case "concatenatingInsertAll":
concatenating(request.get("id")) concatenating(request.get("id"))
.addMediaSources((Integer)request.get("index"), getAudioSources(request.get("children")), handler, () -> result.success(new HashMap<String, Object>())); .addMediaSources((Integer)request.get("index"), getAudioSources(request.get("children")), handler, () -> result.success(new HashMap<String, Object>()));
@ -681,7 +673,6 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
} }
public void dispose() { public void dispose() {
if (player == null) return;
mediaSources.clear(); mediaSources.clear();
mediaSource = null; mediaSource = null;
loopingChildren.clear(); loopingChildren.clear();
@ -693,7 +684,6 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
if (eventSink != null) { if (eventSink != null) {
eventSink.endOfStream(); eventSink.endOfStream();
} }
onDispose.run();
} }
private void abortSeek() { private void abortSeek() {

View File

@ -26,15 +26,24 @@ public class MainMethodCallHandler implements MethodCallHandler {
@Override @Override
public void onMethodCall(MethodCall call, @NonNull Result result) { public void onMethodCall(MethodCall call, @NonNull Result result) {
switch (call.method) {
case "init":
final Map<?, ?> request = call.arguments(); final Map<?, ?> request = call.arguments();
switch (call.method) {
case "init": {
String id = (String)request.get("id"); String id = (String)request.get("id");
players.put(id, new AudioPlayer(applicationContext, messenger, id, players.put(id, new AudioPlayer(applicationContext, messenger, id));
() -> players.remove(id)
));
result.success(null); result.success(null);
break; break;
}
case "disposePlayer": {
String id = (String)request.get("id");
AudioPlayer player = players.get(id);
if (player != null) {
player.dispose();
players.remove(id);
}
result.success(new HashMap<String, Object>());
break;
}
default: default:
result.notImplemented(); result.notImplemented();
break; break;