How to get rid of "media playing" notification shown by Chrome on Android?
Asked Answered
S

1

8

My HTML5 game has some background music that uses Howler.js in "html5" mode, which apparently triggers Chrome for Android's media playback notifications. This means a notification appears while the user has my game open in any tab:

The game is a good citizen and pauses the music while the tab is not in focus, so there is no need for this notification. It's even actively confusing, because the user can pause and resume the game's background music without being in the game. But I can't find a way to get rid of the notification.

I tried calling stop() instead of pause() or mute() on the music object, but this doesn't remove the notification.

Looking a bit deeper, I discovered the experimental MediaSession API (W3C draft) which supposedly can be used to control the notification. But, if I understand correctly, it offers no way to disable it outright!

I tried this at the start of my application:

if (typeof navigator.mediaSession == 'object') {
  navigator.mediaSession.playbackState = 'none'
}

However, this only sets the declared playback state (in spec terminology). And setting that to 'none' has no effect:

The actual playback state is computed in the following way:

  • If the declared playback state is "playing", return "playing".
  • Otherwise, return the guessed playback state.

And the guessed playback state is something I have no control over; it's derived by the browser based on the state of <audio> elements on the page.

Is there a possibility that I'm overlooking, or is this just an oversight in the current MediaSession specification?

Skiest answered 17/4, 2019 at 12:21 Comment(0)
I
3

This may be what you are looking for.

If you can make the music loop within 5 second (like in NES days), notification won't show it said.

Else, use Web Audio API without "audio" element. So, use a stream.

Otherwise, "Dismiss media notifications with audio.src = ''." reference

Imhoff answered 10/9, 2019 at 5:26 Comment(1)
That link was already in my original post. Even music on the NES was much longer than 5 seconds. Using Web Audio has the drawback that you have to download and decode the entire file before you can start playback, which is why I instructed Howler to use an audio element. Finally, audio.src='' (not ".") is going to stop playback, isn't it?Skiest

© 2022 - 2024 — McMap. All rights reserved.