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

Playing multiple files sequentially in background doesn't work in iOS

Open Defcon0 opened this issue 5 years ago • 11 comments

Hello,

I have an app that plays preview mp3's of a music album, i.e. song1, after that song2, ...

Since there's no playBatch() function in the Media object of this plugin, I did that by playing the next file after the last had finished playing. This works great on Android (even in background).

In iOS when I put the app in background (UIBackgroundModes is set correctly in Xcode), it tries to play the next file but fails with error 4.

Here's the log:

2019-01-12 16:52:59.931116+0100 MyApp[13620:2524776] Unable to play audio: (null) 2019-01-12 16:52:59.952107+0100 MyApp[13620:2524565] Error while playing file: 2019-01-12 16:52:59.952184+0100 MyApp[13620:2524565] {"message":"","code":4}

So it seems that the callback in Media.MEDIA_STOPPED doesn't get the needed data in the background, because this is the place where I start playing the next file. But the remote path of the audio file is correctly passed into the Media object. Maybe this isn't possible in iOS in the background at all? I recently noticed that the app is paused here completely, so background sync of user data doesn't seem to be possible neither.

Is there a way to accomplish what I want? Is there a way to have a playMultiple() or playBatch() function or is there another issue with my files?

Thanks in advance!

Bye

Defcon0 avatar Jan 12 '19 15:01 Defcon0

if you are using wkwebview on ios it kills all javascript processing in background by default which will stop any non-native function from working correctly in the background, just as you have seen. Regardless of background operation entitlements.

Check out the ionic-webview, it has additional support for background operation to support legitimate background tasks exactly like this.

ghenry22 avatar Jan 24 '19 05:01 ghenry22

Hi guys. @Defcon0 did you found any workaround for this?

IllesAprod avatar Apr 04 '19 06:04 IllesAprod

No, unfortunately not.

Defcon0 avatar Apr 04 '19 06:04 Defcon0

I'm also facing this issue, using "cordova-plugin-ionic-webview": "^2.4.1" Playing the next file while app is in background fails with {"message":"","code":4} I can see that my javascript is running fine while app is in background, but the plugin returns this error.

Issue not resolved by upgrading to cordova-plugin-ionic-webview 4.0.1

Can anyone give me pointers to a solution or options to investigate?

rlyttle avatar Jun 07 '19 11:06 rlyttle

Also upgraded to cordova-ios 5.0.1 with no resolution.

rlyttle avatar Jun 07 '19 11:06 rlyttle

I can see the following in iOS console when the problem happens:

CoreMedia	-CMSessionMgr- cmsSkipBeginInterruption: CMSession: Skipping begin interruption because it cannot interrupt others

error	14:53:35.397452 +0100	mediaserverd	AudioToolbox	259: { "action":"cm_session_begin_interruption", "error":"translating CM session error", "session":{"ID":"0x6bf2a14","PID":29830,"name":"My App"}, "details":{"calling_line":2949,"error_code":-12985,"error_string":"Operation denied. Cannot interrupt others"} }

rlyttle avatar Jun 07 '19 14:06 rlyttle

I have similar problem on IOS using a live hls streaming, a lot of times it does not start

fdambrosio avatar Sep 08 '19 12:09 fdambrosio

Hi i fix changing the CDVSound.m Line 374

From: [self.avSession setCategory:sessionCategory error:&err]; To: [self.avSession setCategory:sessionCategory withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&err];

If some one could add this to the plugin it was better and ad a PR.

pbocaapp avatar Oct 23 '20 11:10 pbocaapp

Hi i fix changing the CDVSound.m Line 374

From: [self.avSession setCategory:sessionCategory error:&err]; To: [self.avSession setCategory:sessionCategory withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&err];

If some one could add this to the plugin it was better and ad a PR.

This allows the next music to play in background, but then the MusicControl wouldn't work, is there any other fix

impozzible avatar Dec 19 '20 12:12 impozzible

Hi i fix changing the CDVSound.m Line 374 From: [self.avSession setCategory:sessionCategory error:&err]; To: [self.avSession setCategory:sessionCategory withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&err]; If some one could add this to the plugin it was better and ad a PR.

This allows the next music to play in background, but then the MusicControl wouldn't work, is there any other fix

Same for me. It works, but the MusicControl is not loading anymore.

BrOrlandi avatar Mar 27 '21 01:03 BrOrlandi

This solution worked for me: first change line 374 From: [self.avSession setCategory:sessionCategory error:&err]; To: [self.avSession setCategory:AVAudioSessionCategoryPlayback error:&err];

and then in 'AppDelegate.swift' in function 'didFinishLaunchingWithOptions' add this lline:

UIApplication.shared.beginReceivingRemoteControlEvents()

mahmoudym avatar Apr 08 '21 06:04 mahmoudym