Skip to content

Commit

Permalink
fix: use audio_service_mpris plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Oct 16, 2023
1 parent ed6ca00 commit e29cc25
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
53 changes: 17 additions & 36 deletions lib/services/audio_services/audio_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,40 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/models/spotube_track.dart';
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
import 'package:spotube/services/audio_player/audio_player.dart';
import 'package:spotube/services/audio_services/linux_audio_service.dart';
import 'package:spotube/services/audio_services/mobile_audio_service.dart';
import 'package:spotube/services/audio_services/windows_audio_service.dart';
import 'package:spotube/utils/type_conversion_utils.dart';

class AudioServices {
final MobileAudioService? mobile;
final WindowsAudioService? smtc;
final LinuxAudioService? mpris;

AudioServices(this.mobile, this.smtc, this.mpris);
AudioServices(this.mobile, this.smtc);

static Future<AudioServices> create(
Ref ref,
ProxyPlaylistNotifier playback,
) async {
final mobile =
DesktopTools.platform.isMobile || DesktopTools.platform.isMacOS
? await AudioService.init(
builder: () => MobileAudioService(playback),
config: const AudioServiceConfig(
androidNotificationChannelId: 'com.krtirtho.Spotube',
androidNotificationChannelName: 'Spotube',
androidNotificationOngoing: true,
),
)
: null;
final mobile = DesktopTools.platform.isMobile ||
DesktopTools.platform.isMacOS ||
DesktopTools.platform.isLinux
? await AudioService.init(
builder: () => MobileAudioService(playback),
config: const AudioServiceConfig(
androidNotificationChannelId: 'com.krtirtho.Spotube',
androidNotificationChannelName: 'Spotube',
androidNotificationOngoing: true,
),
)
: null;
final smtc = DesktopTools.platform.isWindows
? WindowsAudioService(ref, playback)
: null;
final mpris =
DesktopTools.platform.isLinux ? LinuxAudioService(ref, playback) : null;

if (mpris != null) {
playback.addListener((state) {
mpris.player.updateProperties();
});
audioPlayer.playerStateStream.listen((state) {
mpris.player.updateProperties();
});
audioPlayer.positionStream.listen((state) async {
await mpris.player.emitPropertiesChanged(
"org.mpris.MediaPlayer2.Player",
changedProperties: {
"Position": (await mpris.player.getPosition()).returnValues.first,
},
);
});
}

return AudioServices(mobile, smtc, mpris);
return AudioServices(
mobile,
smtc,
);
}

Future<void> addTrack(Track track) async {
Expand Down Expand Up @@ -86,6 +68,5 @@ class AudioServices {

void dispose() {
smtc?.dispose();
mpris?.dispose();
}
}
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.18.12"
audio_service_mpris:
dependency: "direct main"
description:
name: audio_service_mpris
sha256: "31be5de2db0c71b217157afce1974ac6d0ad329bd91deb1f19ad094d29340d8e"
url: "https://pub.dev"
source: hosted
version: "0.1.0"
audio_service_platform_interface:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ dependencies:
path: plugins/window_size
youtube_explode_dart: ^2.0.1
simple_icons: ^7.10.0
audio_service_mpris: ^0.1.0

dev_dependencies:
build_runner: ^2.3.2
Expand Down

0 comments on commit e29cc25

Please sign in to comment.