Flutter video_player dispose

video_player flutter
flutter web video player
flutter video player github
flutter chewie dispose
video_player flutter latest version
chewie flutter
flutter video player full screen
flutter media player

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.


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

Play and pause a video, A VideoPlayerController was used after being disposed. I/flutter (15861): Once you have called dispose() on a VideoPlayerController, it can no� Flutter video_player dispose. Related. 215. Finalize vs Dispose. 193. Should I Dispose() DataSet and DataTable? 307. Do you need to dispose of objects and set them to


I know is later but I think i found one way to fix this.

1.Create initPlayer() (I've created using Provider)

  void initPlayer( {pause = false, update = false, updateUrl = ""} ) async {
  var isPlaying = false;

  if (controller == null) {
    controller = VideoPlayerController.file(File(url))..addListener(() {
    isPlaying = true;
    notifyListeners();
   })..initialize().then((value) => notifyListeners());
   controller.play();
   } else {
     print ("Controller is playing...");
   if (pause) {
    if (controller.value.isPlaying) {
      controller.pause();
    } else {
      controller.play();
    }
   }
   if (update && updateUrl != "") {
    final oldController = controller;
    await oldController.dispose();
    controller = VideoPlayerController.file(File(updateUrl))..addListener(() {
      isPlaying = true;
      notifyListeners();
       })..initialize().then((value) => notifyListeners());
      controller.play();
    }
  }
}
  1. Add Actions (start, pause change) in the widget.

          child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
        FlatButton(
          child: Text("Play Video"),
          color: Colors.red,
          onPressed: () {
            musicPlayerProvider.initPlayer();
          },
        ),
        FlatButton(
          child: Text("Stop Video"),
          color: Colors.pink,
          onPressed: () {
            musicPlayerProvider.initPlayer(pause: true);
          },
        ),
        FlatButton(
          child: Text("Change Video"),
          color: Colors.yellow,
          onPressed: () {
            musicPlayerProvider.initPlayer(update: true,updateUrl: 
                "url");
          },
        ),
      ],),
    

For now this works but of course you can make changes.

Later we need to check when the video has finished using addListener method ( I guess ).

video_player 0.7.2: disposing issue � Issue #25661 � flutter/flutter , The video is displayed in a Flutter app by creating a VideoPlayer widget. To reclaim the resources used by the player call dispose. After dispose all further calls� Add the video_player dependency. Add permissions to your app. Create and initialize a VideoPlayerController. Display the video player. Play and pause the video. 1. Add the video_player dependency. This recipe depends on one Flutter plugin: video_player. First, add this dependency to your pubspec.yaml.


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;

VideoPlayerController class - video_player library, Flutter plugin for displaying inline video with other Flutter widgets on Android and iOS. Please set your constraint to video_player: '>=0.10.y+x <2.0.0' @ override void dispose() { super.dispose(); _controller.dispose(); } }. Seifon changed the title flutter video player 0.8.0, Once you have called dispose() flutter video player updated to 0.8.0, Once you have called dispose() Jan 1, 2019 zoechi added severe: crash plugin p: first party labels Jan 2, 2019


video_player, Flutter plugin for displaying inline video with other Flutter widgets on Android and iOS. Please set your constraint to video_player: '>=0.10.y+x <2.0.0' @ override void dispose() { super.dispose(); _controller.dispose(); } }. The Flutter team have created a Video Player widget to display a video in Flutter application. flutter video player example The Video Player widget can play multiple types of videos that may be stored in the mobile, as an asset, and from the internet. This plugin allows flutter to interface with the platform's native video players. In iOS, the video player widget use AVPlayer to handle


video_player, The video player for Flutter with a heart of gold. access to video playback. Chewie uses the video_player under the hood and wraps it in a friendly Material or Cupertino UI! Please make sure to dispose both controller widgets after use. Flutter Video Player – Chewie Tutorial. flutter, tutorial. 6 comments. Trending Tutorials. you need to create a StatefulWidget to get hold of the dispose() method.


Chewie, [video_player] StreamBuilder A VideoPlayerController was used after being disposed � flutter/flutter. When using multiple VideoPlayerControllers (with� Flutter Video Player: A Flutter plugin used for iOS and Android for playing back video on a Widget surface. Adding Dependency Packages to the Flutter: To use Video Player in Flutter we need to add its dependency package into the pubspec.yaml file . Use the below code to add package dependencies: dependencies: video_player: ^0.7.2