feat(player): expand MediaSession surface for Wear + lock-screen + Auto
External media controllers (Android Wear, Auto, Bluetooth dashes, lock-screen widgets) consume the audio_service MediaSession and silently no-op on any action that isn't in the handler's systemActions set. Several handler methods were already implemented but never advertised, plus stop() defaulted to a no-op — which matched user reports of "media controller on the watch sometimes works but doesn't play nice with Minstrel." This patch lines the advertised surface up with what the handler actually implements + wires a native heart-rating button. **Expanded controls + systemActions:** - Added MediaControl.stop to the expanded controls list. - systemActions now also enumerates stop, skipToQueueItem (override shipped in v2026.05.13.1), setShuffleMode, setRepeatMode, and setRating. Without these in the set, Android 13+ drops the corresponding callbacks from external surfaces. **stop() override:** halts _player and dismisses the foreground notification via super.stop(). Default just flipped processingState to idle without releasing the audio session — external surfaces treated that as "paused forever". **setRating wiring (native heart-button protocol):** new LikeBridge adapter passes through configure() carrying toggleTrackLike + isTrackLiked closures over LikesController and likedIdsProvider. - setRating override flips the like through the bridge and re-emits mediaItem so the watch's heart icon updates immediately. - _toMediaItem populates MediaItem.rating on every track change so the right filled/outlined heart shows on track-A → track-B. - PlayerActions ref.listen on likedIdsProvider calls refreshCurrentRating so toggling a like from TrackRow / kebab / another device (SSE) also keeps the watch icon in sync. **artUri seed on first broadcast:** AlbumCoverCache.peekCached returns the file path synchronously when the cover is already on disk. _toMediaItem uses this so warm-cache tracks broadcast with artUri populated from the first frame — external controllers see the cover immediately instead of waiting for the later async _loadArtForCurrentItem path. Cold-cache tracks fall through to that path unchanged.
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../api/endpoints/likes.dart' show LikeKind;
|
||||
import '../api/endpoints/radio.dart';
|
||||
import '../auth/auth_provider.dart';
|
||||
import '../cache/audio_cache_manager.dart';
|
||||
import '../library/library_providers.dart' show dioProvider;
|
||||
import '../likes/likes_provider.dart';
|
||||
import '../models/track.dart';
|
||||
import 'album_cover_cache.dart';
|
||||
import 'audio_handler.dart';
|
||||
@@ -49,7 +51,18 @@ final positionProvider = StreamProvider<Duration>(
|
||||
);
|
||||
|
||||
class PlayerActions {
|
||||
PlayerActions(this._ref);
|
||||
PlayerActions(this._ref) {
|
||||
// Keep MediaItem.rating in sync with the drift-backed likedIds
|
||||
// cache so external media surfaces (Wear's heart button, lock-
|
||||
// screen favorite, Auto's like icon) reflect the current state
|
||||
// even when the user toggles a like from somewhere other than
|
||||
// the watch itself — e.g. tapping the heart on a TrackRow, the
|
||||
// kebab menu, or another logged-in device propagating via SSE.
|
||||
_ref.listen<AsyncValue<LikedIds>>(likedIdsProvider, (_, next) {
|
||||
if (next.value == null) return;
|
||||
_ref.read(audioHandlerProvider).refreshCurrentRating();
|
||||
});
|
||||
}
|
||||
final Ref _ref;
|
||||
|
||||
Future<void> playTracks(List<TrackRef> tracks, {int initialIndex = 0}) async {
|
||||
@@ -63,11 +76,28 @@ class PlayerActions {
|
||||
token: token,
|
||||
coverCache: cache,
|
||||
audioCacheManager: audioCache,
|
||||
likeBridge: _buildLikeBridge(),
|
||||
);
|
||||
await h.setQueueFromTracks(tracks, initialIndex: initialIndex);
|
||||
await h.play();
|
||||
}
|
||||
|
||||
/// Builds the adapter the audio handler uses to read + flip the
|
||||
/// current track's like state in response to external media-
|
||||
/// controller events (Wear's heart button, lock-screen favorite).
|
||||
/// Constructed here because the audio handler is initialized in
|
||||
/// main.dart before the ProviderScope exists; passing the bridge
|
||||
/// in via configure() keeps the handler Riverpod-agnostic while
|
||||
/// still letting it call into LikesController + likedIdsProvider.
|
||||
LikeBridge _buildLikeBridge() {
|
||||
return LikeBridge(
|
||||
toggleTrackLike: (id) =>
|
||||
_ref.read(likesControllerProvider).toggle(LikeKind.track, id),
|
||||
isTrackLiked: (id) =>
|
||||
_ref.read(likedIdsProvider).value?.has(LikeKind.track, id) ?? false,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> playNext(TrackRef track) async {
|
||||
final h = _ref.read(audioHandlerProvider);
|
||||
await h.playNext(track);
|
||||
|
||||
Reference in New Issue
Block a user