Search code examples
androidcordovanavigator

Cordova navigator.mediaSession


I a trying to get media controls working on Android when playing audio.

I originally was using html5 audio, but then moved to using cordova-plugin-media to play the audio, and used music-controls which is a cordova plugin for the controls. (I actually tried quite a few different plugins etc). The issue I had was that none of the plugins worked 100%, I was still able to find ways to pause audio etc on the device without it calling my callbacks to keep my app in sync. I have now given up with this approach after many days of troubleshooting.

I was reading an article yesterday and found out about navigator.mediaSession and was able to get it up and running inside of google chrome on desktop by switching back to html5 audo, however, navigator.mediaSession is not defined when I build it through cordova.

Is it possible to use naviator.mediaSession through cordova? If so what am I missing?

If not, then it's back to pulling my hair out. If anyone has any suggestions they would be greatly appreciated.


Solution

  • To answer your question, Navigator.mediaSession don't work in cordova as Android webview don't support them. You can find more info on this under the browser compatibility section of Navigator API Infact, mediaSession itself is in experimental mode as of now.

    The best bet is to go with cordova plugins and enhance them according to your need. As you mentioned there are quite a few plugins exclusively for media and music controls. To name a few,

    https://github.com/homerours/cordova-music-controls-plugin

    https://github.com/521dimensions/cordova-music-controls-plugin

    Plugins is the right approach to proceed is what I would say.