Skip to content

Commit e3d1a35

Browse files
authored
fix: bring back smtc windows, audio service win does not work yet sadly (#1363)
1 parent d02c572 commit e3d1a35

File tree

6 files changed

+109
-16
lines changed

6 files changed

+109
-16
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import 'dart:async';
2+
3+
import 'package:smtc_windows/smtc_windows.dart';
4+
import 'package:watch_it/watch_it.dart';
5+
6+
import '../app_config.dart';
7+
import '../common/data/audio_type.dart';
8+
import 'player_service.dart';
9+
10+
StreamSubscription<PressedButton>? smtcSubscription;
11+
12+
Future<SMTCWindows> registerSMTCWindows() async {
13+
final playerService = di<PlayerService>();
14+
15+
await SMTCWindows.initialize();
16+
final smtc = SMTCWindows(
17+
enabled: true,
18+
config: const SMTCConfig(
19+
fastForwardEnabled: false,
20+
nextEnabled: true,
21+
pauseEnabled: true,
22+
playEnabled: true,
23+
rewindEnabled: false,
24+
prevEnabled: true,
25+
stopEnabled: false,
26+
),
27+
);
28+
29+
smtcSubscription = smtc.buttonPressStream.listen((event) {
30+
switch (event) {
31+
case PressedButton.play:
32+
playerService.playOrPause().then(
33+
(_) => smtc.setPlaybackStatus(
34+
playerService.isPlaying
35+
? PlaybackStatus.playing
36+
: PlaybackStatus.paused,
37+
),
38+
);
39+
case PressedButton.pause:
40+
playerService.playOrPause().then(
41+
(_) => smtc.setPlaybackStatus(
42+
playerService.isPlaying
43+
? PlaybackStatus.playing
44+
: PlaybackStatus.paused,
45+
),
46+
);
47+
case PressedButton.next:
48+
playerService.playNext();
49+
case PressedButton.previous:
50+
playerService.playPrevious();
51+
default:
52+
break;
53+
}
54+
});
55+
56+
playerService.registerMediaControlsCallBacks(
57+
onSetMetaData: ({required artUri, required audio}) => smtc.updateMetadata(
58+
MusicMetadata(
59+
title: audio.title ?? AppConfig.appTitle,
60+
album: audio.album,
61+
albumArtist: audio.artist,
62+
artist: audio.artist ?? '',
63+
thumbnail: audio.audioType == AudioType.local
64+
? AppConfig.fallbackThumbnailUrl
65+
: artUri == null
66+
? null
67+
: '$artUri',
68+
),
69+
),
70+
onIsPlaying:
71+
({required audioType, required isPlaying, required queueNotEmpty}) =>
72+
smtc.setPlaybackStatus(
73+
isPlaying ? PlaybackStatus.playing : PlaybackStatus.paused,
74+
),
75+
onSetPosition: (position) => smtc.setPosition(position ?? Duration.zero),
76+
onSetDuration: null,
77+
);
78+
79+
return smtc;
80+
}

lib/register.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:media_kit/media_kit.dart';
77
import 'package:media_kit_video/media_kit_video.dart';
88
import 'package:package_info_plus/package_info_plus.dart';
99
import 'package:shared_preferences/shared_preferences.dart';
10+
import 'package:smtc_windows/smtc_windows.dart';
1011
import 'package:watch_it/watch_it.dart';
1112
import 'package:window_manager/window_manager.dart';
1213

@@ -31,6 +32,7 @@ import 'persistence_utils.dart';
3132
import 'player/player_model.dart';
3233
import 'player/player_service.dart';
3334
import 'player/register_audio_service_handler.dart';
35+
import 'player/register_smtc_windows.dart';
3436
import 'podcasts/download_model.dart';
3537
import 'podcasts/podcast_model.dart';
3638
import 'podcasts/podcast_service.dart';
@@ -139,12 +141,26 @@ void registerDependencies() {
139141
},
140142
dependsOn: [ExposeService],
141143
dispose: (s) async => s.dispose(),
142-
)
143-
..registerSingletonAsync<AudioServiceHandler>(
144+
);
145+
146+
if (isWindows) {
147+
di.registerSingletonAsync<SMTCWindows>(
148+
registerSMTCWindows,
149+
dependsOn: [PlayerService],
150+
dispose: (s) async {
151+
smtcSubscription?.cancel();
152+
await s.dispose();
153+
},
154+
);
155+
} else {
156+
di.registerSingletonAsync<AudioServiceHandler>(
144157
registerAudioServiceHandler,
145158
dependsOn: [PlayerService],
146159
dispose: (s) async => s.stop(),
147-
)
160+
);
161+
}
162+
163+
di
148164
..registerSingleton<ExternalPathService>(const ExternalPathService())
149165
..registerSingletonAsync<LibraryService>(
150166
() async {

pubspec.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,6 @@ packages:
106106
url: "https://pub.dev"
107107
source: hosted
108108
version: "0.1.4"
109-
audio_service_win:
110-
dependency: "direct main"
111-
description:
112-
name: audio_service_win
113-
sha256: "8e8cd6a576901e4386dc22a8ca6d959317bf41c74b2bc4c6c5466826d0e529e8"
114-
url: "https://pub.dev"
115-
source: hosted
116-
version: "0.0.2"
117109
audio_session:
118110
dependency: transitive
119111
description:
@@ -1439,6 +1431,14 @@ packages:
14391431
description: flutter
14401432
source: sdk
14411433
version: "0.0.0"
1434+
smtc_windows:
1435+
dependency: "direct main"
1436+
description:
1437+
name: smtc_windows
1438+
sha256: dee279b0ddf663c4c729a88bca4e57fb4861aa1b3d01e230bdbf1277b8bfe664
1439+
url: "https://pub.dev"
1440+
source: hosted
1441+
version: "1.1.0"
14421442
source_gen:
14431443
dependency: transitive
14441444
description:

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ dependencies:
1414
git:
1515
url: https://github.com/Feichtmeier/audio-service-mpris
1616
ref: ad6f743b73e320670449026fa1852d9978438bb9
17-
audio_service_win: ^0.0.2
1817
basic_utils: ^5.7.0
1918
blur: ^4.0.2
2019
cached_network_image: ^3.4.1
@@ -77,6 +76,7 @@ dependencies:
7776
scroll_to_index: ^3.0.1
7877
shared_preferences: ^2.3.1
7978
shimmer: ^3.0.0
79+
smtc_windows: ^1.1.0
8080
super_drag_and_drop: ^0.9.1
8181
synchronized: ^3.3.1
8282
system_theme: ^3.0.0

windows/flutter/generated_plugin_registrant.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include "generated_plugin_registrant.h"
88

9-
#include <audio_service_win/audio_service_win_plugin_c_api.h>
109
#include <connectivity_plus/connectivity_plus_windows_plugin.h>
1110
#include <file_selector_windows/file_selector_windows.h>
1211
#include <irondash_engine_context/irondash_engine_context_plugin_c_api.h>
@@ -22,8 +21,6 @@
2221
#include <window_manager/window_manager_plugin.h>
2322

2423
void RegisterPlugins(flutter::PluginRegistry* registry) {
25-
AudioServiceWinPluginCApiRegisterWithRegistrar(
26-
registry->GetRegistrarForPlugin("AudioServiceWinPluginCApi"));
2724
ConnectivityPlusWindowsPluginRegisterWithRegistrar(
2825
registry->GetRegistrarForPlugin("ConnectivityPlusWindowsPlugin"));
2926
FileSelectorWindowsRegisterWithRegistrar(

windows/flutter/generated_plugins.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#
44

55
list(APPEND FLUTTER_PLUGIN_LIST
6-
audio_service_win
76
connectivity_plus
87
file_selector_windows
98
irondash_engine_context
@@ -21,6 +20,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
2120

2221
list(APPEND FLUTTER_FFI_PLUGIN_LIST
2322
flutter_discord_rpc
23+
smtc_windows
2424
)
2525

2626
set(PLUGIN_BUNDLED_LIBRARIES)

0 commit comments

Comments
 (0)