1

Using AudioQueueServices to play a streaming MP3 file, the player stops when the app is switched to the background.

I have:

  • On the "Background Modes" panel of the "Capabilities" project settings page, I've turned on "Audio, AirPlay and Picture in Picture" and "Background fetch"
  • On the "Info" page, under "Custom iOS Target Properties", added a "Required background modes" node with entries for "App plays audio or streams audio/video using AirPlay" and "App downloads content from the network"

  • In the initialisation code for the player module, I include the following call:

    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &error];

    which completes without signalling an error.

According to my logs, as soon as the app switches to the background, I stop receiving any callbacks, so playback stops almost immediately afterwards. What do I need to do to get it to continue?

Jules
  • 14,841
  • 9
  • 83
  • 130
  • did you try to play audio from bundle is it working ? – Prashant Tukadiya Jul 24 '18 at 09:24
  • Try to set `AVAudioSessionCategoryPlayAndRecord` – Prashant Tukadiya Jul 24 '18 at 09:35
  • @PrashantTukadiya - I haven't tried playing from the bundle, but can't see how that would make any difference at all -- the system doesn't know where the audio data is coming from, because I load it into memory and then play it back through a buffer; the callback to refill the audio queue buffer from my own internal buffer never gets called. Changing the category had no effect. – Jules Jul 24 '18 at 10:03
  • Changing the category had no effect ... other than to cause playback to go through the receiver rather than the speaker when used on a phone (no change on simulator, though). – Jules Jul 24 '18 at 12:50

1 Answers1

0
  1. Checking AVAudioSession is active
  2. Checking AudioQueueRef is running:

    UInt32 isRuning = NO;
    UInt32 size = sizeof(isRuning);
    AudioQueueGetProperty(inAQ, kAudioQueueProperty_IsRunning, &isRuning, &size);
    
selton
  • 77
  • 4
  • 1
    Adding a call to `AVAudioSession.sharedInstance().setActive(true)` during my play operation has no effect, so the session seems to be active (or there was another issue anyway). The queue is definitely running, and plays while in foreground (and automatically resumes playing when the app returns to foreground). – Jules Jul 24 '18 at 10:30