1

I'm initializing a VideoPlayerController with the video_player package (https://pub.dev/packages/video_player) in one dart file in my flutter project and when i want to controller.pause() the video from a different dart file it fails to recognize the controller.

I tried to insert _controller.pause(); in my gesture detector to stop the video when a camera opens but it dosn't work.

GestureDetector(
                  onTap: ()   {
                    _controller.pause();
                    _openCameraWithURL(context, projectTitle, projectURL);
                  },
                  child: Image.asset("assets/images/example.png",
                      fit: BoxFit.contain),
                ),

The video is initialized in the original dart file like this:

class LanguageSelect extends StatefulWidget {
  const LanguageSelect({Key? key}) : super(key: key);

  @override
  _LanguageSelectState createState() => _LanguageSelectState();
}

class _LanguageSelectState extends State<LanguageSelect> {
  late VideoPlayerController _controller;
  bool _visible = false;

  @override
  void initState() {
    // Create and store the VideoPlayerController. The VideoPlayerController
    // offers several different constructors to play videos from assets, files,
    // or the internet.
    _controller = VideoPlayerController.asset('assets/videos/example.mp4');

    /*// Initialize the controller and store the Future for later use.
    _initializeVideoPlayerFuture = _controller.initialize();

    // Use the controller to loop the video.
    _controller.setLooping(true);*/

    _controller.initialize().then((_) {
      _controller.setLooping(true);
      Timer(Duration(milliseconds: 100), () {
        setState(() {
          _controller.play();
          _visible = true;
        });
      });
    });

    super.initState();
  }

  @override
  void dispose() {
    // Ensure disposing of the VideoPlayerController to free up resources.
    _controller.pause();
    _controller.dispose();

    super.dispose();
  }

If anyone knows how to solve this problem i would be very happy to know the solution.

TezzeP
  • 11
  • 1

2 Answers2

0

You might do the following :

  • Declare a singleton class as a video manager then declare the video controller inside, after that you can init the video from the first class and control it from other
Mohamad Alzabibi
  • 166
  • 1
  • 14
0

You can do this by using state management. Heres a link to an article you can follow showing you how to just this:

https://medium.com/@sanjeevmadhav03/preloading-videos-in-flutter-4b65cf0681c6

Manbus
  • 706
  • 4
  • 9