Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect the progress(initialize/end) of video player in Flutter?

Tags:

flutter

I am very new to Flutter and don't know much about it.

I am using video_player and chewie package in Flutter. I want to send analytics data when video starts and ends.

So, I want to know both timing. how can I detect the video start / end timing?

My code is following.

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

class VideoItem extends StatefulWidget {
  final VideoPlayerController videoPlayerController;
  final bool looping;

  VideoItem({
    @required this.videoPlayerController,
    this.looping,
    Key key,
  }) : super(key: key);

  @override
  _VideoState createState() => _VideoState();

}

class _VideoState extends State<VideoItem> {
  ChewieController _chewieController;

  @override
  void initState() {
    super.initState();

    _chewieController = ChewieController(
      videoPlayerController: widget.videoPlayerController,
      aspectRatio: 1 / 1,
      autoPlay: true,
      autoInitialize: true,
      looping: widget.looping,
      allowFullScreen: false,
      allowMuting: true,
      errorBuilder: (context, errorMessage) {
        return Center(
          child: Text(
            errorMessage,
            style: TextStyle(color: Colors.white),
          ),
        );
      },
    );
  }

...

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(0.0),
      child: Chewie(
        controller: _chewieController,
      ),
    );
  }
}
like image 872
tonteki Avatar asked Aug 21 '19 05:08

tonteki


People also ask

How do I make videos play automatically on flutter?

Based on this post's answer, you can use the WidgetsBinding. instance. addPostFrameCallback to autoplay your video.

How do you show videos in container in flutter?

Displaying & Playing the video:The VideoPlayer widget from the video_player plugin is used in flutter to display a video. To control the Aspect ratio of the video, we will wrap it inside a AspectRatio Widget. We will also be adding a FloatingActionButton to control the play and pause of the video as shown below: Dart.


1 Answers

Add a Listener function to your videoPlayerController , and inside that function check the the current position of your VideoPlayer :

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

class VideoItem extends StatefulWidget {
  final VideoPlayerController videoPlayerController;
  final bool looping;

  VideoItem({
    @required this.videoPlayerController,
    this.looping,
    Key key,
  }) : super(key: key);

  @override
  _VideoState createState() => _VideoState();

}

class _VideoState extends State<VideoItem> {
  ChewieController _chewieController;

  @override
  void initState() {
    super.initState();

    widget.videoPlayerController.addListener(checkVideo);

    _chewieController = ChewieController(
      videoPlayerController: widget.videoPlayerController,
      aspectRatio: 1 / 1,
      autoPlay: true,
      autoInitialize: true,
      looping: widget.looping,
      allowFullScreen: false,
      allowMuting: true,
      errorBuilder: (context, errorMessage) {
        return Center(
          child: Text(
            errorMessage,
            style: TextStyle(color: Colors.white),
          ),
        );
      },
    );
  }

...

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(0.0),
      child: Chewie(
        controller: _chewieController,
      ),
    );
  }

    void checkVideo(){
    // Implement your calls inside these conditions' bodies : 
    if(videoPlayerController.value.position == Duration(seconds: 0, minutes: 0, hours: 0)) {
      print('video Started');
    }

    if(videoPlayerController.value.position == videoPlayerController.value.duration) {
      print('video Ended');
    }

  }
} 
like image 91
Mazin Ibrahim Avatar answered Sep 30 '22 06:09

Mazin Ibrahim