app播放聲音被打斷處理
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
此方法在iOS8已經廢棄
/* AVAudioPlayer INTERRUPTION NOTIFICATIONS ARE DEPRECATED - Use AVAudioSession instead. */
改用
AVAudioSession
通知AVAudioSessionInterruptionNotification
文檔信息
/* Registered listeners will be notified when the system has interrupted the audio session and when
the interruption has ended. Check the notification's userInfo dictionary for the interruption type -- either begin or end.
In the case of an end interruption notification, check the userInfo dictionary for AVAudioSessionInterruptionOptions that
indicate whether audio playback should resume.
*/
- 播放聲音的時候注冊通知
// 注冊打斷通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AVAudioSessionInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:session];
- 實現方法
// 接收通知方法
- (void)AVAudioSessionInterruptionNotification: (NSNotification *)notificaiton {
NSLog(@"%@", notificaiton.userInfo);
}
打電話 測試 打印出 AVAudioSessionInterruptionTypeKey = 1
/* keys for AVAudioSessionInterruptionNotification */
/* value is an NSNumber representing an AVAudioSessionInterruptionType */
typedef NS_ENUM(NSUInteger, AVAudioSessionInterruptionType)
{
AVAudioSessionInterruptionTypeBegan = 1, /* the system has interrupted your audio session */
AVAudioSessionInterruptionTypeEnded = 0, /* the interruption has ended */
} NS_AVAILABLE_IOS(6_0);
- 可以通過通知信息中的
type
來做不同的操作
// 接收通知方法
- (void)AVAudioSessionInterruptionNotification: (NSNotification *)notificaiton {
NSLog(@"%@", notificaiton.userInfo);
AVAudioSessionInterruptionType type = [notificaiton.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
if (type == AVAudioSessionInterruptionTypeBegan) {
[self.player pause];
} else {
[self.player play];
}
}
這樣 當有電話打進來 就會停止播放,掛斷電話繼續播放