tik_video_player.dart
3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import 'dart:async';
import 'package:Parlando/events/trans_event.dart';
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';
const kDefaultAspectRatio = 16 / 9;
enum VideoType {
asset,
network,
}
class TikVideoPlayer extends StatefulWidget {
const TikVideoPlayer({
Key? key,
required this.path,
this.videoType = VideoType.network,
this.width,
this.height,
this.aspectRatio,
this.autoPlay = false,
this.looping = false,
this.showControls = true,
this.allowFullScreen = true,
this.allowPlaybackSpeedMenu = false,
}) : super(key: key);
final String path;
final VideoType videoType;
final double? width;
final double? height;
final double? aspectRatio;
final bool autoPlay;
final bool looping;
final bool showControls;
final bool allowFullScreen;
final bool allowPlaybackSpeedMenu;
@override
State<StatefulWidget> createState() => _TikVideoPlayerState();
}
class _TikVideoPlayerState extends State<TikVideoPlayer> {
late VideoPlayerController _videoPlayerController;
ChewieController? _chewieController;
late StreamSubscription bus;
@override
void initState() {
initializePlayer();
super.initState();
bus = eventBus.on<TransEvent>().listen((event) {
_chewieController!.videoPlayerController.pause();
});
}
@override
void dispose() {
_videoPlayerController.dispose();
_chewieController!.dispose();
bus.cancel();
super.dispose();
}
double? get width => widget.width == null || widget.width! >= double.infinity
? MediaQuery.of(context).size.width
: widget.width;
double? get height =>
widget.height == null || widget.height! >= double.infinity
? (width != null ? width! / aspectRatio : null)
: widget.height;
double get aspectRatio =>
_chewieController!.videoPlayerController.value.aspectRatio;
Future initializePlayer() async {
_videoPlayerController = widget.videoType == VideoType.network
? VideoPlayerController.network(widget.path)
: VideoPlayerController.asset(widget.path);
await _videoPlayerController.initialize();
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController,
deviceOrientationsOnEnterFullScreen: [
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
],
deviceOrientationsAfterFullScreen: [DeviceOrientation.portraitUp],
aspectRatio: widget.aspectRatio,
autoPlay: widget.autoPlay,
looping: widget.looping,
showControls: widget.showControls,
allowFullScreen: widget.allowFullScreen,
allowPlaybackSpeedChanging: widget.allowPlaybackSpeedMenu,
);
setState(() {});
}
@override
Widget build(BuildContext context) => FittedBox(
fit: BoxFit.cover,
child: SizedBox(
height: height,
width: width,
child: _chewieController != null &&
_chewieController!.videoPlayerController.value.isInitialized
? Chewie(controller: _chewieController!)
: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
CircularProgressIndicator(),
SizedBox(height: 20),
Text('Loading'),
],
),
),
);
}