Flutter video_player sound sometimes not in sync with video - flutter

I am having an issue with Flutter video_player where sometimes the video hangs and the sound is not in sync with the video. There is a delay of ~1 second between the video and sound. This only happens when playing certain videos, most videos are fine. I've checked that one of the affected videos is in the same format (mp4) as other videos and also have downloaded that video from my S3 bucket and have confirmed that it plays correctly in that case, so I believe it must be an issue with the video_player plugin. Here is my code to load the video controller. Is there any reason that videos would behave differently with this plugin where the audio and video are not in sync?
void loadVideo() async {
videoController =
VideoPlayerController.network(videoLink);
videoController.initialize().then((_) {
if (!mounted) {
// displays an error message if the video controller doesn't load
videoError = true;
setState(() {});
return;
}
setState(() {});
});
videoController.addListener(_listenForError);
}
void playVideo() async {
videoController.play();
}
Widget cameraWidget = Transform.scale(
scale: videoController.value.aspectRatio / deviceRatio,
child: AspectRatio(
aspectRatio: videoController.value.aspectRatio,
child: VideoPlayer(videoController),
),
);

Related

Trying to navigate via HLS video in Flutter

I'm trying to navigate via HLS video. Navigation includes clicking -15 seconds and +15 seconds buttons, clicking on a video progress bar and playing video again when it reaches 30 seconds timestamp. I use Flutter, video_player package with chewie package. HLS is not traditional. I receive a big HLS from video archive: it's a .m3u8 playlist with one .ts file inside it. If I try to paste the url with .m3u8 playlist to the browser it starts to load it and it takes nearly all day.
void makePlayChecker() {
playerTimeChecker = Timer.periodic(Duration(seconds: 1), (timer) {
if (videoPlayerController.value.position > videoMaxLength) {
videoPlayerController.seekTo(Duration(seconds: 0));
}
});
}
Upper method checks whether the video has reached 30 seconds timestamp, if so, I try to navigate it to its beginning, but unfortunately the video just resume playing without any navigation.
Future<void> initializePlayer() async {
videoPlayerController = VideoPlayerController.network(
generatedUrl,
formatHint: VideoFormat.hls,
);
await Future.wait([
videoPlayerController.initialize()
]);
_createChewieController();
await videoPlayerController.play();
setState(() {});
}
void _createChewieController() {
_chewieController = ChewieController(
videoPlayerController: videoPlayerController,
autoPlay: true,
allowMuting: true,
progressIndicatorDelay: Duration(seconds: 0),
customControls: CustomCupertinoControls(
backgroundColor: Colors.blue,
iconColor: Colors.white,
),
overlay: MixinWrapper(child: Text("UserMixin", style: TextStyle(color: Colors.red),)),
allowPlaybackSpeedChanging: false,
hideControlsTimer: Duration(minutes: 5),
);
}
Upper methods initialize video player and chewie controllers
How could I navigate via HLS video?

How to identify if the controls has gone or not in flutter chewie video player

I am working on a flutter app and using chewie videoplayer plugin.So when we started playing a video it shows controls at bottom and after few seconds it vanishes and when we move the mouse again it shows again. so is there any method to find when the controls are shown on screen and when its not shown.
actually i am giving close button on the video player. but it doesnt vanishes with those video player controls. it still stay on screen so to hide that close button along with video controls i need to get which process hides the control.
Please help me...
This process is inside the source code of chewie player. So if you need to customize and synchronise your own custom controls and options, either you have to explicitly fetch the source code and edit in that or you need to make your own controllers from scratch using video_player package as the core.
You need to customize the source code of the chewie player.
You can find MaterialControl class inside the source code of this package there is a function called _buildHitArea() just change notifier.hideStuff boolean according to your preference.
Widget _buildHitArea() {
final bool isFinished = _latestValue.position >= _latestValue.duration;
final bool showPlayButton = widget.showPlayButton && !_dragging && !notifier.hideStuff;
return GestureDetector(
onTap: () {
if (_latestValue.isPlaying) {
if (_displayTapped) {
setState(() {
notifier.hideStuff = false;
});
} else {
_cancelAndRestartTimer();
}
} else {
_playPause();
setState(() {
notifier.hideStuff = false;
});
}
},
child: CenterPlayButton(
backgroundColor: Colors.black54,
iconColor: Colors.white,
isFinished: isFinished,
isPlaying: controller.value.isPlaying,
show: showPlayButton,
onPressed: _playPause,
),
);
}

