I'm trying to make a simple Radio player for iOS 7 (AVPlayer), but i have no clue how to use the AudioSession API. There are some tutorials, but those are targeting iOS 6 or below.
Could somebody post a snippet or maybe a link to an iOS 7 AV tutorial?
Sure. This will setup your audio session for playback and enable mixing with other audio, then activate the session. This is using the new Objective-C API, not the old C-based one you see in all the examples.
If you want to receive remote control events and/or display album/song info over AirPlay and in Control Center, you can't enable the mixing with other apps option so in your case you may want to omit that options dictionary.
NSError *audioError = nil;
AVAudioSession *session = [AVAudioSession sharedInstance];
if(![session setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&audioError]) {
NSLog(@"[AppDelegate] Failed to setup audio session: %@", audioError);
}
[session setActive:YES error:&audioError];
A few other tips - make sure you add audio to the UIBackgroundModes key in your info.plist file to allow background audio playback.
If you want remote control events (via Control Center, headphones, bluetooth, AirPlay, etc) then call
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
and put this in your app delegate:
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
if(event.type == UIEventTypeRemoteControl)
{
switch(event.subtype)
{
case UIEventSubtypeRemoteControlPause:
case UIEventSubtypeRemoteControlStop:
break;
case UIEventSubtypeRemoteControlPlay:
break;
default:
break;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With