Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter video_player dispose

Tags:

flutter

dart

I'm using provider with video_player to manage the state.

VideoProvider has:

videoPlayerController = VideoPlayerController.network(...);

which I frequently change when the user switches to a new video (same screen). If I directly assign a new VideoPlayerController.network(...) into the videoPlayerController, the old video will still play and you can hear the sound. The workaround is to videoPlayerController.pause() then assign a new VideoPlayerCOntroller.network afterwards.

Is the previous Video being disposed by GC? Are there any performance issues with this? I want to get rid of the previous video and the resources used before switching to a new one. I can't videoPlayerController.dispose() before switching because it causes an error.

like image 917
AnsellC Avatar asked Nov 20 '19 13:11

AnsellC


People also ask

How do I dispose of a video player?

Call . dispose() To remove the html associated with your videojs player from the page always call the player's dispose() method: var oldPlayer = document. getElementById('my-player'); videojs(oldPlayer). dispose();

How do I dispose of stateful widget flutter?

Flutter – dispose() Method with ExampleDispose is a method triggered whenever the created object from the stateful widget is removed permanently from the widget tree. It is generally overridden and called only when the state object is destroyed.


2 Answers

When you call dispose your controller is still being used by VideoPlayer widget. First, you need to make sure that it's not used anymore (setting controller in state to null) and AFTER that you call dispose.

I'm not sure about your state management via Provider, but I'll give you an example how to do this with regular State.

  VideoPlayerController _controller;

  void _initController(String link) {
    _controller = VideoPlayerController.network(link)
      ..initialize().then((_) {
        setState(() {});
      });
  }

  Future<void> _onControllerChange(String link) async {
    if (_controller == null) {
      // If there was no controller, just create a new one
      _initController(link);
    } else {
      // If there was a controller, we need to dispose of the old one first
      final oldController = _controller;

      // Registering a callback for the end of next frame
      // to dispose of an old controller
      // (which won't be used anymore after calling setState)
      WidgetsBinding.instance.addPostFrameCallback((_) async {
        await oldController.dispose();

        // Initing new controller
        _initController(link);
      });

      // Making sure that controller is not used by setting it to null
      setState(() {
        _controller = null;
      });
    }
  }
like image 148
Igor Kharakhordin Avatar answered Sep 23 '22 11:09

Igor Kharakhordin


I know its late but this will help others with same problem.

I had same issue, i wanted to change Video when user selects new video from list, after researching so much i finally created a solution by myself.

Follows the below step and you can video video in Video Player in same screen

  1. Create a StatefulWidget class containing VideoPlayer and initialize the videoPlayerController in init method. Suppose you created a StatefulWidget class named MyVideoPlayer then in constructor accept two variable, i.e, i> String videoLink ii> UniqueKey()

Note - You have to pass UniqueKey() to the super of this class, example,

class MyVideoPlayer extends StatefulWidget {
  final String videoLink;
  final UniqueKey newKey;

  MyVideoPlayer(this.videoLink, this.newKey): super(key: newKey); // passing Unique key to dispose old class instance and create new with new data

  @override
  _MyVideoPlayerState createState() => _MyVideoPlayerState();
} ...
  1. Replace your VideoPlayer instance of main dart file where you are playing video with your previously created class i.e, MyVideoPlayer and pass videoLink and UniqueKey().

  2. Now whenever you want to change the video just update videoLink inside setState(() {}) of your main dart file and after that a new VideoPlayer instance is created by disposing the old one totally.

Note - Here the main work is done by UniqueKey(), by passing UniqueKey() you are saying flutter to create a new unique instance of this particular class;

like image 30
Harsh Barnwal Avatar answered Sep 21 '22 11:09

Harsh Barnwal