Chewie and Video player had a wrong video duration on some videos in Android

This issue has been tracked since 2022-09-23.

I created a video player using the Chewie package with video_player package as well, in Android it's not getting the correct video duration, on my UI is I have a video player and a list of videos to play when user select an item I then load the video.

Problem:

  1. The video progress indicator is finished even the video is still playing
  2. The total video duration displayed is always 00:00

Reproduce:

  1. Switch to other videos from the list.
  2. Pay attention to the video progress indicator and the total duration displaye.
Code Sample
import 'package:chewie/chewie.dart';
import 'package:eyenation_mobile_app/api/pdf_certificate_api.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:eyenation_mobile_app/api/pdf_api.dart';
//Helper
import 'package:eyenation_mobile_app/helper/download_helper.dart';
//Provider
import 'package:eyenation_mobile_app/provider/device_provider.dart';
import 'package:eyenation_mobile_app/provider/video_provider.dart';
//Model
import 'package:eyenation_mobile_app/model/video_certificate.dart'
    show VideoCertificate, Item, VideoEventReport;

class VideoPlayerEyeNation extends StatefulWidget {
  const VideoPlayerEyeNation({
    Key? key,
    required this.video,
    required this.filename,
  }) : super(key: key);

  final String video;
  final String filename;
  @override
  State<StatefulWidget> createState() {
    return _VideoPlayerEyeNationState();
  }
}

class _VideoPlayerEyeNationState extends State<VideoPlayerEyeNation> {
  late VideoPlayerController _videoPlayerController1;

  ChewieController? _chewieController;
  String uri = "";
  String _filename = "";
  @override
  void initState() {
    super.initState();
    _getFirstVideo();
  }

  _getFirstVideo() async {
    final _ref =
        await Provider.of<DeviceProvider>(context, listen: false).getPath();
    List<String> _firstVideo =
        await Provider.of<VideoProvider>(context, listen: false)
            .getFirstVideo(_ref);
    initializePlayer(_firstVideo[0], _firstVideo[1]);
  }

  @override
  didUpdateWidget(VideoPlayerEyeNation oldWidget) {
    if (oldWidget.video != widget.video) {
      toggleVideo();
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    _videoPlayerController1.dispose();
    _chewieController?.dispose();

    super.dispose();
  }

  Future<void> initializePlayer(String url, String filename) async {
    _videoPlayerController1 = VideoPlayerController.network(url)
      ..initialize().then((_) {
        setState(() {
          uri = url;
          _filename = filename;
          _videoPlayerController1.play();
        });
      });
    _videoPlayerController1.addListener(() {
      print("Video player DURATION: ${_videoPlayerController1.value.duration}");
      print(
          "CHEWIE player SIZE: ${_chewieController!.videoPlayerController.value.duration}");
    });
    _createChewieController(_videoPlayerController1);
  }

  _createChewieController(videoPlayer) async {
    return _chewieController = ChewieController(
        videoPlayerController: videoPlayer,
        allowFullScreen: true,
        autoPlay: false,
        looping: false,
        additionalOptions: (context) {
          return <OptionItem>[
            OptionItem(
              onTap: () {
                downloadVideo(context, uri, _filename);
              },
              iconData: Icons.download,
              title: AppLocalizations.of(context)!.download,
            ),
          ];
        },
        materialProgressColors: ChewieProgressColors(
          playedColor: Colors.red,
          bufferedColor: Colors.transparent,
        ),
        cupertinoProgressColors: ChewieProgressColors(
          playedColor: Colors.red,
          bufferedColor: Colors.transparent,
        ),
        placeholder: Container(
          color: Colors.grey,
        ),
        progressIndicatorDelay: null);
  }

  Future<void> toggleVideo() async {
    //await _videoPlayerController1.pause();
    _videoPlayerController1.dispose();
    _chewieController?.dispose();
    await initializePlayer(widget.video, widget.filename);
  }

  @override
  Widget build(BuildContext context) {
    return (_chewieController != null &&
                _chewieController!.videoPlayerController.value.isInitialized) &&
            _videoPlayerController1.value.isInitialized
        ? Stack(
            children: [
              Chewie(
                controller: _chewieController!,
              ),
              VideoProgressIndicator(
                _videoPlayerController1,
                allowScrubbing: true,
                padding: EdgeInsets.symmetric(horizontal: 20),
              ),
              ValueListenableBuilder(
                valueListenable: _videoPlayerController1,
                builder: (context, VideoPlayerValue value, child) {
                  //Do Something with the value.
                  return Text("${value.position}");
                },
              )
            ],
          )
        : Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const CircularProgressIndicator(),
                const SizedBox(height: 20),
                Text(AppLocalizations.of(context)!.loading),
              ],
            ),
          );
  }
}
exaby73 wrote this answer on 2022-09-23

Hello @helendeguinion1. Thank you for filing this issue. Can you please provide the following information?

  • A complete and minimal, reproducible example. Ideally a single main.dart file contents that can be directly run. If you are using a video from assets, please include a download link for the video as well or a public Github repository that includes the file.
  • The output of flutter doctor -v
  • The output of flutter run-v
More Details About Repo
Owner Name flutter
Repo Name flutter
Full Name flutter/flutter
Language Dart
Created Date 2015-03-06
Updated Date 2022-09-30
Star Count 145381
Watcher Count 3565
Fork Count 23363
Issue Count 11208

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date