Skip to content

Commit d89f8ac

Browse files
authored
Merge pull request #20 from nini22P/dev
fix: not being able to continue playback after startup
2 parents 97edffd + 48e9552 commit d89f8ac

6 files changed

Lines changed: 98 additions & 31 deletions

File tree

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## v1.3.3
2+
3+
### Changelog
4+
5+
* Fix issue of not being able to continue playback after startup
6+
7+
### 更新日志
8+
9+
* 修复启动后无法继续播放的问题
10+
11+
112
## v1.3.2
213

314
### Changelog

lib/hooks/use_fvp_player.dart

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,15 @@ FvpPlayer useFvpPlayer(BuildContext context) {
5050
final List<Subtitle> externalSubtitles = useMemoized(
5151
() => currentPlay?.file.subtitles ?? [], [currentPlay?.file.subtitles]);
5252

53+
final initValue = useState(false);
54+
55+
final isInitializing = useState(false);
56+
57+
Future<void> init() async => initValue.value = true;
58+
5359
final controller = useMemoized(() {
5460
if (file == null) return VideoPlayerController.networkUrl(Uri.parse(''));
61+
isInitializing.value = true;
5562
final storage = useStorageStore().findById(file.storageId);
5663
final auth = storage?.getAuth();
5764
switch (checkDataSourceType(file)) {
@@ -75,21 +82,28 @@ FvpPlayer useFvpPlayer(BuildContext context) {
7582
httpHeaders: auth != null ? {'authorization': auth} : {},
7683
);
7784
}
78-
}, [file]);
85+
}, [file, initValue.value]);
7986

8087
useEffect(() {
8188
() async {
8289
if (controller.dataSource.isEmpty) return;
83-
await controller.initialize();
84-
await controller.setLooping(repeat == Repeat.one ? true : false);
85-
await controller.setVolume(isMuted ? 0 : volume / 100);
90+
91+
try {
92+
await controller.initialize();
93+
await controller.setLooping(repeat == Repeat.one ? true : false);
94+
await controller.setVolume(isMuted ? 0 : volume / 100);
95+
} catch (e) {
96+
logger('Error initializing player: $e');
97+
}
98+
99+
isInitializing.value = false;
86100
}();
87101

88102
return () {
89103
controller.dispose();
90104
externalSubtitle.value = null;
91105
};
92-
}, [controller]);
106+
}, [controller, initValue.value]);
93107

94108
useEffect(() => controller.dispose, []);
95109

