How can we handle enable/disable background audio

2019-02-20 21:09发布

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 ?

2条回答
Fickle 薄情
2楼-- · 2019-02-20 21:29

In your AppDeletate:

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

Thus The audio stops at app entering background.

查看更多
萌系小妹纸
3楼-- · 2019-02-20 21:40

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
查看更多
登录 后发表回答