ocarina2/lib/api/audio/audioplayer_service.dart

56 lines
1.6 KiB
Dart

import 'package:just_audio/just_audio.dart';
import 'package:just_audio_background/just_audio_background.dart';
import 'package:ocarina/api/subsonic/song.dart';
import 'package:ocarina/main.dart';
/// Service used to control the audio player
class AudioPlayerService {
/// Service used to control the audio player
factory AudioPlayerService() {
return _audioPlayerService;
}
AudioPlayerService._internal();
static final AudioPlayerService _audioPlayerService =
AudioPlayerService._internal();
/// The [AudioPlayer] instance
final player = AudioPlayer();
/// Currently playing song
///
/// Null if no song is loaded
Song? song;
/// Plays the passed [Song] as a file
Future<void> playFile({Song? song}) async {
final doCache = sp.getBool("doCache") ?? true;
if (song == null && this.song == null) return;
song ??= this.song;
await player.setAudioSource(
doCache
? LockCachingAudioSource(
Uri.parse(song!.streamUrl),
tag: MediaItem(
id: song.id,
title: song.title,
album: song.albumName,
artist: song.artistName,
artUri: Uri.parse(song.coverArtUrl),
),
)
: AudioSource.uri(
Uri.parse(song!.streamUrl),
tag: MediaItem(
id: song.id,
title: song.title,
album: song.albumName,
artist: song.artistName,
artUri: Uri.parse(song.coverArtUrl),
),
),
);
await player.seek(Duration.zero);
await player.play();
}
}