Skip to content

Commit d3fe127

Browse files
committed
update position for mediasession, reset repeat and shuffel when playing new playlist
1 parent 1b0d3bf commit d3fe127

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

MyMusicClientSveltePwa/src/lib/pages/Playlist.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
2020
intervalId = setInterval(() => {
2121
songs.set(getCachedPlaylistSongs(playlistId));
22-
setPlaylists(playlistId);
2322
}, updateIntervalTimeOut);
2423
});
2524

MyMusicClientSveltePwa/src/lib/scripts/mediasessionService.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getImageUrl } from "./api.js"
22
import { nextSong, previousSong } from "./playbackService.js";
33

4+
const DEFAULT_PLAYBACK_RATE = 1.0;
5+
46
export function initializeMediaSessionService() {
57
if ("mediaSession" in navigator) {
68
navigator.mediaSession.setActionHandler("previoustrack", () => {
@@ -20,6 +22,16 @@ export function updateMediaSessionPlaybackState(isPlaying) {
2022
}
2123
}
2224

25+
export function updatePositionState(currentTime, duration) {
26+
if ("mediaSession" in navigator) {
27+
navigator.mediaSession.setPositionState({
28+
duration: duration,
29+
playbackRate: DEFAULT_PLAYBACK_RATE,
30+
position: currentTime,
31+
});
32+
}
33+
}
34+
2335
export function updateMediaSessionMetadata(song, playlist) {
2436
if ("mediaSession" in navigator) {
2537
navigator.mediaSession.playbackState = "paused";

MyMusicClientSveltePwa/src/lib/scripts/playbackService.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getCachedPlaylistSongs, getCachedPlaylists,
88
getCurrentSongIndex, setCurrentSongIndex, setCurrentSongTime,
99
getCurrentSongTime } from "./storageService";
1010
import { shuffleArray } from "./util";
11-
import { updateMediaSessionMetadata, updateMediaSessionPlaybackState } from "./mediasessionService";
11+
import { updateMediaSessionMetadata, updateMediaSessionPlaybackState, updatePositionState } from "./mediasessionService";
1212

1313
export let currentSong = writable({id: -999, title: "", artist: "", album: "", source_id: ""});
1414
export let isPlaying = writable(false);
@@ -52,7 +52,6 @@ export function initializePlaybackService() {
5252
playOrPauseSong(playlistSongs[songIndex].id);
5353
}
5454

55-
5655
audioElement.addEventListener("play", () => {
5756
isPlaying.set(true);
5857
updateMediaSessionPlaybackState(true);
@@ -65,7 +64,7 @@ export function initializePlaybackService() {
6564
updateMediaSessionPlaybackState(false);
6665
if (get(isLoopingEnabled)) {
6766
audioElement.currentTime = 0;
68-
audioElement.play();
67+
audioElement.load();
6968
} else {
7069
nextSong();
7170
}
@@ -90,7 +89,7 @@ export function initializePlaybackService() {
9089
}
9190

9291
setCurrentSongTime(audioElement.currentTime);
93-
92+
updatePositionState(audioElement.currentTime, audioElement.duration);
9493
playPercentage.set(percentage);
9594
});
9695

@@ -132,6 +131,7 @@ export function playOrPauseSong(songId) {
132131
else if (get(isPlaying)) {
133132
audioElement.pause();
134133
}else {
134+
// data is already loaded, just play
135135
audioElement.play();
136136
}
137137
}
@@ -174,6 +174,9 @@ export function setPlaylists(playlistId) {
174174
// and update playlistSongs accordingly if shuffle is enabled
175175
return; // Already set to this playlist
176176
}
177+
isLoopingEnabled.set(false);
178+
isShuffledEnabled.set(false);
179+
setPlaybackState(false, false);
177180
currentPlaylistId = playlistId;
178181
originalPlaylistSongs = getCachedPlaylistSongs(playlistId);
179182
playlistSongs = originalPlaylistSongs;

0 commit comments

Comments
 (0)