Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Release VideoPlayer resources when FlutterView is gone #739

Merged
merged 1 commit into from
Aug 27, 2018

Conversation

nichtverstehen
Copy link
Contributor

Otherwise the exoplayer MediaPlayer objects continue generating frames after FlutterNativeView is destroyed and onFrameAvailableListener for the video target SurfaceTexture tries to access a missing engine instance.

This is a workaround for widgets not receiving dispose signals when FlutterView is destroyed (flutter/flutter#19358).
flutter/flutter#20989 tracks ensuring that resources are released.

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 ;).

Copy link
Contributor

@mehmetf mehmetf left a comment

Choose a reason for hiding this comment

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

Nice! Did you test that this does what you intend it to do?

@nichtverstehen
Copy link
Contributor Author

Yes, in conjunction with flutter/engine#6079 we do see FlutterView cleanly released: it doesn't crash and verbose log records that I temporarily added are produced in an expected order.

I didn't find any unittests for VideoPlayer, so I didn't add test cases.

@Hixie
Copy link
Contributor

Hixie commented Aug 24, 2018

Please don't hesitate to add unit tests even if we don't have any yet.

void onDestroy() {
// The whole FlutterView is being destroyed. Here we release resources
// acquired for all instances of VideoPlayer.
// TODO(https://github.com/flutter/flutter/issues/20989) Replace with
Copy link
Contributor

Choose a reason for hiding this comment

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

syntax for TODOs is TODO(username): description, url

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 rephrased this in another way, so that I'm not on the hook, but the bug owner :).

@mehmetf
Copy link
Contributor

mehmetf commented Aug 25, 2018

Kirill if you can think of a quick way to write a test for it, it would be great. Otherwise, please address Ian's comment about the TODO and you are good to go. Thanks for fixing it!

@nichtverstehen
Copy link
Contributor Author

Testing the Java code against FlutterView is a bit tough right now (b/111201965), so I'll keep it as-is for now. (FWIW, there are no Java tests in this repo now).

…terView is being destroyed.

Otherwise the exoplayer MediaPlayer objects continue generating frames after FlutterNativeView is destroyed and onFrameAvailableListener for the video target SurfaceTexture tries to access a missing engine instance.

This is a workaround for widgets not receiving dispose signals when FlutterView is destroyed (flutter/flutter#19358).
flutter/flutter#20989 tracks ensuring that resources are released.
@mehmetf mehmetf merged commit 9f80468 into flutter:master Aug 27, 2018
andreidiaconu pushed a commit to andreidiaconu/plugins that referenced this pull request Feb 17, 2019
…terView is being destroyed. (flutter#739)

Otherwise the exoplayer MediaPlayer objects continue generating frames after FlutterNativeView is destroyed and onFrameAvailableListener for the video target SurfaceTexture tries to access a missing engine instance.

This is a workaround for widgets not receiving dispose signals when FlutterView is destroyed (flutter/flutter#19358).
flutter/flutter#20989 tracks ensuring that resources are released.
andreidiaconu added a commit to andreidiaconu/plugins that referenced this pull request Feb 17, 2019
…ing FlutterView is being destroyed. (flutter#739)"

This reverts commit e235935.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
4 participants