class _State extends State<HomeScreen> {
// video type
XFile? video;
build
// gesture onTap function + choosing a video
void onLogoTap() async {
final video = await ImagePicker().pickVideo(
source: ImageSource.gallery,
);
}
}
Dart
복사
class CustomPlayer extends StatefulWidget {
final XFile video;
}
class _CustomPlayerState extends State<CustomVideoPlayer> {
//
VideoPlayerController? videoController;
initState() {
super.initState();
initializeController();
}
// async function
initializeController() async {
videoController = await videoPlayerController.file(
// convert path from XFile => File
File(widget.video.path)
);
// initialize controller
await videoController!.initialize();
// setState to rebuild widgets
setState(() {});
}
build() {
// VideoPlayer widget
if (cideoController == null) {
CircularProgressIndicator();
}
return VideoPlayer(videoController!);
}
}
Dart
복사
Simple Process
videoController.value.isPlaying
Stack()
AspectRatio({
aspectRatio: videoController.value.aspectRatio,
})
videoController!.seekTo(targetPosition)
Dart
복사
initState
videoController!.addListener(() {
final currentPosition = videoController!.value.position;
setState() {
this.currentPosition = currentPosition;
}
}
Dart
복사
