【问题标题】:How do i request mic record permission from user我如何向用户请求麦克风录音权限
【发布时间】:2013-06-09 12:35:48
【问题描述】:

我正在使用新的 iOS7 开发人员 SDK,现在当应用程序第一次尝试录制时,用户请求他允许从麦克风录制。

我的应用程序会在倒计时后记录,所以用户看不到这个请求。 我用这段代码检查requestRecordPermission:

[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
            if (granted) {
                // Microphone enabled code
            }
            else {
                // Microphone disabled code
            }
        }];

但是在我开始录制之前,我怎样才能自己触发请求呢?

【问题讨论】:

  • @OneManCrew 这个问题似乎没有显示出任何解决问题的努力,它基本上是在要求一个教程。 (因此在接近的原因中“过于宽泛”)

标签: objective-c ios7 xcode5


【解决方案1】:

在新的iOS7中很简单试试这个:

if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission)])
{
    [[AVAudioSession sharedInstance] requestRecordPermission];
}

【讨论】:

  • 实际上在 iOS7 中这个函数需要使用块,可以像这样调用:[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL response){ NSLog(@"允许麦克风使用响应:%d",回复); }];
  • 此外,您可能需要在上述代码 #endif 周围加上 #ifdef __IPHONE_7_0 才能在旧版本的 xcode 中编译它。
  • Apple 的 PrivacyPrompts 示例应用程序使用 requestRecordPermission 不起作用。什么给了?
  • 你能检查一下这个问题吗? stackoverflow.com/questions/18957643/… 并提供必要的信息,谢谢
【解决方案2】:

正如“One Man Crew”所说,您可以使用 requestRecordPermission。

需要注意的重要一点是,您必须检查 requestRecordPermission 是否已实现。原因是如果您的应用程序将在较旧的 iOS 版本(例如 iOS 6.x)上运行,它会在此调用后崩溃。 为防止这种情况发生,您必须检查此选择器是否已实现(无论如何这是一个好习惯)。

代码应该是这样的:

if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]){
    [[AVAudioSession sharedInstance] requestRecordPermission];
}

使用这种方法,您的应用将支持新操作系统以及之前版本的操作系统。

每次 Apple 向新操作系统添加更多功能时,我都会使用这种方法(这样我可以很容易地支持旧版本)。

【讨论】:

  • 这很简单!顺便说一句,如果您在早期的 iO​​S 版本上使用此方法,该方法将不会执行任何操作,因为该方法是 ios7 sdk 的一部分。
  • 在以前的 SDK 中使用这种方法是不合理的,因为你不会编译到 iOS 7 并且你与它无关。但是,如果您使用新的 SDK 并针对旧设备(例如 iOS 6.x),如果您不使用此检查,您的应用程序肯定会崩溃!所以,请注意使用这个超级重要的检查。每次 Apple 添加新功能时都会发生这种情况。如果是另一种方式(Apple 删除功能),您的应用不会崩溃,您只会收到该功能已弃用的警告(不会崩溃)。
  • 你大错特错!构建应用程序的SDK包与设备上安装的操作系统之间没有直接联系。您可以使用IOS7的SDK构建完整的应用程序,它将在旧操作系统上运行。!!
  • 我想你误解了我的意思。简单来说,如果您在 7.x 之前的 SDK 上使用 requestRecordPermission,您的应用程序将无法编译(Xcode 5 之前的任何 Xcode)。如果您使用 SDK 7.x (Xcode 5.x) 并且未对答案进行检查,您的应用程序将在 7.x 之前的任何 iOS 上崩溃(iOS 5.x 或 iOS 6.x)希望更清楚现在。
  • 我试过了,我没有在iPhone 4S iOS 6.1上实现检查并且应用程序没有崩溃,我认为你错了。
【解决方案3】:

这是对我有用的最终代码 sn-p。它同时支持 Xcode 4 和 5,并且适用于 iOS5+。

#ifndef __IPHONE_7_0
    typedef void (^PermissionBlock)(BOOL granted);
#endif

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

    // iOS7+
    if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)])
    {
        [[AVAudioSession sharedInstance] performSelector:@selector(requestRecordPermission:)
                                              withObject:permissionBlock];
    }
    else
    {
        [self doActualRecording];
    }

【讨论】:

    【解决方案4】:
        AVAudioSession.sharedInstance().requestRecordPermission({ (granted) -> Void in
            if !granted
            {
                let microphoneAccessAlert = UIAlertController(title: NSLocalizedString("recording_mic_access",comment:""), message: NSLocalizedString("recording_mic_access_message",comment:""), preferredStyle: UIAlertControllerStyle.Alert)
    
                var okAction = UIAlertAction(title: NSLocalizedString("OK",comment:""), style: UIAlertActionStyle.Default, handler: { (alert: UIAlertAction!) -> Void in
                    UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
                })
    
    
                var cancelAction = UIAlertAction(title: NSLocalizedString("Cancel",comment:""), style: UIAlertActionStyle.Cancel, handler: { (alert: UIAlertAction!) -> Void in
    
                })
                microphoneAccessAlert.addAction(okAction)
                microphoneAccessAlert.addAction(cancelAction)
                self.presentViewController(microphoneAccessAlert, animated: true, completion: nil)
                return
            }
            self.performSegueWithIdentifier("segueNewRecording", sender: nil)
        });
    

    【讨论】:

      【解决方案5】:

      基于https://stackoverflow.com/users/1071887/idan 的回复。

      AVAudioSession *session = [AVAudioSession sharedInstance];
      
      // AZ DEBUG @@ iOS 7+
      AVAudioSessionRecordPermission sessionRecordPermission = [session recordPermission];
      switch (sessionRecordPermission) {
          case AVAudioSessionRecordPermissionUndetermined:
              NSLog(@"Mic permission indeterminate. Call method for indeterminate stuff.");
              break;
          case AVAudioSessionRecordPermissionDenied:
              NSLog(@"Mic permission denied. Call method for denied stuff.");
              break;
          case AVAudioSessionRecordPermissionGranted:
              NSLog(@"Mic permission granted.  Call method for granted stuff.");
              break;
          default:
              break;
      }
      

      【讨论】:

        【解决方案6】:

        斯威夫特 4:

        let session = AVAudioSession.sharedInstance()
        if (session.responds(to: #selector(AVAudioSession.requestRecordPermission(_:)))) {
            AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
                if granted {
                    print("granted")
                } else {
                    print("not granted")
                }
            })
        }
        

        【讨论】:

          猜你喜欢
          • 2021-01-26
          • 2022-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-23
          相关资源
          最近更新 更多