Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A VideoPlaverController was used after being disposed.Once you have called dispose() on a VideoplaverController, it can no longer be used. #170

Closed
CodeInn1024 opened this issue Jun 14, 2019 · 15 comments

Comments

@CodeInn1024
Copy link

微信图片_20190614180321

@dmlzj
Copy link

dmlzj commented Jun 18, 2019

I have the same problem!

@js1972
Copy link

js1972 commented Jun 29, 2019

Are you using the video player inside a list - if so - what happens is that the list disposes list items as they scroll out of view. What I am doing to get around this is to set the cacheExtent on the List.builder of CustomScrollView (depending on which one you are using) to a really large value like 10000.0. Then you can scroll up and down with heaps of items without issue.

However - if you did have hundreds of items you'll still hit an issue. For me its no problem as I limit my list to 50 items.

@Mayankk1995
Copy link

Mayankk1995 commented Jul 8, 2019

So I faced the same problem. I compared it with the example given and I found one very silly error that I was making. It was related to making 'super' calls in initState() and dispose().

  void initState() {
    super.initState();  //Super should be called at the very beginning of init
    _controller = VideoPlayerController.network(
      'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4',
    );
    _chewieController = ChewieController(
      videoPlayerController: _controller,
      aspectRatio: 3 / 2,
      autoPlay: true,
      looping: true,
    );
  }

@override
  void dispose() {
    _controller.dispose();
    _chewieController.dispose();
    super.dispose(); //Super should be called at the very end of dispose
  }

//Let me know if this doesn't solve your issue.

@khietlam
Copy link

khietlam commented Jul 12, 2019

try this

 Future<void> _initializePlay(int index) async {
    _controller = VideoPlayerController.asset(videoList[index]);
    _controller.addListener(_controllerListener);
    if (_controller.value.hasError) {
      print(_controller.value.errorDescription);
    }
    _chewieController = ChewieController(
      videoPlayerController: _controller,
      aspectRatio: 9 / 16,
      showControls: false,
      allowedScreenSleep: false,
    );
    _chewieController.setVolume(1.0);
    _initializeVideoPlayerFuture = _controller.initialize();
  }

