gpt4 book ai didi

android - 如何知道我的应用程序的 AudioRecord 权限是否被禁止

转载 作者:行者123 更新时间:2023-12-02 03:32:52 24 4
gpt4 key购买 nike

我的应用程序的录音权限被用户禁止。问题是一些 android 操作系统设备 `audio_recorder.getRecordingState()` 总是返回 `AudioRecord.RECORDSTATE_RECORDING`。这不是正确的结果。

如何知道录音权限可用?

public final class MtAudioRecord {

private static final String TAG = "audioRecord";
private static MtAudioRecord instance = null;
public static int SAMPLE_RATE = 44100;// default 44100
public static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO;
public static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
public static final int AUDIO_SOURCE = MediaRecorder.AudioSource.MIC;

private AudioRecord mAudioRecord;
private final int minBufferSize;
private boolean isRecording = true;
private static short[] audioData = null;

public static final MtAudioRecord getInstance() {
if (null == instance) {
instance = new MtAudioRecord();
}
return instance;
}

public MtAudioRecord() {
minBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT);
if (minBufferSize == AudioRecord.ERROR_BAD_VALUE) {
} else if (minBufferSize == AudioRecord.ERROR) {
} else {
mAudioRecord =
new AudioRecord(AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBufferSize);
}
audioData = new short[minBufferSize];
}

public synchronized void start() {

new Thread(new Runnable() {

@Override
public void run() {
if(null != mAudioRecord){
if (mAudioRecord.getRecordingState() != AudioRecord.STATE_INITIALIZED) {
Log.e(TAG, "Mic didn't successfully initialized");
return;
}
mAudioRecord.startRecording();
Log.e(TAG, "recording state = " + mAudioRecord.getRecordingState());
if (mAudioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
Log.e(TAG, "Mic is in use and can't be accessed");
return;
}
isRecording = true;
while (isRecording && null != mAudioRecord) {
if (null == audioData) {
audioData = new short[minBufferSize];
}
final int read_distance = mAudioRecord.read(audioData, 0, minBufferSize);
if (read_distance == AudioRecord.ERROR_INVALID_OPERATION || read_distance == AudioRecord.ERROR || read_distance == AudioRecord.ERROR_BAD_VALUE) {
isRecording = false;
break;
}
}
if (mAudioRecord != null) {
mAudioRecord.stop();
mAudioRecord.release();
mAudioRecord = null;
}
}
}
}).start();
}

public synchronized void stop() {
isRecording = false;
}

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

最佳答案

我认为您应该改用 AudioManager。这有助于您检查麦克风是否已被用户静音。您可能搜索的方法是 isMicrophoneMute(),它返回 bool 值。

这是 Android 开发者网站中 AudioManager 的链接。 Android Audio Manager

函数描述为here

你的 toast 不正确,应该是:

Toast.makeText(applicationContext(),"The microphone is 
disabled",Toast.LENGTH_SHORT).show();

更新: 你可以使用 Context.checkCallingOrSelfPermission(String permission);

permission 是描述你需要的权限的字符串:android.permission.RECORD_AUDIO

它应该返回 PERMISSION_GRANTED 或 PERMISSION_DENIED 的整数结果

在此处查看 android 开发者网站中的描述:check permission

例如:

public boolean checkRecordingPermission()
{
String permission = "android.permission.RECORD_AUDIO";
int result = getContext().checkCallingOrSelfPermission(permission);
return (result == PackageManager.PERMISSION_GRANTED);
}

关于android - 如何知道我的应用程序的 AudioRecord 权限是否被禁止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25640326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com