Xcode中plist文件中需要配置:
//向用户询问授权
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
[audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
// NSLog(@"%i",granted);
// bCanRecord = granted;
}];
}
//另一种方式向用户询问授权
// Explicit user permission is required for media capture, but the user has not yet granted or denied such permission.
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
if(granted){//点击允许访问时调用
//用户明确许可与否,媒体需要捕获,但用户尚未授予或拒绝许可。
NSLog(@"Granted access to %@", mediaType);
}
else {
NSLog(@"Not granted access to %@", mediaType);
}
}];
int CanRecord(){
int bCanRecord = -1;
/*AVMediaTypeVideo//相机
AVMediaTypeAudio//麦克风
*/
//获取当前权限状态
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
switch (authStatus) {
case AVAuthorizationStatusNotDetermined://尚未询问过用户是否授权
{
NSLog(@"AVAuthorizationStatusNotDetermined");
}
break;
case AVAuthorizationStatusDenied://不授权
NSLog(@"AVAuthorizationStatusDenied");
bCanRecord = 0;
break;
case AVAuthorizationStatusAuthorized://已授权
NSLog(@"AVAuthorizationStatusAuthorized");
bCanRecord = 1;
break;
case AVAuthorizationStatusRestricted://限制
NSLog(@"AVAuthorizationStatusRestricted");
bCanRecord = 0;
break;
default:
NSLog(@"default");
bCanRecord = 0;
break;
}
return bCanRecord;
}