// and put this code in the place you want to dispose 

 _initializeVideoPlayerFuture = null;
        _controller?.pause()?.then((_) {
          _controller.removeListener(_controllerListener);
          Future.delayed(Duration(seconds: 2));
          _controller.dispose();

@VictorUvarov
Copy link
Contributor

in the example they have:

@override
  void dispose() {
    _videoPlayerController?.dispose();
    chewieController?.dispose();
    super.dispose();
  }

@kangjinwen
Copy link

So I faced the same problem. I compared it with the example given and I found one very silly error that I was making. It was related to making 'super' calls in initState() and dispose().

  void initState() {
    super.initState();  //Super should be called at the very beginning of init
    _controller = VideoPlayerController.network(
      'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4',
    );
    _chewieController = ChewieController(
      videoPlayerController: _controller,
      aspectRatio: 3 / 2,
      autoPlay: true,
      looping: true,
    );
  }

@override
  void dispose() {
    _controller.dispose();
    _chewieController.dispose();
    super.dispose(); //Super should be called at the very end of dispose
  }

//Let me know if this doesn't solve your issue.

TKS, it's worked .

@ashcode07
Copy link

So I faced the same problem. I compared it with the example given and I found one very silly error that I was making. It was related to making 'super' calls in initState() and dispose().

  void initState() {
    super.initState();  //Super should be called at the very beginning of init
    _controller = VideoPlayerController.network(
      'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4',
    );
    _chewieController = ChewieController(
      videoPlayerController: _controller,
      aspectRatio: 3 / 2,
      autoPlay: true,
      looping: true,
    );
  }

@override
  void dispose() {
    _controller.dispose();
    _chewieController.dispose();
    super.dispose(); //Super should be called at the very end of dispose
  }

//Let me know if this doesn't solve your issue.

this doesn't worked for me

@cbenhagen
Copy link
Collaborator

Can you please provide a non-working example?

@ashcode07
Copy link

I was using a list view so i just have to add cacheExtent and it solved the problem for me

@cbenhagen
Copy link
Collaborator

cbenhagen commented Dec 22, 2019

Please feel free to open a new issue if you think there is something we can do in Chewie to improve this.

@pishguy
Copy link

pishguy commented Jan 3, 2020

I have the same problem!

@VictorUvarov
Copy link
Contributor

Make sure that you check if the widget is still mounted before you use the videoController

@Am607
Copy link

Am607 commented Jun 26, 2020

i have the same promlem

@Johndavid437
Copy link

i have the same problem still....
Any one find the solution please help me.

My code :

import 'package:better_player/better_player.dart';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:responsive_widgets/responsive_widgets.dart';

class videolsight extends StatefulWidget {
final String courseid;
videolsight({required this.courseid});

@OverRide
_videolsightState createState() => _videolsightState();
}

class _videolsightState extends State {
final CollectionReference _productRef = FirebaseFirestore.instance.collection("lsight");

@OverRide
Widget build(BuildContext context) {
ResponsiveWidgets.init(
context,
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
allowFontScaling: false,
);
return ResponsiveWidgets.builder(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
allowFontScaling: false,
child: OrientationBuilder(
builder: (context, orientation){
if(orientation == Orientation.portrait){
return Scaffold(
body: Stack(
children: [
FutureBuilder(
future: _productRef.doc(widget.courseid).get(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Scaffold(
body: Center(
child: Text("Error : ${snapshot.error}"),
),
);
}
if (snapshot.connectionState == ConnectionState.done) {

                  Map<String, dynamic> documentData = snapshot.data.data();

                  return Scaffold(

                    body: SingleChildScrollView(
                      child: Column(
                        children: [
                          Padding(
                              padding: const EdgeInsets.only(top:20,),
                              child: BetterPlayer.network(documentData["video"],

                              )
                          ),


                          Padding(
                            padding:  const EdgeInsets.only(top: 25,),
                            child: Text("Class Name : ${documentData['name']}", style:  GoogleFonts.poppins(            fontSize: 20,
                                fontWeight: FontWeight.w600
                            ),
                              textAlign: TextAlign.left,
                            ),
                          ),


                        ],
                      ),
                    ),
                  );
                }


                return Scaffold(
                  body: Center(
                    child: CircularProgressIndicator(
                      color: Color(0xff005AA6),
                    ),
                  ),
                );
              },
            ),
            Padding(
              padding: const EdgeInsets.only(top: 320,left: 10),
              child: Text("More on this Course :",style: TextStyle(

                  fontWeight: FontWeight.w800,
                  fontSize: 16
              ),

              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 340),
              child: FutureBuilder<QuerySnapshot>(
                future: _productRef.orderBy('class',).get(),
                builder: (context, snapshot) {
                  if (snapshot.hasError) {
                    return Scaffold(
                      body: Center(
                        child: Text("Error : ${snapshot.error}"),
                      ),
                    );
                  }
                  if (snapshot.connectionState == ConnectionState.done) {
                    return ListView(
                      children: snapshot.data!.docs.map((document) {
                        return GestureDetector(
                          onTap: () {
                            Navigator.pushReplacement(context, MaterialPageRoute(
                                builder: (context) =>
                                    videolsight(courseid: document.id,)));

                          },
                          child: Container(
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(18.0),
                              ),
                              margin: EdgeInsets.symmetric(
                                vertical: 1.0,
                                horizontal: 4.0,
                              ),
                              child: Row(
                                children: [
                                  Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: Container(
                                      width: 90,
                                      height: 90,
                                      child: ClipRRect(
                                        borderRadius: BorderRadius.circular(12.0),
                                        child: SvgPicture.network(
                                          "${document['image']}",
                                          fit: BoxFit.cover,
                                          placeholderBuilder: (BuildContext context) => Scaffold(
                                            body: Center(
                                              child: Container(
                                                  width: 50,
                                                  height: 50,

                                                  child: Image.asset("assets/load.gif",) ),
                                            ),
                                          ),

                                        ),
                                      ),
                                    ),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: Container(
                                      width: MediaQuery
                                          .of(context)
                                          .size
                                          .width*0.60,
                                      child: Text(
                                        document['name'] ?? "Course",
                                        style: TextStyle(
                                            fontWeight: FontWeight.w800,
                                            fontSize: 20
                                        ),),
                                    ),
                                  ),
                                ],
                              )
                          ),
                        );
                      },).toList(),
                    );
                  }
                  return Scaffold(
                    body: Center(
                      child: CircularProgressIndicator(color: Color(0xff005AA6)),
                    ),
                  );
                },
              ),
            ),
          ],
        )
    );}
  else{
    return Scaffold(
      body:  FutureBuilder<dynamic>(
        future: _productRef.doc(widget.courseid).get(),
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Scaffold(
              body: Center(
                child: Text("Error : ${snapshot.error}"),
              ),
            );
          }
          if (snapshot.connectionState == ConnectionState.done) {

            Map<String, dynamic> documentData = snapshot.data.data();

            return Scaffold(

              body: SingleChildScrollView(
                child: Column(
                  children: [
                    BetterPlayer.network(documentData["video"],
                      betterPlayerConfiguration: BetterPlayerConfiguration(
                        fullScreenByDefault: true,
                          controlsConfiguration: BetterPlayerControlsConfiguration(
                            fullscreenDisableIcon: Icons.cast,
                            fullscreenEnableIcon:  Icons.cast,
                          )
                      ),
                    ),





                  ],
                ),
              ),
            );
          }


          return Scaffold(
            body: Center(
              child: CircularProgressIndicator(
                color: Color(0xff005AA6),
              ),
            ),
          );
        },
      ),
    );
  }
  }
  ),
);

}
}

Please help me with this . I'm struggling for days

@Nialixus
Copy link

Adding cache extent to the list still works to this day. You're a lifesaver, @js1972!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests