Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix background music interruption on plugin init on Android and iOS #139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Rodmg
Copy link

@Rodmg Rodmg commented Nov 14, 2017

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.

@waymondo
Copy link

waymondo commented Dec 8, 2017

I just tried this and can verify it works on iOS

@shi11
Copy link

shi11 commented Jan 11, 2018

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.

@hirbod
Copy link

hirbod commented Apr 5, 2018

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!

@mxmzb
Copy link

mxmzb commented May 9, 2018

@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).

@shi11
Copy link

shi11 commented May 9, 2018

@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.

@sagrawal31
Copy link

sagrawal31 commented Apr 4, 2019

We are now maintaining a forked version wizpanda/cordova-plugin-nativeaudio of this plugin which includes commits from this PR.

@sanctus671
Copy link

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?

@sagrawal31
Copy link

I just tried with one of my app and it worked fine. Is it possible for you to create a fresh app & test that?

@andrey3diq
Copy link

@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?

@sagrawal31
Copy link

Thanks @andrey3diq. We can make that as configuration for sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants