Skip to content

Commit

Permalink
Merge pull request #20 from nini22P/dev
Browse files Browse the repository at this point in the history
fix: not being able to continue playback after startup
  • Loading branch information
nini22P authored Feb 11, 2025
2 parents 97edffd + 48e9552 commit d89f8ac
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 31 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## v1.3.3

### Changelog

* Fix issue of not being able to continue playback after startup

### 更新日志

* 修复启动后无法继续播放的问题


## v1.3.2

### Changelog
Expand Down
28 changes: 23 additions & 5 deletions lib/hooks/use_fvp_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ FvpPlayer useFvpPlayer(BuildContext context) {
final List<Subtitle> externalSubtitles = useMemoized(
() => currentPlay?.file.subtitles ?? [], [currentPlay?.file.subtitles]);

final initValue = useState(false);

final isInitializing = useState(false);

Future<void> init() async => initValue.value = true;

final controller = useMemoized(() {
if (file == null) return VideoPlayerController.networkUrl(Uri.parse(''));
isInitializing.value = true;
final storage = useStorageStore().findById(file.storageId);
final auth = storage?.getAuth();
switch (checkDataSourceType(file)) {
Expand All @@ -75,21 +82,28 @@ FvpPlayer useFvpPlayer(BuildContext context) {
httpHeaders: auth != null ? {'authorization': auth} : {},
);
}
}, [file]);
}, [file, initValue.value]);

useEffect(() {
() async {
if (controller.dataSource.isEmpty) return;
await controller.initialize();
await controller.setLooping(repeat == Repeat.one ? true : false);
await controller.setVolume(isMuted ? 0 : volume / 100);

try {
await controller.initialize();
await controller.setLooping(repeat == Repeat.one ? true : false);
await controller.setVolume(isMuted ? 0 : volume / 100);
} catch (e) {
logger('Error initializing player: $e');
}

isInitializing.value = false;
}();

return () {
controller.dispose();
externalSubtitle.value = null;
};
}, [controller]);
}, [controller, initValue.value]);

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

Expand Down Expand Up @@ -222,6 +236,9 @@ FvpPlayer useFvpPlayer(BuildContext context) {

Future<void> play() async {
await useAppStore().updateAutoPlay(true);
if (!controller.value.isInitialized && !isInitializing.value) {
init();
}
controller.play();
}

Expand Down Expand Up @@ -260,6 +277,7 @@ FvpPlayer useFvpPlayer(BuildContext context) {

return FvpPlayer(
controller: controller,
isInitializing: isInitializing.value,
isPlaying: isPlaying,
externalSubtitle: externalSubtitle,
externalSubtitles: externalSubtitles,
Expand Down
36 changes: 28 additions & 8 deletions lib/hooks/use_media_kit_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,34 @@ MediaKitPlayer useMediaKitPlayer(BuildContext context) {
}
}

useEffect(() {
if (currentFile == null || playQueue.isEmpty) {
player.stop();
} else {
final storage = useStorageStore().findById(currentFile.storageId);
final isInitializing = useState(false);

Future<void> init(FileItem file) async {
if (file.uri == '') return;
isInitializing.value = true;

try {
final storage = useStorageStore().findById(file.storageId);
final auth = storage?.getAuth();
logger('Now playing: ${currentFile.uri}, auto play: $autoPlay');
player.open(
await player.open(
Media(
currentFile.uri,
file.uri,
httpHeaders: auth != null ? {'authorization': auth} : {},
),
play: autoPlay,
);
} catch (e) {
logger('Error initializing player: $e');
}

isInitializing.value = false;
}

useEffect(() {
if (currentFile == null || playQueue.isEmpty) {
player.stop();
} else {
init(currentFile);
}
return () {
if (currentFile != null && player.state.duration != Duration.zero) {
Expand Down Expand Up @@ -252,6 +266,11 @@ MediaKitPlayer useMediaKitPlayer(BuildContext context) {

Future<void> play() async {
await useAppStore().updateAutoPlay(true);
if (duration == Duration.zero &&
currentFile != null &&
!isInitializing.value) {
await init(currentFile);
}
await player.play();
}

Expand Down Expand Up @@ -285,6 +304,7 @@ MediaKitPlayer useMediaKitPlayer(BuildContext context) {
externalSubtitles: externalSubtitles ?? [],
audio: audio,
audios: audios,
isInitializing: isInitializing.value,
isPlaying: playing,
position: duration == Duration.zero ? Duration.zero : position.value,
duration: duration,
Expand Down
4 changes: 4 additions & 0 deletions lib/models/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:media_kit_video/media_kit_video.dart' as media_kit_video;
import 'package:video_player/video_player.dart';

class MediaPlayer {
final bool isInitializing;
final bool isPlaying;
final List<Subtitle> externalSubtitles;
final Duration position;
Expand All @@ -26,6 +27,7 @@ class MediaPlayer {
final Future<void> Function(Duration) seekTo;

MediaPlayer({
required this.isInitializing,
required this.isPlaying,
required this.externalSubtitles,
required this.position,
Expand Down Expand Up @@ -64,6 +66,7 @@ class MediaKitPlayer extends MediaPlayer {
required super.externalSubtitles,
required this.audio,
required this.audios,
required super.isInitializing,
required super.isPlaying,
required super.position,
required super.duration,
Expand Down Expand Up @@ -91,6 +94,7 @@ class FvpPlayer extends MediaPlayer {

FvpPlayer({
required this.controller,
required super.isInitializing,
required super.isPlaying,
required this.externalSubtitle,
required super.externalSubtitles,
Expand Down
48 changes: 31 additions & 17 deletions lib/pages/player/control_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,37 @@ class ControlBar extends HookWidget {
),
),
DarkTheme(
child: IconButton(
tooltip:
'${player.isPlaying == true ? t.pause : t.play} ( Space )',
icon: Icon(
player.isPlaying == true
? Icons.pause_rounded
: Icons.play_arrow_rounded,
size: 36,
),
onPressed: () {
showControl();
if (player.isPlaying == true) {
player.pause();
} else {
player.play();
}
},
child: Stack(
alignment: Alignment.center,
children: [
IconButton(
tooltip:
'${player.isPlaying == true ? t.pause : t.play} ( Space )',
icon: Icon(
player.isPlaying == true
? Icons.pause_rounded
: Icons.play_arrow_rounded,
size: 36,
),
onPressed: () {
showControl();
if (player.isPlaying == true) {
player.pause();
} else {
player.play();
}
},
),
if (player.isInitializing)
SizedBox(
width: 36,
height: 36,
child: CircularProgressIndicator(
strokeWidth: 4,
color: Theme.of(context).colorScheme.surface,
),
),
],
),
),
if (playQueueLength > 1)
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: iris
description: "A lightweight video player"
publish_to: 'none'
version: 1.3.2+3
version: 1.3.3+3

environment:
sdk: ^3.5.4
Expand Down

0 comments on commit d89f8ac

Please sign in to comment.