2024-08-26 17:49:05 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:rhythm_box/providers/audio_player.dart';
|
|
|
|
import 'package:rhythm_box/services/audio_services/image.dart';
|
|
|
|
import 'package:smtc_windows/smtc_windows.dart';
|
|
|
|
import 'package:spotify/spotify.dart';
|
|
|
|
import 'package:rhythm_box/services/audio_player/audio_player.dart';
|
|
|
|
import 'package:rhythm_box/services/audio_player/playback_state.dart';
|
|
|
|
import 'package:rhythm_box/services/artist.dart';
|
|
|
|
|
|
|
|
class WindowsAudioService {
|
|
|
|
final SMTCWindows smtc;
|
|
|
|
|
|
|
|
final subscriptions = <StreamSubscription>[];
|
|
|
|
|
|
|
|
WindowsAudioService() : smtc = SMTCWindows(enabled: false) {
|
2024-08-29 07:02:49 +00:00
|
|
|
smtc.setPlaybackStatus(PlaybackStatus.stopped);
|
2024-08-26 17:49:05 +00:00
|
|
|
final buttonStream = smtc.buttonPressStream.listen((event) {
|
|
|
|
switch (event) {
|
|
|
|
case PressedButton.play:
|
|
|
|
audioPlayer.resume();
|
|
|
|
break;
|
|
|
|
case PressedButton.pause:
|
|
|
|
audioPlayer.pause();
|
|
|
|
break;
|
|
|
|
case PressedButton.next:
|
|
|
|
audioPlayer.skipToNext();
|
|
|
|
break;
|
|
|
|
case PressedButton.previous:
|
|
|
|
audioPlayer.skipToPrevious();
|
|
|
|
break;
|
|
|
|
case PressedButton.stop:
|
|
|
|
Get.find<AudioPlayerProvider>().stop();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
final playerStateStream =
|
|
|
|
audioPlayer.playerStateStream.listen((state) async {
|
|
|
|
switch (state) {
|
|
|
|
case AudioPlaybackState.playing:
|
2024-08-29 07:02:49 +00:00
|
|
|
await smtc.setPlaybackStatus(PlaybackStatus.playing);
|
2024-08-26 17:49:05 +00:00
|
|
|
break;
|
|
|
|
case AudioPlaybackState.paused:
|
2024-08-29 07:02:49 +00:00
|
|
|
await smtc.setPlaybackStatus(PlaybackStatus.paused);
|
2024-08-26 17:49:05 +00:00
|
|
|
break;
|
|
|
|
case AudioPlaybackState.stopped:
|
2024-08-29 07:02:49 +00:00
|
|
|
await smtc.setPlaybackStatus(PlaybackStatus.stopped);
|
2024-08-26 17:49:05 +00:00
|
|
|
break;
|
|
|
|
case AudioPlaybackState.completed:
|
2024-08-29 07:02:49 +00:00
|
|
|
await smtc.setPlaybackStatus(PlaybackStatus.changing);
|
2024-08-26 17:49:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
final positionStream = audioPlayer.positionStream.listen((pos) async {
|
|
|
|
await smtc.setPosition(pos);
|
|
|
|
});
|
|
|
|
|
|
|
|
final durationStream = audioPlayer.durationStream.listen((duration) async {
|
|
|
|
await smtc.setEndTime(duration);
|
|
|
|
});
|
|
|
|
|
|
|
|
subscriptions.addAll([
|
|
|
|
buttonStream,
|
|
|
|
playerStateStream,
|
|
|
|
positionStream,
|
|
|
|
durationStream,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> addTrack(Track track) async {
|
|
|
|
if (!smtc.enabled) {
|
|
|
|
await smtc.enableSmtc();
|
|
|
|
}
|
|
|
|
await smtc.updateMetadata(
|
|
|
|
MusicMetadata(
|
|
|
|
title: track.name!,
|
2024-08-27 06:48:31 +00:00
|
|
|
albumArtist: track.artists?.firstOrNull?.name ?? 'Unknown',
|
|
|
|
artist: track.artists?.asString() ?? 'Unknown',
|
|
|
|
album: track.album?.name ?? 'Unknown',
|
2024-08-27 06:35:16 +00:00
|
|
|
thumbnail: (track.album?.images).asUrlString(),
|
2024-08-26 17:49:05 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
smtc.disableSmtc();
|
|
|
|
smtc.dispose();
|
|
|
|
for (var element in subscriptions) {
|
|
|
|
element.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|