没有授权之前是不能进行操作摄像头的,使用之前先弹出系统的
AVCaptureDevice *captureDevice=[self getCameraDeviceWithPosition:AVCaptureDevicePositionBack];//取得后置摄像头
if (!captureDevice) {
// NSLog(@"取得后置摄像头时出现问题.");
}
NSError *error=nil;
//根据输入设备初始化设备输入对象,用于获得输入数据
AVCaptureDeviceInput *captureDeviceInput=[[AVCaptureDeviceInput alloc]initWithDevice:captureDevice error:&error];
if (error) {
}
然后实际上在弹出自己的
// 检测麦克风功能是否打开
[[AVAudioSession sharedInstance]requestRecordPermission:^(BOOL granted) {
if (!granted)
{
[self showAlertView:@"热波间需要访问你的麦克风" message:@"使用手机直播,热波间需要访问你的麦克风权限。点击“设置”前往系统设置允许热波间访问你的麦克风" confirm:^(id sender) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
[self stopPlayingautoExitRoom];
} cancel:^(id sender) {
[self stopPlayingautoExitRoom];
}];
return ;
}else{
}
}];
//检测摄像头功能是否打开
NSString *mediaType1 = AVMediaTypeVideo;
AVAuthorizationStatus authStatus1 = [AVCaptureDevice authorizationStatusForMediaType:mediaType1];
if (authStatus1 == 0) {
return;
}
if(authStatus1 == AVAuthorizationStatusRestricted || authStatus1 == AVAuthorizationStatusDenied ){
[self showAlertView:@"热波间需要访问你的相机" message:@"使用手机直播,热波间需要访问你的相机权限。点击“设置”前往系统设置允许热波间访问你的相机" confirm:^(id sender) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
[self stopPlayingautoExitRoom];
} cancel:^(id sender) {
[self stopPlayingautoExitRoom];
}];
return;
}
对以上做了修改
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus ==AVAuthorizationStatusRestricted){//此应用程序没有被授权访问的照片数据。可能是家长控制权限。
}else if(authStatus == AVAuthorizationStatusDenied){// 用户已经明确否认了这一照片数据的应用程序访问
[self showAlertView:@"热波间需要访问你的相机" message:@"使用手机直播,热波间需要访问你的相机权限。点击“设置”前往系统设置允许热波间访问你的相机" confirm:^(id sender) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
} cancel:^(id sender) {
}];
return;
}
else if(authStatus == AVAuthorizationStatusAuthorized){//用户已授权应用访问照片数据.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
picker.mediaTypes = @[(NSString *) kUTTypeImage];
[self presentViewController:picker animated:YES completion:nil];
}else if(authStatus == AVAuthorizationStatusNotDetermined){// 用户尚未做出了选择这个应用程序的问候.一次一出现
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {//弹出授权弹框
if(granted){//点击允许访问时调用
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
picker.mediaTypes = @[(NSString *) kUTTypeImage];
[self presentViewController:picker animated:YES completion:nil];
}
else {
return ;
}
}];
}else {
NSLog(@"Unknown authorization status");
}