参考: //https://www.jb51.net/article/148641.htm
1、ios中判断录音权限:
#import
@interface Permission:NSObject
@end
Permission.h
#import "Permission.h"
#import
#import
#import
//#import
//#import
@implementation Permission
Permission.mm
//首次调用获取麦克风权限
extern "C" void GetMicrophonePermission()
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
/*if (granted) {
//NSLog(@"%@",@"麦克风准许xxxxxxxxx");
}else{
//NSLog(@"%@",@"麦克风不准许xxxxxxxxx");
}*/
}];
}
//获取麦克风权限准许状态
extern "C" int GetMicrophonePermissionState()
{
GetMicrophonePermission();
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
return (int)status;
}
/*
//首次调用获取相机权限
extern "C" void GetCameraPermission()
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
NSLog(@"%@",@"相机准许");
}else{
NSLog(@"%@",@"相机不准许");
}
}];
}
//获取相机权限准许状态
extern "C" bool GetCameraPermissionState()
{
GetCameraPermission();
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status == AVAuthorizationStatusAuthorized){
NSLog(@"%@",@"相机准许");
}else{
NSLog(@"%@",@"相机不准许");
}
return (status == AVAuthorizationStatusAuthorized);
}
//首次调用获取相册权限
extern "C" void GetPhotoPermission()
{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
NSLog(@"%@",@"相册准许");
}else{
NSLog(@"%@",@"相册不准许");
}
}];
}
//获取相册权限准许状态
extern "C" bool GetPhotoPermissionState()
{
GetPhotoPermission();
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusAuthorized) {
NSLog(@"%@",@"相册准许");
}else{
NSLog(@"%@",@"相册不准许");
}
return (status == PHAuthorizationStatusAuthorized);
}
*/
//打开本程序的设置
extern "C" void OpenAppSettings()
{
NSURL*url=[NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication]canOpenURL:url]){
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
//设备系统为IOS 10.0或者以上的
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}else{
//设备系统为IOS 10.0以下的
[[UIApplication sharedApplication]openURL:url];
}
}
}
@end
unity中调用:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class PermissionUtils
{
public static PermissionState GetMicrophonePermissionState()
{
PermissionState permissionState = PermissionState.NotAllow;
if(Application.platform == RuntimePlatform.Android)
{
// to do ...
}
else if(Application.platform == RuntimePlatform.IPhonePlayer)
{
int state = UnityGetiOSPermissionsState.GetMicrophonePermissionState();
permissionState = (PermissionState)state;
}
else
{
permissionState = PermissionState.UnnecessaryPlatform;
}
//#if UNITY_EDITOR || UNITY_STANDALONE_WIN
// permissionState = PermissionState.UnnecessaryPlatform;
//#elif UNITY_IOS || UNITY_IPHONE
// //Debug.LogError("a = " + a);
//#elif UNITY_ANDROID
// //AndroidRuntimePermissions.Permission result = AndroidRuntimePermissions.RequestPermission("android.permission.RECORD_AUDIO");
// //switch (result)
// //{
// // case AndroidRuntimePermissions.Permission.Denied://权限被拒绝且不再询问
// // permissionState = PermissionState.NotAllow;
// // break;
// // case AndroidRuntimePermissions.Permission.Granted://允许
// // permissionState = PermissionState.Allow;
// // break;
// // case AndroidRuntimePermissions.Permission.ShouldAsk://拒绝权限但不拒绝询问
// // permissionState = PermissionState.WaitAsk;
// // break;
// //}
//#endif
return permissionState;
}
/*
public static PermissionState GetCameraPermissionState()
{
PermissionState permissionState = PermissionState.NotAllow;
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
permissionState = PermissionState.UnnecessaryPlatform;
#elif UNITY_IOS || UNITY_IPHONE
permissionState = UnityGetiOSPermissionsState.GetCameraPermissionState()?PermissionState.Allow:PermissionState.NotAllow;
#elif UNITY_ANDROID
AndroidRuntimePermissions.Permission result = AndroidRuntimePermissions.RequestPermission("android.permission.CAMERA");
switch (result)
{
case AndroidRuntimePermissions.Permission.Denied://权限被拒绝且不再询问
permissionState = PermissionState.NotAllow;
break;
case AndroidRuntimePermissions.Permission.Granted://允许
permissionState = PermissionState.Allow;
break;
case AndroidRuntimePermissions.Permission.ShouldAsk://拒绝权限但不拒绝询问
permissionState = PermissionState.WaitAsk;
break;
}
#endif
return permissionState;
}
public static PermissionState GetWritePhotoPermissionState()
{
PermissionState permissionState = PermissionState.NotAllow;
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
permissionState = PermissionState.UnnecessaryPlatform;
#elif UNITY_IOS || UNITY_IPHONE
permissionState = UnityGetiOSPermissionsState.GetPhotoPermissionState()?PermissionState.Allow:PermissionState.NotAllow;
#elif UNITY_ANDROID
AndroidRuntimePermissions.Permission result = AndroidRuntimePermissions.RequestPermission("android.permission.WRITE_EXTERNAL_STORAGE");
switch (result)
{
case AndroidRuntimePermissions.Permission.Denied://权限被拒绝且不再询问
permissionState = PermissionState.NotAllow;
break;
case AndroidRuntimePermissions.Permission.Granted://允许
permissionState = PermissionState.Allow;
break;
case AndroidRuntimePermissions.Permission.ShouldAsk://拒绝权限但不拒绝询问
permissionState = PermissionState.WaitAsk;
break;
}
#endif
return permissionState;
}
public static PermissionState GetReadPhotoPermissionState()
{
PermissionState permissionState = PermissionState.NotAllow;
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
permissionState = PermissionState.UnnecessaryPlatform;
#elif UNITY_IOS || UNITY_IPHONE
permissionState = UnityGetiOSPermissionsState.GetPhotoPermissionState()?PermissionState.Allow:PermissionState.NotAllow;
#elif UNITY_ANDROID
AndroidRuntimePermissions.Permission result = AndroidRuntimePermissions.RequestPermission("android.permission.READ_EXTERNAL_STORAGE");
switch (result)
{
case AndroidRuntimePermissions.Permission.Denied://权限被拒绝且不再询问
permissionState = PermissionState.NotAllow;
break;
case AndroidRuntimePermissions.Permission.Granted://允许
permissionState = PermissionState.Allow;
break;
case AndroidRuntimePermissions.Permission.ShouldAsk://拒绝权限但不拒绝询问
permissionState = PermissionState.WaitAsk;
break;
}
#endif
return permissionState;
}
*/
///
/// 打开App设置界面
///
/// 权限状态
public static void OpenAppSettings()
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
#elif UNITY_IOS || UNITY_IPHONE
UnityGetiOSPermissionsState.OpenAppSettings();
#elif UNITY_ANDROID
//AndroidRuntimePermissions.OpenSettings();
#endif
}
}
public enum PermissionState
{
WaitAsk = 0,
Restricted,
NotAllow,
Allow,
UnnecessaryPlatform
}
using System.Runtime.InteropServices;
using UnityEngine;
//参考: https://www.jianshu.com/p/b74b8e388265
///
/// iOS权限静态类
///
public class UnityGetiOSPermissionsState
{
///
/// 首次调用获取麦克风权限
///
[DllImport("__Internal")]
public static extern void GetMicrophonePermission();
///
/// 获取麦克风权限准许状态
///
[DllImport("__Internal")]
public static extern int GetMicrophonePermissionState();
/*
///
/// 首次调用获取相机权限
///
[DllImport("__Internal")]
public static extern void GetCameraPermission();
///
/// 获取相机权限准许状态
///
[DllImport("__Internal")]
public static extern bool GetCameraPermissionState();
///
/// 首次调用获取相册权限
///
[DllImport("__Internal")]
public static extern void GetPhotoPermission();
///
/// 获取相册权限准许状态
///
[DllImport("__Internal")]
public static extern bool GetPhotoPermissionState();
*/
///
/// 打开本程序的设置
///
[DllImport("__Internal")]
public static extern void OpenAppSettings();
}
2、android平台:
public class MainActivity2 extends UnityPlayerActivity
{
public XiaomingInterface xiaomingInterface;
private final Rect windowVisibleDisplayFrame = new Rect();
public int Add(int a, int b)
{
return a+b;
}
public void CheckCamera()
{
/*
这个会报错
if (PermissionChecker.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
//ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA }, PERMISSION_REQUEST);
Log.e("CheckCamera", "CheckCamera is not allowed");
}
else
{
Log.e("CheckCamera", "CheckCamera is allowed");
}*/
PackageManager packageManager = this.getPackageManager();
String packageName = this.getPackageName();
int per = packageManager.checkPermission(Manifest.permission.RECORD_AUDIO, packageName);
Log.e("CheckCamera", "CheckCamera is allowed"+per);
if(per == PackageManager.PERMISSION_GRANTED){
Log.e("CheckCamera", "CheckCamera is allowed");
}
else
{
Log.e("CheckCamera", "CheckCamera is not allowed");
//this.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA);
this.requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, 0);
Log.e("CheckCamera", "CheckCamera xxxx");
}
}
public float GetKeyboardHeight()
{
// 获取屏幕可视区域
getWindow().getDecorView().getWindowVisibleDisplayFrame(windowVisibleDisplayFrame);
// 获取键盘高度
int currentKeyboardHeight = getWindow().getDecorView().getHeight() - windowVisibleDisplayFrame.bottom;
Log.d("GetKeyboardHeight", "GetKeyboardHeight: "+currentKeyboardHeight);
return currentKeyboardHeight;
}
}