diff --git a/lib/services/audio_player/audio_player_impl.dart b/lib/services/audio_player/audio_player_impl.dart index 82c8c906..028c9cf1 100644 --- a/lib/services/audio_player/audio_player_impl.dart +++ b/lib/services/audio_player/audio_player_impl.dart @@ -1,45 +1,28 @@ -part of 'audio_player.dart'; - final audioPlayer = SpotubeAudioPlayer(); -class SpotubeAudioPlayer extends AudioPlayerInterface - with SpotubeAudioPlayersStreams { - Future pause() async { - await _mkPlayer.pause(); - } +class SpotubeAudioPlayer extends AudioPlayerInterface with SpotubeAudioPlayersStreams { + // Playback control methods + Future pause() async => await player.pause(); - Future resume() async { - await _mkPlayer.play(); - } + Future resume() async => await player.play(); - Future stop() async { - await _mkPlayer.stop(); - } + Future stop() async => await player.stop(); - Future seek(Duration position) async { - await _mkPlayer.seek(position); - } + Future seek(Duration position) async => await player.seek(position); - /// Volume is between 0 and 1 + /// Set volume between 0 and 1 Future setVolume(double volume) async { assert(volume >= 0 && volume <= 1); - await _mkPlayer.setVolume(volume * 100); + await player.setVolume(volume * 100); } - Future setSpeed(double speed) async { - await _mkPlayer.setRate(speed); - } + Future setSpeed(double speed) async => await player.setRate(speed); - Future setAudioDevice(mk.AudioDevice device) async { - await _mkPlayer.setAudioDevice(device); - } + Future setAudioDevice(mk.AudioDevice device) async => await player.setAudioDevice(device); - Future dispose() async { - await _mkPlayer.dispose(); - } - - // Playlist related + Future dispose() async => await player.dispose(); + // Playlist control methods Future openPlaylist( List tracks, { bool autoPlay = true, @@ -47,88 +30,59 @@ class SpotubeAudioPlayer extends AudioPlayerInterface }) async { assert(tracks.isNotEmpty); assert(initialIndex <= tracks.length - 1); - await _mkPlayer.open( + + await player.open( mk.Playlist(tracks, index: initialIndex), play: autoPlay, ); } - List get sources { - return _mkPlayer.state.playlist.medias.map((e) => e.uri).toList(); - } + // Helper methods for playlist sources + List get sources => player.state.playlist.medias.map((e) => e.uri).toList(); String? get currentSource { - if (_mkPlayer.state.playlist.index == -1) return null; - return _mkPlayer.state.playlist.medias - .elementAtOrNull(_mkPlayer.state.playlist.index) - ?.uri; + final index = player.state.playlist.index; + if (index == -1) return null; + return player.state.playlist.medias.elementAtOrNull(index)?.uri; } String? get nextSource { - if (loopMode == PlaylistMode.loop && - _mkPlayer.state.playlist.index == - _mkPlayer.state.playlist.medias.length - 1) { - return sources.first; - } + final isLastTrack = player.state.playlist.index == player.state.playlist.medias.length - 1; + if (loopMode == PlaylistMode.loop && isLastTrack) return sources.first; - return _mkPlayer.state.playlist.medias - .elementAtOrNull(_mkPlayer.state.playlist.index + 1) - ?.uri; + return player.state.playlist.medias.elementAtOrNull(player.state.playlist.index + 1)?.uri; } String? get previousSource { - if (loopMode == PlaylistMode.loop && _mkPlayer.state.playlist.index == 0) { - return sources.last; - } + if (loopMode == PlaylistMode.loop && player.state.playlist.index == 0) return sources.last; - return _mkPlayer.state.playlist.medias - .elementAtOrNull(_mkPlayer.state.playlist.index - 1) - ?.uri; + return player.state.playlist.medias.elementAtOrNull(player.state.playlist.index - 1)?.uri; } - int get currentIndex => _mkPlayer.state.playlist.index; + int get currentIndex => player.state.playlist.index; - Future skipToNext() async { - await _mkPlayer.next(); - } + // Playlist navigation methods + Future skipToNext() async => await player.next(); - Future skipToPrevious() async { - await _mkPlayer.previous(); - } + Future skipToPrevious() async => await player.previous(); - Future jumpTo(int index) async { - await _mkPlayer.jump(index); - } + Future jumpTo(int index) async => await player.jump(index); - Future addTrack(mk.Media media) async { - await _mkPlayer.add(media); - } + // Playlist management methods + Future addTrack(mk.Media media) async => await player.add(media); - Future addTrackAt(mk.Media media, int index) async { - await _mkPlayer.insert(index, media); - } + Future addTrackAt(mk.Media media, int index) async => await player.insert(index, media); - Future removeTrack(int index) async { - await _mkPlayer.remove(index); - } + Future removeTrack(int index) async => await player.remove(index); - Future moveTrack(int from, int to) async { - await _mkPlayer.move(from, to); - } + Future moveTrack(int from, int to) async => await player.move(from, to); - Future clearPlaylist() async { - _mkPlayer.stop(); - } + Future clearPlaylist() async => await player.stop(); - Future setShuffle(bool shuffle) async { - await _mkPlayer.setShuffle(shuffle); - } + // Shuffle and loop mode control + Future setShuffle(bool shuffle) async => await player.setShuffle(shuffle); - Future setLoopMode(PlaylistMode loop) async { - await _mkPlayer.setPlaylistMode(loop); - } + Future setLoopMode(PlaylistMode loop) async => await player.setPlaylistMode(loop); - Future setAudioNormalization(bool normalize) async { - await _mkPlayer.setAudioNormalization(normalize); - } + Future setAudioNormalization(bool normalize) async => await player.setAudioNormalization(normalize); }