Can control player from browser

This commit is contained in:
Pierre HUBERT 2022-03-26 11:55:09 +01:00
parent 8987a0cd94
commit c8e44f810a
2 changed files with 22 additions and 2 deletions

View File

@ -81,7 +81,14 @@ class _MusicPlayerState extends State<MusicPlayer> {
} }
})); }));
updateMusicSession(currMusic); updateMusicSession(
entry: currMusic,
onPlay: _play,
onPause: _pause,
onNextTrack: _playNext,
onPreviousTrack: _playPrevious,
onStop: _stop,
);
} }
} }

View File

@ -5,7 +5,14 @@ import 'dart:html';
import 'package:audio_service_web/js/media_session_web.dart'; import 'package:audio_service_web/js/media_session_web.dart';
import 'package:music_web_player/api.dart'; import 'package:music_web_player/api.dart';
void updateMusicSession(MusicEntry entry) { void updateMusicSession({
required MusicEntry entry,
required Function() onPlay,
required Function() onPause,
required Function() onStop,
required Function() onPreviousTrack,
required Function() onNextTrack,
}) {
MediaSession.metadata = MediaMetadata({ MediaSession.metadata = MediaMetadata({
"title": entry.title, "title": entry.title,
"artist": entry.artist, "artist": entry.artist,
@ -17,4 +24,10 @@ void updateMusicSession(MusicEntry entry) {
} }
] ]
}); });
MediaSession.setActionHandler("play", (p0) => onPlay());
MediaSession.setActionHandler("pause", (p0) => onPause());
MediaSession.setActionHandler("stop", (p0) => onStop());
MediaSession.setActionHandler("previoustrack", (p0) => onPreviousTrack());
MediaSession.setActionHandler("nexttrack", (p0) => onNextTrack());
} }