Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 231
Thomas Avatar asked Apr 17 '19 12:04

Thomas


1 Answers

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

like image 168
Edward Aung Avatar answered Oct 06 '22 01:10

Edward Aung