start playing audio from a background task via AVAudioPlayer in Xcode

前端 未结 5 1116
小鲜肉
小鲜肉 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 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.

提交回复
热议问题