-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathstory_video.dart
164 lines (138 loc) · 3.95 KB
/
story_video.dart
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:video_player/video_player.dart';
import '../utils.dart';
import '../controller/story_controller.dart';
class VideoLoader {
String url;
File? videoFile;
Map<String, dynamic>? requestHeaders;
LoadState state = LoadState.loading;
VideoLoader(this.url, {this.requestHeaders});
void loadVideo(VoidCallback onComplete) {
if (this.videoFile != null) {
this.state = LoadState.success;
onComplete();
}
final fileStream = DefaultCacheManager()
.getFileStream(this.url, headers: this.requestHeaders as Map<String, String>?);
fileStream.listen((fileResponse) {
if (fileResponse is FileInfo) {
if (this.videoFile == null) {
this.state = LoadState.success;
this.videoFile = fileResponse.file;
onComplete();
}
}
});
}
}
class StoryVideo extends StatefulWidget {
final StoryController? storyController;
final VideoLoader videoLoader;
final Widget? loadingWidget;
final Widget? errorWidget;
StoryVideo(this.videoLoader, {
Key? key,
this.storyController,
this.loadingWidget,
this.errorWidget,
}) : super(key: key ?? UniqueKey());
static StoryVideo url(String url, {
StoryController? controller,
Map<String, dynamic>? requestHeaders,
Key? key,
Widget? loadingWidget,
Widget? errorWidget,
}) {
return StoryVideo(
VideoLoader(url, requestHeaders: requestHeaders),
storyController: controller,
key: key,
loadingWidget: loadingWidget,
errorWidget: errorWidget,
);
}
@override
State<StatefulWidget> createState() {
return StoryVideoState();
}
}
class StoryVideoState extends State<StoryVideo> {
Future<void>? playerLoader;
StreamSubscription? _streamSubscription;
VideoPlayerController? playerController;
@override
void initState() {
super.initState();
widget.storyController!.pause();
widget.videoLoader.loadVideo(() {
if (widget.videoLoader.state == LoadState.success) {
this.playerController =
VideoPlayerController.file(widget.videoLoader.videoFile!);
playerController!.initialize().then((v) {
setState(() {});
widget.storyController!.play();
});
if (widget.storyController != null) {
_streamSubscription =
widget.storyController!.playbackNotifier.listen((playbackState) {
if (playbackState == PlaybackState.pause) {
playerController!.pause();
} else {
playerController!.play();
}
});
}
} else {
setState(() {});
}
});
}
Widget getContentView() {
if (widget.videoLoader.state == LoadState.success &&
playerController!.value.isInitialized) {
return Center(
child: AspectRatio(
aspectRatio: playerController!.value.aspectRatio,
child: VideoPlayer(playerController!),
),
);
}
return widget.videoLoader.state == LoadState.loading
? Center(
child: widget.loadingWidget?? Container(
width: 70,
height: 70,
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
strokeWidth: 3,
),
),
)
: Center(
child: widget.errorWidget?? Text(
"Media failed to load.",
style: TextStyle(
color: Colors.white,
),
));
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.black,
height: double.infinity,
width: double.infinity,
child: getContentView(),
);
}
@override
void dispose() {
playerController?.dispose();
_streamSubscription?.cancel();
super.dispose();
}
}