开发者

Setting the AVAudioSession category in AppDelegate.m

开发者 https://www.devze.com 2023-02-03 03:17 出处:网络
So I hate to have to ask this question but I\'ve spent a fair bit of time searching through Apple\'s documentation and Google with no avail. I\'m simply trying to set the AVAudioSession category for m

So I hate to have to ask this question but I've spent a fair bit of time searching through Apple's documentation and Google with no avail. I'm simply trying to set the AVAudioSession category for my app ONCE, when the applicationDidFinishLaunching. I have an app that plays an audio stream and I would like it to continue playing when the app enters the background, so I'm trying to use the Playback category. Here is my code for AppDelegate.m :

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Set AudioSession
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
[[AVAudioSession sharedInstance] setDelegate:self];

// create window and set up navigation controller
[window addSubview:myNavController.view];
[windo开发者_如何学编程w makeKeyAndVisible];

}

# pragma mark -
# pragma mark AVAudioSession Delegate Methods 
- (void)beginInterruption {
}
- (void)endInterruption {
}
- (void)endInterruptionWithFlags:(NSUInteger)flags {
}
- (void)inputIsAvailableChanged:(BOOL)isInputAvailable {
}

With this code, the audio fades out anytime I hit the home button, putting the app in the background. Any help is much appreciated, I hope that it is a quick fix type of answer for anybody who has done this before.


First add the UIBackgroundModes key to your Info.plist file if you haven't done already. More info here.

If you have done that already, which framework do you use to play your media?


Thanks for the help Irene. You are pretty much right with your answer except I just wanted to provide the steps that were necessary for it to work for me. I read the apple documentation that you posted and for some reason it left these important details out:

  1. When you add the UIBackgroundModes key in the .plist file, you have to make it an array.
  2. The value for Item 0 of the array should be audio.

Of course your app should also take care of setting its audio session category in combination with setting this key.

0

精彩评论

暂无评论...
验证码 换一张
取 消