Not getting the error in Video Player in flutter?

I am using the video_player package provided by flutter team to implement a video player in my flutter project. Everything is working fine when internet is connected. But the problem with me is I could not get the error while turning off my internet and also when I am providing wrong video url to the video player controller. Below is my code:-
videoPlayerController = VideoPlayerController.network(alertClipList[0]);
videoPlayerController.addListener(() {
print('here');
if(videoPlayerController.value.isBuffering){
print('buffering');
}else if(videoPlayerController.value.hasError){
print('error');
}else if(videoPlayerController.value.position == videoPlayerController.value.duration){
print('completed');
}
});
var temp = await videoPlayerController.initialize().then((value){
print('started');
if(videoPlayerController.value.isPlaying){
print('already playing');
return;
}
videoPlayerController.play();
}, onError: (error){
print(error);
print('error is here');
});
And here is the simple UI code:
Widget JVideoPlayer() {
return AspectRatio(
aspectRatio: 16 / 9,
child: VideoPlayer(alertClipController.videoPlayerController));
}
Also I using the below implementation of exoplayer
implementation 'com.google.android.exoplayer:exoplayer:2.16.1'
I solved the issue by removing this line from my app level build.gradle file - implementation 'com.google.android.exoplayer:exoplayer:2.16.1'. I think there is no problem with the video player package, the problem is with the exoplayer.

Playing videos with flutter web and firebase storage

I am trying to make a website in which the user can add videos and also see these videos. All of them are stored in firebase storage. This is how I store it
fb.StorageReference storageRef = fb.storage().ref('videos/$chapter)');
fb.UploadTaskSnapshot uploadTaskSnapshot = await storageRef.put(videoFile, fb.UploadMetadata(contentType: 'video/mp4')).future;
await uploadTaskSnapshot.ref.getDownloadURL().then((value){
videoUrl = value.toString();
});
snapshot.reference.collection("videos").add({
"chapter":chapter,
"class":grade,
"description":description,
"notes":notes,
"subject":subject,
"video":videoUrl,
"timestamp":DateTime.now().millisecondsSinceEpoch.toString()
});
And I play them using the normal video player plugin. Now the problem is when I store it, I get a url like
https://firebasestorage.googleapis.com/v0/b/studyme-me.appspot.com/o/videos%2Ff)?alt=media&token=ec4fea39-032b-438f-8122-f8e042c1c9c7
But the video player in flutter web requires a .mp4 or .wav or something like that file. What can I do that can either allow the video player to play these (I don't think its possible) or I can get the .mp4 file for this. Maybe I can use firebase storage to open this url and get it but I dont know how
Any suggestions would be appreciated
You can use the VideoPlayerController plugin to do that
VideoPlayerController controller = VideoPlayerController.network('your-video-url',);
//Play/Pause Video:
FloatingActionButton(
onPressed: () {
// Wrap the play or pause in a call to `setState`. This ensures the
// correct icon is shown
setState(() {
// If the video is playing, pause it.
if (_controller.value.isPlaying) {
_controller.pause();
} else {
// If the video is paused, play it.
_controller.play();
}
});
},
// Display the correct icon depending on the state of the player.
child: Icon(
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
)
'''
more examples in the link above

Track progress Flutter video player

Hi I'm using the flutter video player plugin, I have something similar like this [{phrase:"something", startAt: 1039}, {phrase:"other somthing", startAt: 26500}] is there a way to change in ui the phrase based on the start time while the video is playing.
I have try to use a timer with a duration of 100 ms but I retrieve no exact time from the Flutter video player plugin.
You probably want to take a look at stateful widgets as they allow you to do exactly this. In short, you can set phrase to be some variable and once the video starts you can call setState and change the variable. Flutter will automatically redraw the widget and the text will be updated.
To determine if the video player is playing you can add a listener to your video controller.
_controller = VideoPlayerController.network(
'http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4',
)
..addListener(() {
final bool isPlaying = _controller.value.isPlaying;
if (isPlaying != _isPlaying) {
setState(() {
_isPlaying = isPlaying;
});
}
})
..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
setState(() {});
});