@@ -222,6 +236,9 @@ FvpPlayer useFvpPlayer(BuildContext context) {
222236

223237
Future<void> play() async {
224238
await useAppStore().updateAutoPlay(true);
239+
if (!controller.value.isInitialized && !isInitializing.value) {
240+
init();
241+
}
225242
controller.play();
226243
}
227244

@@ -260,6 +277,7 @@ FvpPlayer useFvpPlayer(BuildContext context) {
260277

261278
return FvpPlayer(
262279
controller: controller,
280+
isInitializing: isInitializing.value,
263281
isPlaying: isPlaying,
264282
externalSubtitle: externalSubtitle,
265283
externalSubtitles: externalSubtitles,

lib/hooks/use_media_kit_player.dart

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,34 @@ MediaKitPlayer useMediaKitPlayer(BuildContext context) {
124124
}
125125
}
126126

127-
useEffect(() {
128-
if (currentFile == null || playQueue.isEmpty) {
129-
player.stop();
130-
} else {
131-
final storage = useStorageStore().findById(currentFile.storageId);
127+
final isInitializing = useState(false);
128+
129+
Future<void> init(FileItem file) async {
130+
if (file.uri == '') return;
131+
isInitializing.value = true;
132+
133+
try {
134+
final storage = useStorageStore().findById(file.storageId);
132135
final auth = storage?.getAuth();
133-
logger('Now playing: ${currentFile.uri}, auto play: $autoPlay');
134-
player.open(
136+
await player.open(
135137
Media(
136-
currentFile.uri,
138+
file.uri,
137139
httpHeaders: auth != null ? {'authorization': auth} : {},
138140
),
139141
play: autoPlay,
140142
);
143+
} catch (e) {
144+
logger('Error initializing player: $e');
145+
}
146+
147+
isInitializing.value = false;
148+
}
149+
150+
useEffect(() {
151+
if (currentFile == null || playQueue.isEmpty) {
152+
player.stop();
153+
} else {
154+
init(currentFile);
141155
}
142156
return () {
143157
if (currentFile != null && player.state.duration != Duration.zero) {
@@ -252,6 +266,11 @@ MediaKitPlayer useMediaKitPlayer(BuildContext context) {
252266

253267
Future<void> play() async {
254268
await useAppStore().updateAutoPlay(true);
269+
if (duration == Duration.zero &&
270+
currentFile != null &&
271+
!isInitializing.value) {
272+
await init(currentFile);
273+
}
255274
await player.play();
256275
}
257276

@@ -285,6 +304,7 @@ MediaKitPlayer useMediaKitPlayer(BuildContext context) {
285304
externalSubtitles: externalSubtitles ?? [],
286305
audio: audio,
287306
audios: audios,
307+
isInitializing: isInitializing.value,
288308
isPlaying: playing,
289309
position: duration == Duration.zero ? Duration.zero : position.value,
290310
duration: duration,

lib/models/player.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:media_kit_video/media_kit_video.dart' as media_kit_video;
55
import 'package:video_player/video_player.dart';
66

77
class MediaPlayer {
8+
final bool isInitializing;
89
final bool isPlaying;
910
final List<Subtitle> externalSubtitles;
1011
final Duration position;
@@ -26,6 +27,7 @@ class MediaPlayer {
2627
final Future<void> Function(Duration) seekTo;
2728

2829
MediaPlayer({
30+
required this.isInitializing,
2931
required this.isPlaying,
3032
required this.externalSubtitles,
3133
required this.position,
@@ -64,6 +66,7 @@ class MediaKitPlayer extends MediaPlayer {
6466
required super.externalSubtitles,
6567
required this.audio,
6668
required this.audios,
69+
required super.isInitializing,
6770
required super.isPlaying,
6871
required super.position,
6972
required super.duration,
@@ -91,6 +94,7 @@ class FvpPlayer extends MediaPlayer {
9194

9295
FvpPlayer({
9396
required this.controller,
97+
required super.isInitializing,
9498
required super.isPlaying,
9599
required this.externalSubtitle,
96100
required super.externalSubtitles,

lib/pages/player/control_bar.dart

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,37 @@ class ControlBar extends HookWidget {
104104
),
105105
),
106106
DarkTheme(
107-
child: IconButton(
108-
tooltip:
109-
'${player.isPlaying == true ? t.pause : t.play} ( Space )',
110-
icon: Icon(
111-
player.isPlaying == true
112-
? Icons.pause_rounded
113-
: Icons.play_arrow_rounded,
114-
size: 36,
115-
),
116-
onPressed: () {
117-
showControl();
118-
if (player.isPlaying == true) {
119-
player.pause();
120-
} else {
121-
player.play();
122-
}
123-
},
107+
child: Stack(
108+
alignment: Alignment.center,
109+
children: [
110+
IconButton(
111+
tooltip:
112+
'${player.isPlaying == true ? t.pause : t.play} ( Space )',
113+
icon: Icon(
114+
player.isPlaying == true
115+
? Icons.pause_rounded
116+
: Icons.play_arrow_rounded,
117+
size: 36,
118+
),
119+
onPressed: () {
120+
showControl();
121+
if (player.isPlaying == true) {
122+
player.pause();
123+
} else {
124+
player.play();
125+
}
126+
},
127+
),
128+
if (player.isInitializing)
129+
SizedBox(
130+
width: 36,
131+
height: 36,
132+
child: CircularProgressIndicator(
133+
strokeWidth: 4,
134+
color: Theme.of(context).colorScheme.surface,
135+
),
136+
),
137+
],
124138
),
125139
),
126140
if (playQueueLength > 1)

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: iris
22
description: "A lightweight video player"
33
publish_to: 'none'
4-
version: 1.3.2+3
4+
version: 1.3.3+3
55

66
environment:
77
sdk: ^3.5.4

0 commit comments

Comments
 (0)