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,
),
);
}
}
Based on this post's answer, you can use the WidgetsBinding. instance. addPostFrameCallback to autoplay your video.
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.
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');
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With