audioplayer icon indicating copy to clipboard operation
audioplayer copied to clipboard

[iOS] audio stops playing in background

Open ralph-bergmann opened this issue 8 years ago • 9 comments

I tried the example app on a real iPhone device. When I switch the phone off audio stops playing. When I switch it on again it continue playing.

Do you have any hints?

I added audio to the UIBackgroundModes in the example/ios/Runner/Info.plist but it doesn't fixed the issue.

ralph-bergmann avatar Dec 15 '17 15:12 ralph-bergmann

Wow, is it true? Because background playing is a critical for my application.

denvist avatar Feb 24 '18 01:02 denvist

yes, the mediaplayer.prepare method blocks the ui thread, if you have a hugh file the prepare methods needs a couple of seconds.

ralph-bergmann avatar Feb 24 '18 11:02 ralph-bergmann

I managed to solve the issue with the background mode with this code and with the plist configuration UIBackgroundModes

do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers) try AVAudioSession.sharedInstance().setActive(true) } catch { print(error) }

ficiverson avatar Mar 27 '18 10:03 ficiverson

@ficiverson I'm about to start a Flutter project and playing audio in the background is a core requirement. Can you please confirm that you were able to handle it with your workaround?

omarrida avatar Mar 30 '18 13:03 omarrida

@omarrida For me its working on a radiostation app (only iOS version tested). Now I can listen to streaming music on background but I dont know if it works with local files because I dont have that requirement

ficiverson avatar Apr 03 '18 09:04 ficiverson

The way to fix that is

  1. Set AVAudioSession category to playback mode, I proposed a fix waiting to be merged : pull request #43
  2. Add ausio in UIBackgroundModes in your .plist files.

eddywm avatar Jun 24 '18 23:06 eddywm

The way to fix that is

  1. Set AVAudioSession category to playback mode, I proposed a fix waiting to be merged : pull request Audio output for speakers now supported on iOS #43
  2. Add ausio in UIBackgroundModes in your .plist files.

I done: 1.AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback) 2. UIBackgroundModes audio

It still can't play in the background.

gateshibill avatar Dec 16 '21 11:12 gateshibill

The way to fix that is

  1. Set AVAudioSession category to playback mode, I proposed a fix waiting to be merged : pull request Audio output for speakers now supported on iOS #43
  2. Add ausio in UIBackgroundModes in your .plist files.

ERROR: CANNOT PLAY MUSIC IN BACKGROUND. Message from code: "Error Domain=NSOSStatusErrorDomain Code=2003329396 "(null)""

gateshibill avatar Dec 16 '21 12:12 gateshibill

The way to fix that is

  1. Set AVAudioSession category to playback mode, I proposed a fix waiting to be merged : pull request Audio output for speakers now supported on iOS #43
  2. Add ausio in UIBackgroundModes in your .plist files.

I done: 1.AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback) 2. UIBackgroundModes audio

It still can't play in the background.

fixed it, it work well try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: AVAudioSession.CategoryOptions.mixWithOthers) NSLog("Playback OK") try AVAudioSession.sharedInstance().setActive(true) NSLog("Session is Active") } catch { NSLog("ERROR: CANNOT PLAY MUSIC IN BACKGROUND. Message from code: "(error)"") }

gateshibill avatar Dec 17 '21 09:12 gateshibill