How do i request mic record permission from user

2020-02-17 09:31发布

问题:

I am using the new iOS7 developer SDK and now the app request from the user his permission to record from mic when the App try to record in the first time.

My App will record after a countdown,so the user can't see this request. I use this code to check the requestRecordPermission:

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

But how can i trigger the request by myself before i start to record ?

回答1:

In the new iOS7 it's very simple try this:

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


回答2:

Here is final code snippet that does work for me. It support both Xcode 4 and 5, and works for 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];
    }


回答3:

As "One Man Crew" claimed you can use requestRecordPermission.

Important thing to be aware of is that you must check that requestRecordPermission is implemented. The reason is that if your app would run on older iOS version (iOS 6.x for example) it would crash after this call. To prevent that you must check that this selector is implemented (this is a good practice anyway).

Code should be something like this:

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

Using this method your app would support the new OS and also previous versions of the OS.

I'm using this method every time Apple add more functionality to new OS (that way I can support older versions pretty easy).



回答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:

Based on https://stackoverflow.com/users/1071887/idan's response.

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:

Swift 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")
        }
    })
}