From 7c0998e549b3965457e0b0eb8c5d84e19a6744a2 Mon Sep 17 00:00:00 2001 From: WardPearce Date: Sun, 22 Sep 2024 01:38:37 +1200 Subject: [PATCH] Improve android bg play logic --- materialious/src/lib/Player.svelte | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/materialious/src/lib/Player.svelte b/materialious/src/lib/Player.svelte index 416c0f5a..0cadded3 100644 --- a/materialious/src/lib/Player.svelte +++ b/materialious/src/lib/Player.svelte @@ -350,6 +350,8 @@ const audioId = { audioId: data.video.videoId }; + let isPlayingInBackground = false; + await AudioPlayer.create({ ...audioId, audioSource: proxyVideoUrl(highestBitrateAudio.url), @@ -360,6 +362,10 @@ }); AudioPlayer.onAppGainsFocus(audioId, async () => { + if (!isPlayingInBackground) return; + + isPlayingInBackground = false; + await AudioPlayer.pause(audioId); const audioPlayerTime = await AudioPlayer.getCurrentTime(audioId); @@ -369,13 +375,14 @@ }); AudioPlayer.onAppLosesFocus(audioId, async () => { - if (!player.paused) { - await AudioPlayer.play(audioId); - await AudioPlayer.seek({ - ...audioId, - timeInSeconds: Math.round(player.currentTime) - }); - } + if (player.paused) return; + + isPlayingInBackground = true; + await AudioPlayer.play(audioId); + await AudioPlayer.seek({ + ...audioId, + timeInSeconds: Math.round(player.currentTime) + }); }); await AudioPlayer.initialize(audioId);