如何在iOS 7中检测麦克风输入权限被拒绝

39

我希望能够检测到用户在我的iOS应用程序上拒绝麦克风权限。 只有在尝试录制麦克风时,我才能获得此值:-120.000000 db

但是,在获取这个值之前,我必须设置一个AVAudioSession。是否有另外的函数可以使用?

并且我在输出中得到了这条消息: Microphone input permission refused - will record only silence

谢谢。


可能是重复的问题:iOS检查应用程序是否有麦克风访问权限 - pkamb
4个回答

51

如果你仍在使用iOS SDK 6.0(就像我一样),你必须比@Luis E. Prado更加间接,因为requestRecordPermission方法不存在。

以下是我的做法。如果你使用ARC,请删除自动释放位。在iOS6上不会发生任何事情,在iOS7上将记录“麦克风已启用”消息或弹出警报。

AVAudioSession *session = [AVAudioSession sharedInstance];
if ([session respondsToSelector:@selector(requestRecordPermission:)]) {
    [session performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
        if (granted) {
            // Microphone enabled code
            NSLog(@"Microphone is enabled..");
        }
        else {
            // Microphone disabled code
            NSLog(@"Microphone is disabled..");

            // We're in a background thread here, so jump to main thread to do UI work.
            dispatch_async(dispatch_get_main_queue(), ^{
                [[[[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
                                        message:@"This app requires access to your device's Microphone.\n\nPlease enable Microphone access for this app in Settings / Privacy / Microphone"
                                       delegate:nil
                              cancelButtonTitle:@"Dismiss"
                              otherButtonTitles:nil] autorelease] show];
            });
        }
    }];
}

编辑: 原来发现withObject块在后台线程中执行,因此请勿在其中进行任何UI工作,否则您的应用程序可能会崩溃。我已经调整了上面的代码。客户在幸运的测试版中指出了这一点。对于错误表示歉意。


我在iPad Air上使用这个解决方案时遇到了问题。您知道是否存在一些针对这种设备的额外配置吗? - Beto
当我使用iPad Air内置麦克风录制文件时,文件会被创建,但是当我回放时没有声音,并且回放永远不会停止,这种情况只发生在这个设备上。这太奇怪了。 - Beto
1
我解决了这个问题。当在iPadAir上使用RecordSound时,我必须从设置会话中删除AVEncoderBitRateKey。我认为这可能与多个麦克风有关,因为我无法选择不同的麦克风来保存我的声音文件。 - Beto
@KyleJurick 这很奇怪,当我尝试时它明显在非 UI 线程中,并导致了一个严重的挂起。也许线程使用不同,或者在新的 iOS 版本中表现不同。 - Ben Clayton
1
@BenClayton 我们都知道,苹果喜欢在没有发布说明的情况下更改这样的事情 :) - Kyle Jurick
显示剩余2条评论

42
请注意,这仅适用于使用Xcode 5构建的项目,不适用于4.6。
将AVFoundation框架添加到您的项目中。
然后从AVFoundation框架中导入AVAudioSession头文件,在您打算检查麦克风设置是否启用的位置使用它。
#import <AVFoundation/AVAudioSession.h>

然后只需调用此方法

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

第一次运行该方法时,它将显示提示允许麦克风访问,并根据用户的响应执行完成块。从第二次开始,它将仅基于设备上存储的设置执行操作。


1
请注意,此方法仅适用于iOS7及以上版本。https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html#//apple_ref/occ/instm/AVAudioSession/requestRecordPermission: - Ben Clayton

3

快速回答:

if AVAudioSession.sharedInstance().recordPermission() == .Denied {
    print("Microphone permission refused");
}

或者您可以使用像PermissionScope这样的框架来轻松检查权限。

https://github.com/nickoneill/PermissionScope

编辑:Swift 3答案:

import AVFoundation
...
if AVAudioSession.sharedInstance().recordPermission() == .denied {
    print("Microphone permission refused");
}

0

我不确定我们是否可以在苹果的开发者论坛之外讨论iOS 7,但我在那里找到了你要找的答案

简而言之,在SDK中的AVAudioSession.h头文件中,您会找到解决方案。如果您想在仍支持iOS 6的情况下使用它,请务必使用“respondsToSelector:”来检查API的可用性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接