Search code examples
flutterflutter-desktopflutter-video-player

"Unable to establish connection on channel" error while trying to play video using the video_player package


I'm using Flutter to develop a desktop app that is supposed to play a video in it and therefore I have added the video_player flutter package to my pubspec.yaml file by adding the following:

dependencies:
  flutter:
    sdk: flutter
  
  video_player: ^2.1.1

The following code is used build the widget:

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  VideoPlayerController _controller;
  Future<void> _initializeVideoPlayerFuture;

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

    _initializeVideoPlayerFuture = _controller.initialize();

    super.initState();
  }

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

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 450,
      width: 800,
      // Use a FutureBuilder to display a loading spinner while waiting for the
      // VideoPlayerController to finish initializing.
      child: FutureBuilder(
        future: _initializeVideoPlayerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            _controller.play();
            // If the VideoPlayerController has finished initialization, use
            // the data it provides to limit the aspect ratio of the video.
            return AspectRatio(
              aspectRatio: 16 / 9,
              // Use the VideoPlayer widget to display the video.
              child: VideoPlayer(_controller),
            );
          } else {
            // If the VideoPlayerController is still initializing, show a
            // loading spinner.
            return Center(child: CircularProgressIndicator());
          }
        },
      ),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

However, it ended up with the following error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)

I'm aware that the same question has been answered here but the error there has been caused due to an unsupported .mov file and due to the use of an older version of the package both of which I have verified to be correct in my case.


Solution

  • Flutter video_player plugin has yet to have desktop support. You can track this feature request on this GitHub Issue thread.