// // 监听音频打断事件
// setup our audio session
AVAudioSession *sessionInstance = [AVAudioSession sharedInstance];
// add interruption handler
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(audioSessionWasInterrupted:)
name:AVAudioSessionInterruptionNotification
object:sessionInstance];
NSError *error = nil;
[sessionInstance setCategory:AVAudioSessionCategoryPlayback error:&error];
if(nil != error) NSLog(@"Error setting audio session category! %@", error);
else {
[sessionInstance setActive:YES error:&error];
if (nil != error) NSLog(@"Error setting audio session active! %@", error);
}
- (void)audioSessionWasInterrupted:(NSNotification *)notification
{
NSLog(@"the notification is %@",notification);
if (AVAudioSessionInterruptionTypeBegan == [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue])
{
NSLog(@"begin");
}
else if (AVAudioSessionInterruptionTypeEnded == [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue])
{
NSLog(@"begin - end");
}
}
Apple Demo
https://developer.apple.com/library/prerelease/content/samplecode/MusicCube/Introduction/Intro.html
AVAudioSessionCategory说明
http://blog.sina.com.cn/s/blog_7ea0400d0102vjur.html
AVAudioSessionCategoryAm
用于非以语音为主的应用,使用这个category的应用会随着静音键和屏幕关闭而静音。并且不会中止其它应用播放声音,可以和其它自带应用如iPod,safari等同时播放声音。注意:该Category无法在后台播放声音