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

喜你入骨 提交于 2019-12-19 12:01:41

问题


I know how to set up my iOS application to make it playing audio in background (= when another application is used on the iOS device) using the plist.

Some applications like: - BLOOM - some radio players

provides a basic UISwitch to enable or disable this behavior.

Any ideas about how to do that ?


回答1:


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



回答2:


In your AppDeletate:

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

Thus The audio stops at app entering background.



来源:https://stackoverflow.com/questions/10504432/how-can-we-handle-enable-disable-background-audio-abilities-at-runtime-on-ios-de

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