Hello,
I’m using HTML5 Audio in my Capacitor App to play music. On iOS, the native Audio controls are showing up, like you see in this screenshot .
Sadly, it doesn’t show up on Android. Surprisingly, it shows up if I open my Ionic/Capacitor App in the browser and press play. See the Web-App: https://audio-player-sigma.vercel.app
This is my Code:
const player = new Audio();
player.src = 'https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_2MG.mp3';
player.onplay = () => {
if ('mediaSession' in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: 'Lorem Ipsum',
artist: 'Lorem Ipsum',
album: 'Lorem Ipsum',
artwork: [
{ src: 'https://dummyimage.com/96x96', sizes: '96x96', type: 'image/png' },
{ src: 'https://dummyimage.com/128x128', sizes: '128x128', type: 'image/png' },
{ src: 'https://dummyimage.com/192x192', sizes: '192x192', type: 'image/png' },
{ src: 'https://dummyimage.com/256x256', sizes: '256x256', type: 'image/png' },
{ src: 'https://dummyimage.com/384x384', sizes: '384x384', type: 'image/png' },
{ src: 'https://dummyimage.com/512x512', sizes: '512x512', type: 'image/png' },
]
});
console.log('init mediaSession metadata');
} else {
console.log('mediaSession not available');
}
};
So it works in the system browser, but not in .apk built by capacitor. On the iOS App aren’t any problems. Everything works fine.
Github repository: https://github.com/mariusbolik/capacitor-audio-player
Any ideas how to get it working on the android capacitor project?
Thanks!
Hi,
I saw this on the past, searching for cordova alternatives.
May be depends on the Webview version that you are using.
" 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."
Created a capacitor community plugin proposal
opened 08:08AM - 03 Feb 22 UTC
plugin proposal
## Plugin Request
**Name**: Music controls
**Package**: `@capacitor-community/… music-controls`
### Platform(s)
Android
iOS
### Existing Solutions
https://github.com/selcip/capacitor-music-controls-plugin
https://github.com/ingageco/capacitor-music-controls-plugin
### Description
Same features that supported by cordova plugin alternative
https://github.com/ghenry22/cordova-plugin-music-controls2
This is not working with capacitor
There is no alternative working on Capacitor v3
01/03/2022 Update:
The media session api for web https://web.dev/media-session/ can handle controls for web and iOS but it is not working on Android. I think that because of WebView.
Also I have found this plugin but is not working yet: https://github.com/Videodock/capacitor-media-session
I think that may be it would be enough with a plugin working on android with https://developer.android.com/guide/topics/media-apps/working-with-a-media-session#init-session
and on iOS with https://web.dev/media-session/
Are there any news on this? I’m running into the same problem and going crazy. I also can’t find other solutions for Cap4 and media controls.
Currently we are using this one:
You can see discussion here:
opened 08:08AM - 03 Feb 22 UTC
plugin proposal
## Plugin Request
**Name**: Music controls
**Package**: `@capacitor-community/… music-controls`
### Platform(s)
Android
iOS
### Existing Solutions
https://github.com/selcip/capacitor-music-controls-plugin
https://github.com/ingageco/capacitor-music-controls-plugin
### Description
Same features that supported by cordova plugin alternative
https://github.com/ghenry22/cordova-plugin-music-controls2
This is not working with capacitor
There is no alternative working on Capacitor v3
01/03/2022 Update:
The media session api for web https://web.dev/media-session/ can handle controls for web and iOS but it is not working on Android. I think that because of WebView.
Also I have found this plugin but is not working yet: https://github.com/Videodock/capacitor-media-session
I think that may be it would be enough with a plugin working on android with https://developer.android.com/guide/topics/media-apps/working-with-a-media-session#init-session
and on iOS with https://web.dev/media-session/
This plugin is the final solution to display media controls on Android: