The video_player plugin fails to play videos – Flutter

Issue

I updated my flutter to 1.22.0 and now my video_player giving me Exception errors. It was working fine before I updated my flutter to 1.22.0.

Here is my error:

E/flutter ( 8545): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)]
Unhandled Exception:
‘package:flutter/src/services/message_codec.dart’: Failed assertion:
line 111 pos 15: ‘code != null’: is not true. E/flutter ( 8545): #0
_AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39) E/flutter ( 8545): #1 _AssertionError._throwNew
(dart:core-patch/errors_patch.dart:36:5) E/flutter ( 8545): #2
new PlatformException
(package:flutter/src/services/message_codec.dart:111:15) E/flutter (
8545): #3 VideoPlayerApi.create
(package:video_player_platform_interface/messages.dart:205:13)
E/flutter ( 8545): E/flutter ( 8545): #4
MethodChannelVideoPlayer.create
(package:video_player_platform_interface/method_channel_video_player.dart:46:42)
E/flutter ( 8545): #5 VideoPlayerController.initialize
(package:video_player/video_player.dart:275:45) E/flutter ( 8545): #6
_ScreenBackgroundVideoState.initState (package:flow/widget/background/screen_background_video.dart:17:9)
E/flutter ( 8545): #7 StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:4765:58) E/flutter (
8545): #8 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:4601:5) E/flutter ( 8545):
#9 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8545): #10 MultiChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6236:32) E/flutter (
8545): #11 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8545): #12 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8545): #13 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6118:14) E/flutter (
8545): #14 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8545): #15 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8545): #16 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4652:16) E/flutter (
8545): #17 Element.rebuild
(package:flutter/src/widgets/framework.dart:4343:5) E/flutter ( 8545):
#18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5) E/flutter ( 8545):
#19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5) E/flutter ( 8545):
#20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8545): #21 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8545): #22 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6118:14) E/flutter (
8545): #23 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8545): #24 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8545): #25 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6118:14) E/flutter (
8545): #26 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8545): #27 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8545): #28 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6118:14) E/flutter (
8545): #29 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8545): #30 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8545): #31 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6118:14) E/flutter (
8545): #32 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8545): #33 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8545): #34 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4652:16) E/flutter (
8545): #35 Element.rebuild
(package:flutter/src/widgets/framework.dart:4343:5) E/flutter ( 8545):
#36 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5) E/flutter ( 8545):
#37 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5) E/flutter ( 8545):
#38 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8545): #39 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8545): #40 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6118:14) E/flutter (
8545): #41 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8545): #42 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8545): #43 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4652:16)

Here is my code:

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class ScreenBackgroundVideo extends StatefulWidget {
  @override
  _ScreenBackgroundVideoState createState() => _ScreenBackgroundVideoState();
}

class _ScreenBackgroundVideoState extends State<ScreenBackgroundVideo> {
  VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.asset('assets/vid/login.mp4')
      // set background video using assets for login screen
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      }); // background video will play as soon as the splashscreen is closed
    _controller.play();
    _controller
        .setLooping(true); // looping set to true for the background video
    _controller.setVolume(0); // volume set to 0 for background video
  }

  @override
  void dispose() {
    super.dispose();
    // TODO: implement dispose
    _controller.dispose(); // Dispose the controller after play is done.
  }

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

Need help to get this fixed.

Solution

I had to downgrade my flutter version to 1.20.2 for my video_player to work.

If there was any other solution, please let me know.

Answered By – Jyo

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.