Skip to content

Commit

Permalink
video: pause if not visible
Browse files Browse the repository at this point in the history
  • Loading branch information
resucutie committed Dec 15, 2024
1 parent cf589be commit 1ee7d5a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/components/video_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:visibility_detector/visibility_detector.dart';

class VideoView extends StatefulWidget {
const VideoView(this.path, {super.key, this.showControls = true});
Expand Down Expand Up @@ -50,7 +51,19 @@ class VideoViewState extends State<VideoView> {
Widget build(BuildContext context) {
return AspectRatio(
aspectRatio: _videoController.value.aspectRatio,
child: _isLoaded ? Chewie(controller: _chewieController,) : const SizedBox(height: 0,)
child: _isLoaded ? VisibilityDetector(
key: Key("unique key"),
onVisibilityChanged: (info) {
debugPrint("${info.visibleFraction} of my widget is visible");
if(info.visibleFraction == 0){
_videoController.pause();
}
else{
_videoController.play();
}
},
child: Chewie(controller: _chewieController,)
): const SizedBox(height: 0,)
);
}
}
4 changes: 3 additions & 1 deletion lib/views/image_manager/components/image_upload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class ImageUploadForm extends StatelessWidget {
if(state.value.isEmpty) {
return const Icon(Icons.add);
} else {
if(lookupMimeType(state.value)!.startsWith("video/")) return IgnorePointer(child: VideoView(state.value, showControls: false,),);
if(lookupMimeType(state.value)!.startsWith("video/")) return IgnorePointer(
child: VideoView(state.value, showControls: false,),
);
return Image(
image: ResizeImage(
FileImage(File(state.value)),
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.3"
visibility_detector:
dependency: "direct main"
description:
name: visibility_detector
sha256: dd5cc11e13494f432d15939c3aa8ae76844c42b723398643ce9addb88a5ed420
url: "https://pub.dev"
source: hosted
version: "0.4.0+2"
vm_service:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ dependencies:
fvp: ^0.28.0
video_player: ^2.9.2
chewie: ^1.8.5
visibility_detector: ^0.4.0+2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 1ee7d5a

Please sign in to comment.