From 65f4ca6356b4d3ee20fb378a7ad8b99544e44da4 Mon Sep 17 00:00:00 2001 From: Jonas Bark Date: Fri, 5 Dec 2025 17:57:11 +0100 Subject: [PATCH] media key detection Windows --- .vscode/launch.json | 3 +- lib/bluetooth/connection.dart | 62 +++++++++++++++++++++++-- pubspec.lock | 24 ++++++++++ pubspec.yaml | 1 + windows/flutter/generated_plugins.cmake | 1 + 5 files changed, 87 insertions(+), 4 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index a47999e..29dafdf 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,8 @@ { "name": "swiftcontrol", "request": "launch", - "type": "dart" + "type": "dart", + "program": "lib/main.dart" }, { "name": "swiftcontrol (profile mode)", diff --git a/lib/bluetooth/connection.dart b/lib/bluetooth/connection.dart index 10d814b..bc02008 100644 --- a/lib/bluetooth/connection.dart +++ b/lib/bluetooth/connection.dart @@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:gamepads/gamepads.dart'; import 'package:media_key_detector/media_key_detector.dart'; +import 'package:smtc_windows/smtc_windows.dart'; import 'package:swift_control/bluetooth/devices/bluetooth_device.dart'; import 'package:swift_control/bluetooth/devices/gamepad/gamepad_device.dart'; import 'package:swift_control/bluetooth/devices/hid/hid_device.dart'; @@ -57,13 +58,68 @@ class Connection { lastLogEntries = lastLogEntries.takeLast(kIsWeb ? 1000 : 60).toList(); }); - isMediaKeyDetectionEnabled.addListener(() { + isMediaKeyDetectionEnabled.addListener(() async { if (!isMediaKeyDetectionEnabled.value) { + mediaKeyDetector.setIsPlaying(isPlaying: false); mediaKeyDetector.removeListener(_onMediaKeyDetectedListener); } else { - mediaKeyDetector.addListener(_onMediaKeyDetectedListener); - mediaKeyDetector.setIsPlaying(isPlaying: true); + if (Platform.isWindows) { + await SMTCWindows.initialize(); + +final smtc = SMTCWindows( + metadata: const MusicMetadata( + title: 'Title', + album: 'Album', + albumArtist: 'Album Artist', + artist: 'Artist', + thumbnail: + 'https://media.glamour.com/photos/5f4c44e20c71c58fc210d35f/master/w_2560%2Cc_limit/mgid_ao_image_mtv.jpg', + ), + // Timeline info for the OS media player + timeline: const PlaybackTimeline( + startTimeMs: 0, + endTimeMs: 1000, + positionMs: 0, + minSeekTimeMs: 0, + maxSeekTimeMs: 1000, + ), + // Which buttons to show in the OS media player + config: const SMTCConfig( + fastForwardEnabled: true, + nextEnabled: true, + pauseEnabled: true, + playEnabled: true, + rewindEnabled: true, + prevEnabled: true, + stopEnabled: true, + ), + ); + smtc.buttonPressStream.listen((event) { + print('Event: $event'); + switch (event) { + case PressedButton.play: + // Update playback status + break; + case PressedButton.pause: + break; + case PressedButton.next: + print('Next'); + break; + case PressedButton.previous: + print('Previous'); + break; + case PressedButton.stop: + smtc.disableSmtc(); + break; + default: + break; + } + }); + } else { + mediaKeyDetector.addListener(_onMediaKeyDetectedListener); + mediaKeyDetector.setIsPlaying(isPlaying: true); + } } }); diff --git a/pubspec.lock b/pubspec.lock index 1375c1a..ebd44e7 100755 --- a/pubspec.lock +++ b/pubspec.lock @@ -136,6 +136,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.2" + build_cli_annotations: + dependency: transitive + description: + name: build_cli_annotations + sha256: e563c2e01de8974566a1998410d3f6f03521788160a02503b0b1f1a46c7b3d95 + url: "https://pub.dev" + source: hosted + version: "2.1.1" characters: dependency: transitive description: @@ -416,6 +424,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.30" + flutter_rust_bridge: + dependency: transitive + description: + name: flutter_rust_bridge + sha256: "37ef40bc6f863652e865f0b2563ea07f0d3c58d8efad803cc01933a4b2ee067e" + url: "https://pub.dev" + source: hosted + version: "2.11.1" flutter_test: dependency: "direct dev" description: flutter @@ -1289,6 +1305,14 @@ packages: description: flutter source: sdk version: "0.0.0" + smtc_windows: + dependency: "direct main" + description: + name: smtc_windows + sha256: dee279b0ddf663c4c729a88bca4e57fb4861aa1b3d01e230bdbf1277b8bfe664 + url: "https://pub.dev" + source: hosted + version: "1.1.0" source_span: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 5437b66..f95e93d 100755 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -20,6 +20,7 @@ dependencies: gamepads: ^0.1.8+2 path_provider: ^2.1.5 + smtc_windows: ^1.1.0 intl: any version: ^3.0.0 bluetooth_low_energy: ^6.1.0 diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index bb38b17..0c0d756 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -18,6 +18,7 @@ list(APPEND FLUTTER_PLUGIN_LIST list(APPEND FLUTTER_FFI_PLUGIN_LIST flutter_local_notifications_windows + smtc_windows ) set(PLUGIN_BUNDLED_LIBRARIES)