forked from Nexus/nexus
rip out media_kit, use better_player and audioplayers
This also let us rip out cocoapods stuff. Needs testing on Windows, MacOS, and ideally, iOS.
This commit is contained in:
parent
161d39322c
commit
37a2c150f3
1 changed files with 375 additions and 604 deletions
|
|
@ -1,9 +1,9 @@
|
|||
import "dart:async";
|
||||
|
||||
import "package:audioplayers/audioplayers.dart" as audioplayers;
|
||||
import "package:material_ui/material_ui.dart";
|
||||
import "package:flutter_hooks/flutter_hooks.dart";
|
||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:media_kit/media_kit.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/helpers/extensions/save_attachment.dart";
|
||||
import "package:nexus/models/info/audio.dart";
|
||||
|
|
@ -18,9 +18,7 @@ class const AudioPlayer(
|
|||
}) extends HookConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final player = useMemoized(
|
||||
() => Player(configuration: .new(bufferSize: 128 * 1024 * 1024)),
|
||||
);
|
||||
final player = useMemoized(audioplayers.AudioPlayer.new);
|
||||
final loaded = useState(false);
|
||||
|
||||
useEffect(() {
|
||||
|
|
@ -29,19 +27,39 @@ class const AudioPlayer(
|
|||
.read(ClientController.provider.notifier)
|
||||
.downloadMedia(.new(mxc: uri, encrypted: encrypted));
|
||||
if (!context.mounted) return;
|
||||
await player.open(Media(audio.path), play: false);
|
||||
// Keeps the audio loaded after it finishes, so it can be replayed
|
||||
await player.setReleaseMode(.stop);
|
||||
await player.setSource(audioplayers.DeviceFileSource(audio.path));
|
||||
if (context.mounted) loaded.value = true;
|
||||
});
|
||||
|
||||
return player.dispose;
|
||||
}, []);
|
||||
|
||||
final state = useStream(
|
||||
player.onPlayerStateChanged,
|
||||
initialData: player.state,
|
||||
).requireData;
|
||||
final position = useStream(
|
||||
player.onPositionChanged,
|
||||
initialData: Duration.zero,
|
||||
).requireData;
|
||||
final duration = useStream(
|
||||
player.onDurationChanged,
|
||||
initialData: Duration.zero,
|
||||
).requireData;
|
||||
final playing = state == .playing;
|
||||
|
||||
return Card(
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHigh,
|
||||
child: Padding(
|
||||
padding: .symmetric(horizontal: 8),
|
||||
child: PlayerControls(
|
||||
player,
|
||||
playing: playing,
|
||||
position: position,
|
||||
duration: duration,
|
||||
onPlayPause: playing ? player.pause : player.resume,
|
||||
onSeek: player.seek,
|
||||
loading: !loaded.value,
|
||||
size: info?.size,
|
||||
actions: .new([
|
||||
|
|
|
|||
Loading…
Reference in a new issue