4

Hello I've been trying to play a video in the background of my flutter application so I followed this issue and this tutorial.

I came up with the following code:

import 'package:flutter/material.dart';

void main() => runApp(VideoWidget());

class VideoWidget extends StatefulWidget {
  @override
  _VideoWidgetState createState() => _VideoWidgetState();
}

class _VideoWidgetState extends State<VideoWidget> {
  VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.asset("images/background.mov")
      ..initialize().then((_) {
        _controller.setLooping(true);
        _controller.play();
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: _controller.value.initialized
          ? AspectRatio(
              aspectRatio: _controller.value.aspectRatio,
              child: VideoPlayer(_controller),
            )
          : Container(),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}

I made sure the video was available in the images folder and on the pubspec.yaml:


environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  hovering: ^1.0.2
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.1
  video_player: ^0.10.5+2

dev_dependencies:
  flutter_test:
    sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - images/2x/IconWithText.png
    - images/IconWithText.png
    - images/background.mov

But after building the application i end up with the following error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
#0      VideoPlayerApi.create (package:video_player_platform_interface/messages.dart:199:7)
<asynchronous suspension>
#1      MethodChannelVideoPlayer.create (package:video_player_platform_interface/method_channel_video_player.dart:46:31)
<asynchronous suspension>
#2      VideoPlayerController.initialize (package:video_player/video_player.dart:275:18)
<asynchronous suspension>
#3      _VideoWidgetState.initState.<anonymous closure> (package:myapp/video_widget.dart)
<asynchronous suspension>

And i call VideoWidget like this:

@override
  Widget build(BuildContext context) {
    var isLogged = false;
    var column = Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        VideoWidget(),
        Table(

I'm a beginner to dart and flutter so make sure to ask me if you need more information to help to resolve this issue and thank you for the help!

Blanky
  • 75
  • 2
  • 9

2 Answers2

11

run the application again. Some time plugins don't work without closing the app and running it again.

Ayush Chauhan
  • 126
  • 2
  • 3
  • 2
    If you are on Windows, stop the running app and run this in the terminal `flutter clean; flutter pub get` and then run the app again and it will start working. – Maruf Hassan Jul 01 '22 at 10:19
3

from your dependencies, your version was to old, try to upgrade it first to 1.0.1 because your error it seems from your dependencies