cordova-plugin-nativeaudio
cordova-plugin-nativeaudio copied to clipboard
Fix background music interruption on plugin init on Android and iOS
This patch fixes background audio or music from other apps being interrupted when an app with this plugin was started, both on iOS and Android.
On iOS: Removed deprecated AudioSessionInitialize call and extra setCategory and setActive calls that where not really needed and where overriding the main initialization call.
On Android: removed the AudioManager requestAudioFocus call that is not really needed, playing audio will work fine without this call.
I just tried this and can verify it works on iOS
hi @Rodmg I just discovered an issue with this PR fix. It works well in the case when say I'm listening to Spotify and then switch over to my app. Spotify keeps playing as intended.
However it fails when my app is playing music and I hit the home button and put my app in the background.
Expected behavior: My app keeps playing.
Actual behavior: My app stops playing.
Note: Background Mode for Audio is enabled in XCode. And when I switch back to the master branch, my app keeps playing when put in the background.
This PR should be fixed asap. Before applying the patch, every audio from html5 was beeing interrupted. (even playing videos was paused).
After applying this patch, I can run those at the same time. Good work!
@shi11 You are describing a different case, whereas your app is the one that should play in the background. The fix and this issue is about not stopping possibly running background music (switched roles of the apps).
@mxmzb correct - audio from my app should continue in the background. This is existing behavior in the master branch. Unfortunately this PR breaks that. I would love for this patch to work as well as not break existing behavior.
We are now maintaining a forked version wizpanda/cordova-plugin-nativeaudio of this plugin which includes commits from this PR.
We are now maintaining a forked version wizpanda/cordova-plugin-nativeaudio of this plugin which includes commits from this PR.
Hey, really appreciate you maintaining a fork with these changes. Has saved me a lot of time!
I'm trying to use it in a project but can't get the sound to play on iOS for some reason. The app is basically a timer that plays a sound when it reaches 0. Was working fine using cordova-plugin-nativeaudio 3.0.9 on npm but I got complaints about it stopping spotify/background music so needed an alternative. Using ionic native on ios12.2. My code is as follows:
Preload the audio:
this.nativeAudio.preloadComplex('timerFinished', 'assets/audio/timer.mp3', 1, 1, 0);
And when the timer reaches 0:
this.nativeAudio.play('timerFinished');
If I monitor the events, the are all triggered. As in, the audio seems to be preloaded, and thinks it is playing. Not getting any errors. However I never hear a sound. Any ideas what could be going wrong?
I just tried with one of my app and it worked fine. Is it possible for you to create a fresh app & test that?
@sagrawal31 many thanks for maintaining your fork of this repo. Unfortunately I don't see the Issues section in your repo so I don't know where to add my comments. It's about pausing music in background mode. This line does it on Android. I don't know why it was implemented like this but I think it must allow playing music on background or at least this should be optional. Is it possible to change this behavior in your fork?
Thanks @andrey3diq. We can make that as configuration for sure.