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

后端 未结 2 749
再見小時候
再見小時候 2021-01-17 05:02

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 applicati

相关标签:
2条回答
  • 2021-01-17 05:54

    In your AppDeletate:

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

    Thus The audio stops at app entering background.

    0 讨论(0)
  • 2021-01-17 05:59

    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
    
    0 讨论(0)
提交回复
热议问题