Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
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 @@ -39,7 +39,9 @@
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.view.FlutterNativeView;
import io.flutter.view.TextureRegistry;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -238,9 +240,18 @@ void dispose() {
}

public static void registerWith(Registrar registrar) {
final VideoPlayerPlugin plugin = new VideoPlayerPlugin(registrar);
final MethodChannel channel =
new MethodChannel(registrar.messenger(), "flutter.io/videoPlayer");
channel.setMethodCallHandler(new VideoPlayerPlugin(registrar));
channel.setMethodCallHandler(plugin);
registrar.addViewDestroyListener(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not written java lambda's much but I think this can be shortened to:

registrar.addViewDestroyListener((FlutterNativeView view) {
  plugin.onDestroy();
  return false;
});

or some such.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote it that way, but the PR checker complained that this is not compatible with Java 7 (I don't know why it cares about Java 7 ;).

new PluginRegistry.ViewDestroyListener() {
@Override
public boolean onViewDestroy(FlutterNativeView view) {
plugin.onDestroy();
return false; // We are not interested in assuming ownership of the NativeView.
}
});
}

private VideoPlayerPlugin(Registrar registrar) {
Expand All @@ -252,6 +263,17 @@ private VideoPlayerPlugin(Registrar registrar) {

private final Registrar registrar;

void onDestroy() {
// The whole FlutterView is being destroyed. Here we release resources acquired for all instances
// of VideoPlayer. Once https://github.com/flutter/flutter/issues/19358 is resolved this may
// be replaced with just asserting that videoPlayers.isEmpty().
// https://github.com/flutter/flutter/issues/20989 tracks this.
for (VideoPlayer player : videoPlayers.values()) {
player.dispose();
}
videoPlayers.clear();
}

@Override
public void onMethodCall(MethodCall call, Result result) {
TextureRegistry textures = registrar.textures();
Expand Down