我想允许访问iOS设备的麦克风。项目在Angular JS 1中,并且Android团队编写了本机代码来访问麦克风,效果很好。以下是Android的本地代码
iOS的等效代码是什么?任何帮助将不胜感激!
答案 0 :(得分:1)
首先,在info.plist中添加“隐私-麦克风使用说明”属性。 比添加以下代码==>
fow swift =>
import AVFoundation
switch AVAudioSession.sharedInstance().recordPermission() {
case AVAudioSessionRecordPermission.granted:
print("Permission granted")
case AVAudioSessionRecordPermission.denied:
print("Pemission denied")
case AVAudioSessionRecordPermission.undetermined:
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
// Handle granted
})
}
对于Objective-C ==>
#include <AVFoundation/AVFoundation.h>
-(void)askForMicrophonePermission {
switch ([[AVAudioSession sharedInstance] recordPermission]) {
case AVAudioSessionRecordPermissionGranted:
break;
case AVAudioSessionRecordPermissionDenied:
break;
case AVAudioSessionRecordPermissionUndetermined:
// This is the initial state before a user has made any choice
// You can use this spot to request permission here if you want
[[AVAudioSession sharedInstance]requestRecordPermission:^(BOOL granted) {
// Check for granted
}];
break;
default:
break;
}
}
让我知道您是否仍然无法做到。
答案 1 :(得分:1)
对于ios,您必须在info.plist文件中设置权限
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createCityForm(Model model) {
model.addAttribute("city", new City());
model.addAttribute("action", "create");
return CITY_FORM_PATH_NAME;
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String postCity(@ModelAttribute City city) {
cityService.saveCity(city);
return REDIRECT_TO_CITY_URL;
}
添加此权限后,在代码文件中检查麦克风的当前状态
<key>NSMicrophoneUsageDescription</key>
<string>${PRODUCT_NAME} always microphone use</string>
答案 2 :(得分:1)
您无需明确请求使用麦克风的许可。首次尝试使用音频输入时,应用程序会自动执行此操作。前提条件是您的NSMicrophoneUsageDescription
中有info.plist
,否则应用程序将崩溃。
或者,您可以通过调用requestRecordPermission
的{{1}}方法来请求许可,但这不是必须的,并且根据Human Interface Guidelines,您应该仅请求使用个人数据当您的应用明确需要它时。