前言
在语音时,如果当前用户正在播放音乐之类的,那么我们必须要中断之,在用户语音完成时,又要通知其中断已完成,以便恢复之前的音乐播放等。看代码吧:
下面的代码是中断处理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
if
([[[UIDevice
currentDevice]
systemVersion]
compare:@"7.0"]
!=
NSOrderedAscending)
{
__block
BOOL bCanRecord
=
NO;
AVAudioSession *audioSession
=
[AVAudioSession
sharedInstance];
if
([audioSession
respondsToSelector:@selector(requestRecordPermission:)])
{
[audioSession
performSelector:@selector(requestRecordPermission:)
withObject:^(BOOL
granted)
{
if
(granted)
{
bCanRecord
=
YES;
}
else
{
bCanRecord
=
NO;
dispatch_async(dispatch_get_main_queue(),
^{
[[[UIAlertView
alloc]
initWithTitle:nil
message :@"需要访问您的麦克风。\n请启用麦克风-设置/隐私/麦克风"
delegate :nil
cancelButtonTitle :@"关闭"
otherButtonTitles :nil]
show];
});
}
}];
}
if
(!bCanRecord)
{
return;
}
}
|
当我们语音完毕之后,我们需要通知中断已经完成,以便恢复原始状态,该播放音乐就播放:
1
2
3
4
5
|
[[AVAudioSession
sharedInstance]
setActive:NO
withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
error:nil];
|