我如何向用户请求麦克风记录许可

时间:2013-06-13 07:42:53

标签: objective-c ios7 xcode5

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

我的应用程序将在倒计时后记录,因此用户无法看到此请求。 我使用此代码检查requestRecordPermission

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

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

6 个答案:

答案 0 :(得分:27)

在新的iOS7中,尝试这个非常简单:

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

答案 1 :(得分:11)

以下是适用于我的最终代码段。它支持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];
    }

答案 2 :(得分:7)

正如“One Man Crew”声称你可以使用requestRecordPermission。

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

代码应该是这样的:

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

使用此方法,您的应用程序将支持新操作系统以及以前版本的操作系统。

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

答案 3 :(得分:3)

    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)
    });

答案 4 :(得分:2)

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

<div class="col-md-12">    
    <div class="tab-content">
        <div class="tab-pane active cont" id="we">
            <span style="float: right; padding: 4px 6px;"><a href='javascript:history.go(-1)'>Go Back to Previous Page</a> </span>
            <span style="float: right; padding: 4px 6px;"><font color="red">*</font> Indicates required field</span>
            <div class="cl-mcont">
                <div class="row">
                    <div class="col-md-12">
                        <div class="block-flat">
                            <div class="page-head">
                                <h3>
                                        Bootstrap Template
                                    </h3>
                            </div>
                            <div class="content">
                                <form action="/xcel" class="form-horizontal data-parsley-validate" id="fForm" method="post">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Category</label>
                                            <div class="col-sm-6">
                                                <select class="select2" id="on" style="width:250px;">
                                                    <option value="Production">Production</option>
                                                    <option value="Production">Production</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Tier</label>
                                            <div class="col-sm-6">
                                                <select class="select2" id="tw" style="width:250px;">
                                                    <option value="Production">Production</option>
                                                    <option value="Production">Production</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Name</label>
                                            <div class="col-sm-6">
                                                <input class="form-control" id="te" placeholder="  Name" type="text" value="" />
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Code</label>
                                            <div class="col-sm-6">
                                                <input class="form-control" id="df" name="ClientCode" placeholder="  Code" type="text" value="" />
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Model</label>
                                            <div class="col-sm-6">
                                                <input class="form-control" id="adg" placeholder="  Model" type="text" value="" />
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Center</label>
                                            <div class="col-sm-6">
                                                <input class="form-control" id="fd" placeholder="  Center" type="text" value="" />
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Center</label>
                                            <div class="col-sm-6">
                                                <input class="form-control" id="sa" placeholder="  Center" type="text" value="" />
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Name</label>
                                            <div class="col-sm-6">
                                                <input class="form-control" id="aeh" placeholder="  Name" type="text" value="" />
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                CI </label>
                                            <div class="col-sm-6">
                                                <input class="form-control" id="he" placeholder="CI  " type="text" value="" />
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Production </label>
                                            <div class="col-sm-6">
                                                <input class="form-control" id="jg" placeholder="Production   IP" type="text" value="" />
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Action</label>
                                            <div class="col-sm-6">
                                                <select class="select2" id="lk" style="width:250px;">
                                                    <option value="Production">Production</option>
                                                    <option value="Production">Production</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Description</label>
                                            <div class="col-sm-6">
                                                <input class="form-control" id="ui" placeholder="  Description" type="text" value="" />
                                            </div>
                                        </div>
                                    </div>
                                    <div style='clear: both'>
                                    </div>
                                    <div style='clear: both'>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Physical </label>
                                            <div class="col-sm-6">
                                                <select class="select2" id="lj" style="width:250px;">
                                                    <option value="Production">Production</option>
                                                    <option value="Production">Production</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Read</label>
                                            <div class="col-sm-6">
                                                <input class="form-control" id="io" placeholder="  Read" type="text" value="" />
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Write</label>
                                            <div class="col-sm-6">
                                                <input class="form-control" id="po" placeholder="  Write" type="text" value="" />
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">
                                                Test</label>
                                            <div class="col-sm-6">
                                                <select class="select2" id=".j" style="width:265px;">
                                                    <option value="Production">Production</option>
                                                    <option value="Production">Production</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group"> </div>

                                </form>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

答案 5 :(得分:0)

迅速4:

import re

df['col'] = None

for i in range(len(df)):
    if df.loc[i, 'status'] == np.NaN:
         df.loc[i, 'col'] = df.loc[i, 'ID']
    else:
         df.loc[i, 'col'] = re.findall(r'\d+', df.loc[i, 'ID'])[0]