rxlabz / audioplayer

A flutter plugin to play audio files iOS / Android / MacOS / Web ( Swift/Java )
https://pub.dartlang.org/packages/audioplayer
494 stars 189 forks source link

[iOS] audio stops playing in background #14

Open ralph-bergmann opened 6 years ago

ralph-bergmann commented 6 years ago

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.

denvist commented 6 years ago

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

ralph-bergmann commented 6 years ago

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

ficiverson commented 6 years ago

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) }

omarrida commented 6 years ago

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

ficiverson commented 6 years ago

@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

eddywm commented 6 years ago

The way to fix that is

  1. Set AVAudioSessioncategory to playback mode, I proposed a fix waiting to be merged : pull request #43
  2. Add ausio inUIBackgroundModes in your .plist files.
gateshibill commented 2 years ago

The way to fix that is

  1. Set AVAudioSessioncategory 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 inUIBackgroundModes in your .plist files.

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

  1. UIBackgroundModes
    <array>
            <string>audio</string>

    It still can't play in the background.

gateshibill commented 2 years ago

The way to fix that is

  1. Set AVAudioSessioncategory 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 inUIBackgroundModes in your .plist files.

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

gateshibill commented 2 years ago

The way to fix that is

  1. Set AVAudioSessioncategory 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 inUIBackgroundModes 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)\"") }