start playing audio from a background task via AVAudioPlayer in Xcode

前端 未结 5 1005
小鲜肉
小鲜肉 2021-02-03 14:46

I am trying to start playing a sound from a background task via an AVAudioPlayer that is instantiated then, so here\'s what I\'ve got.

For readability I cut out all user

相关标签:
5条回答
  • 2021-02-03 14:53

    Seems to me as if you do not have the appropriate background mode set? Xcode screenshot

    Also, looks like another user had the same issue and fixed it with an Apple Docs post. Link to Post

    You may need to just do the step to set the audio session as active:

    [audioSession setActive:YES error:&activationError];
    

    Hope it helps!

    0 讨论(0)
  • 2021-02-03 15:02

    include your playSoundFile call in the below, this should always run it in main thread

    dispatch_async(dispatch_get_main_queue(), ^{
    
    });
    

    add this if to your - (void) playSoundFile to check if player is created, if not, then create it

    if(!_player) {
        _player = [[AVQueuePlayer alloc] init];
    }
    
    0 讨论(0)
  • 2021-02-03 15:13

    If you need to start playing a sound in the background (without a trick such as keeping on playing at volume zero until you need the sound):

    • Set Background mode Audio in your p.list;
    • Set AudioSession category to AVAudioSessionCategoryPlayback
    • Set AudioSession to active
    • When your backgrounded app becomes running, start a background task before playing the sound (apparently when the remaining background time is less than 10 seconds, the sound is not played)

    This now works for me.

    0 讨论(0)
  • 2021-02-03 15:14

    What fixed it for me was to start playing some audio just as application quits, so I added 1sec blank audio in applicationWillResignActive in the AppDelegate

    func applicationWillResignActive(_ application: UIApplication) {
    
            self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
                self.endBackgroundUpdateTask(true)
            })
    
    
            let secSilence = URL(fileURLWithPath: Bundle.main.path(forResource: "1secSilence", ofType: "mp3")!)
    
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: secSilence)
            }
            catch {
                print("Error in loading sound file")
            }
    
            audioPlayer.prepareToPlay()
            audioPlayer.play()
    
    
    
    }
    
    0 讨论(0)
  • 2021-02-03 15:18

    From what I've learned after writing an player App, it seems that you can not start playing audio when your App is already in background for longer than X seconds, even if you have configured everything right.

    To fix it, you have to use background task wisely. The most important thing is that you must NOT call endBackgroundTask immediately after playSoundFile. Delay it for about 5-10 seconds.

    Here is how I managed doing it for iOS6 and iOS7.

    1, Add audio UIBackgroundModes in plist.

    2, Set audio session category:

    3, Create an AVQueuePlayer in the main thread and enqueue an audio asset before the App enter background.

    *For continues playing in background (like playing a playlist)*

    4, Make sure the audio queue in AVQueuePlayer never become empty, otherwise your App will be suspended when it finishes playing the last asset.

    5, If 5 seconds is not enough for you to get the next asset you can employ background task to delay the suspend. When the asset is ready you should call endBackgroundTask after 10 seconds.

    0 讨论(0)
提交回复
热议问题