Audio playing while app in background iOS

前端 未结 7 903
我寻月下人不归
我寻月下人不归 2021-02-05 17:19

I have an app mostly based around Core Bluetooth. When something specific happens, the app is woken up using Core Bluetooth background modes and it fires off an alarm, however I

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 17:58

    I have an App than also needs background audio but my App works with the App background mode "Voice over IP" as it needs to record sometimes. I play background audio telling the App singleton I need to play audio in background:

    UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
    if([thePlayer play]){
        newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];   
    }
    

    EDIT: You must call [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]; before your app goes to background. In my app, it is at the same time you start playing, in yours, if the player might be started in background, you should do:

    - (void)applicationDidEnterBackground:(UIApplication *)application{
      // You should retain newTaskId to check for background tasks and finish them 
      newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];   
    }
    

提交回复
热议问题