【问题标题】:How to check for microphone access at time of launch?如何在启动时检查麦克风访问权限?
【发布时间】:2014-01-15 16:18:38
【问题描述】:

在我的应用程序中,我将使用麦克风进行一些录音。从 iOS7.0 开始,在开始音频之前,会要求用户检查访问麦克风的权限。

我的应用中有一个“开始录制”按钮。这里首先检查用户的录制权限。

这是执行此操作的代码:

if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) {
  [[AVAudioSession sharedInstance] performSelector:@selector(requestRecordPermission:)
    withObject:permissionBlock];
}
#ifndef __IPHONE_7_0
  typedef void (^PermissionBlock)(BOOL granted);
#endif

PermissionBlock permissionBlock = ^(BOOL granted) {
  NSLog(@"permissionBlock");
  if (granted) {
    [self doActualRecording];
  } else {
    // Warn no access to microphone
  }
};

现在,我想要求用户在用户启动应用程序时授权使用麦克风。然后当用户选择录制按钮时,它会再次弹出消息。

定位服务也有类似的功能。我如何才能访问麦克风?

【问题讨论】:

    标签: objective-c ios7 microphone


    【解决方案1】:

    一旦用户拒绝了您应用的麦克风访问权限,您就无法再次向他们展示权限对话框。使用保存的设置。相反,您可以提示用户进入他们的设置并进行更改。

    [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
        if (granted) {
            NSLog(@"granted");
        } else {
            NSLog(@"denied");
    
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
                                                                message:@"You must allow microphone access in Settings > Privacy > Microphone"
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
           [alert show];
        }
    }];
    

    【讨论】:

    【解决方案2】:

    我也有同样的问题。请参阅this response 的相关问题,包括使用 AVAudioSession 检查和处理权限状态的示例代码,您可以对其进行自定义以提供所需的用户体验。

    【讨论】:

    • @Matt,@davidfer。谢谢..你的解决方案给了我一个想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    相关资源
    最近更新 更多