cordova-plugin-nativeaudio icon indicating copy to clipboard operation
cordova-plugin-nativeaudio copied to clipboard

Plugin stops background music

Open netzzeit opened this issue 7 years ago • 12 comments

When I playback a Sound-File with this plugin in Android, it stops music in the background (Napster-App in my case) and doesn't resume the music afterwards. Here is my code:

window.plugins.NativeAudio.preloadSimple( file, 'sound/'+file, function(msg){ console.log("sound loaded:"+file); window.plugins.NativeAudio.play( file ); window.setTimeout( function(){ console.log("Sound unloaded"); window.plugins.NativeAudio.unload( file ); }, 1000 * 5 );

Is there a way to fix that?

netzzeit avatar Feb 13 '17 17:02 netzzeit

There's already an issue (#105) about this. Would be nice to keep the conversation in one place.

gligoran avatar Feb 14 '17 13:02 gligoran

Thanks to @ThibaudD for the pull request #117 to fix this issue.

floatinghotpot avatar Mar 05 '17 02:03 floatinghotpot

@floatinghotpot On Android, 6.0.1, it still pauses music. Original code (uses ionic): NativeAudio.preloadSimple("ding", "assets/sounds/ding.mp3");

Updated to latest version, of course.

AmitMY avatar Mar 10 '17 11:03 AmitMY

Still getting this issue with simple and complex sounds on iOS.

darve avatar Jun 24 '17 11:06 darve

Also still getting this issue on Android, when loading a simple sound. Has anyone found a fix or a workaround?

juanitotaveras avatar Sep 08 '17 02:09 juanitotaveras

Please check if pull request #139 fixes it for you

Rodmg avatar Nov 14 '17 22:11 Rodmg

@Rodmg No it doesn't. The nativeaudio plugin stops Apple Music when the app loads. When a sound is played in the app, the plugin stops any music app playing — and takes over the iOS audio player in control center.

Looking forward to a permanent fix for this issue.

DamiJegede avatar Jan 17 '18 03:01 DamiJegede

Hi everyone. I solved this problem. ı edited plugin files and it is working...

You should find this file in your project.
yourworkspace/platforms/android/src/com/rjfun/cordova/plugin/NativeAudioAssetComplex.java

and you make comment this line

..... private void invokePlay( Boolean loop ) { Boolean playing = mp.isPlaying(); if ( playing ) { //mp.pause(); THIS LINE mp.setLooping(loop); mp.seekTo(0); mp.start(); } if ( !playing && state == PREPARED ) { state = (loop ? PENDING_LOOP : PENDING_PLAY); onPrepared( mp ); } else if ( !playing ) { state = (loop ? PENDING_LOOP : PENDING_PLAY); mp.setLooping(loop); mp.start(); } }

Now your app playing on background...

That's all... :)

vennassafir avatar Sep 29 '18 17:09 vennassafir

don't working for ionic 4.x with android 8.1 sdk. if screen go to standby audio is pause.

onopko81 avatar Nov 12 '19 11:11 onopko81

Same thing on android 10, music is paused, even with this https://github.com/floatinghotpot/cordova-plugin-nativeaudio/pull/139/files

julianCast avatar Aug 13 '20 22:08 julianCast

Same problem as @julianCast here

potije avatar Aug 17 '20 14:08 potije

Same problem here on iOS 13. It stops the background music and also, the case where an iFrame video with music is playing.

Edit:

I noticed Capacitor now has simple and complex preloads. I'm not familiar with Java or writing Cordova plugins though I know some Swift. Would this be a doable migration to Cordova?

https://github.com/capacitor-community/native-audio

cc @ThibaudD @floatinghotpot

aspdev91 avatar Sep 24 '20 00:09 aspdev91