How can we handle enable/disable background audio abilities at runtime on iOS devices?

穿精又带淫゛_ 提交于 2019-12-01 14:13:54

Have an iVar of type UIBackgroundTaskIdentifier in the main class that handles audio playing code, initialize this with beginBackgroundTaskWithExpirationHandler.... method before starting the audio player and use endBackgroundTask when audio player completes.

Code:

@inerface AudioPlayerController : NSObject
{
    UIBackgroundTaskIdentifier bgTaskID;
}
@end

@implementation AudioPlayerController

- (void) startPlayer
{

    bgTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

   // Write code to start the audio player
}

// Call this when your program receives message of audio playing complete

- (void) audioPlayComplete
{

    // Do the audio playing finish

    if (bgTaskID != UIBackgroundTaskInvalid)
      [[UIApplication sharedApplication] endBackgroundTask:bgTaskID];
} 

@end

In your AppDeletate:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[YouPlayerClass sharedInstance] stop];
}

Thus The audio stops at app entering background.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!