How do I run code in the background on iOS to play audio and talk to a Bluetooth accessory?

EasyGoingPatEasyGoingPat GBMember ✭✭✭

I am sorry for the general nature of this question. I have read lots of documents on how to do this. In fact, that's the problem: I have found loads of documents that seem to cover this but none seems to be exactly what I want.

My requirements are deceptively simple:

  1. I do need my application to continue running when it is sent to the background or the phone's screen goes off.
  2. I do not need to update the screen (which would be pointless anyway since it could be off).
  3. I do need to play audio.
  4. I do need to communicate with Bluetooth devices (earphones and the Apple Watch to begin with).

Can anyone point me in the right direction to begin learning how to do this?

Any help or advice would be very much appreciated.

Kind wishes ~ Patrick

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    It seems you need the Background Mode on iOS: https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/registering-applications-to-run-in-background.
    Register the Audio and App communicates using CoreBluetooth capabilities in your app's info.plist:

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @LandLu

    Thank you for the reply.

    I did read that documentation. It has lead me partway to a solution. By doing what you show above and using AVAudioSession, I can get music to play in the background for longer than ten minutes. My problem is that I need other code to run, not just audio playback.

    If I follow this flowchart...

    Choose Backgrounding

    ... I get the audio part working but, when I look for a place to run my other code, I follow this path in the flowchart:
    1. iOS ->
    2. An Application ->
    3. Is it audio? = Yes ->
    4. Register as background necessary application ->
    5. This won't let me do what I want ->
    6. Does app get data from provider? = No ->
    7. Are you tracking location? = No ->
    8. Break it into smaller tasks ->
    9. A Task ->
    10. Network transfer? = No ->
    11. Longer than 10 minutes? Yes or No ->

    Ideally I would answer "Yes" because I'd like simply to run a task in the background for up to an hour. This gets me nowhere, probably because my particular use-case is heavy on the battery and Apple simply don't want me to do it.

    Okay, so I can figure out how to answer "No", by breaking my work into small chunks, but I must get woken up to work on these small chunks, perhaps every 15-30 seconds. Is there any way to do this?

    The only way I can think of is to play a silent WAV or MP3 that is, say, 30 seconds long and register for an event that tells me when this has finished playing. Does this sound like a possible solution?

    Your advice would be gratefully received. I'm in a bit of a hole here.

    Kind wishes - Patrick

  • LandLuLandLu Member, Xamarin Team Xamurai

    No, unfortunately you can't do that. As you mentioned above even though you can play a silent music to persist a long
    time background usage. When you upload to App Store, Apple will refuse this submitting.
    Background mode can only be used for the categories listed above. You can't run a background task for one hour, this is not allowed by Apple.
    If your background task is a network task, I think the most similar one is Background fetch. But this is another thing. We also should use as little time as possible each time this task triggered. And the next time to fire this task is also managed by system. The goal is using the least energy when your app is on the background state.

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    My application will be playing a long piece of music, up to one hour in length. This will be started voluntarily by the user.

    I have added a key-value observer callback like this AVPlayer.AddPeriodicTimeObserver(), that triggers perhaps every 30 seconds, and I do a small amount of processing (maybe a second or two maximum).

    I am aware of the need to conserve battery but my use case is a legitimate one. It is a relaxation application. The user starts the session but then the phone sits there, untouched, for up to an hour. It is similar to a media player, except that the application needs to adjust the audio as it plays.

    Surely Apple would not block such an application?

Sign In or Register to comment.