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

时间:2014-09-03 09:03:40

标签: android

我的应用程序的记录音频权限被用户禁止。问题是一些android os设备`audio_recorder.getRecordingState()`总是返回`AudioRecord.RECORDSTATE_RECORDING`。这不是正确的结果。< / p>

我如何知道录制音频权限是否可用?

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"/>

1 个答案:

答案 0 :(得分:1)

我认为您应该使用AudioManager代替。这有助于您检查用户是否将麦克风静音。您可能正在搜索的方法是isMicrophoneMute(),它返回布尔值。

以下是android开发者网站中AudioManager的链接。 Android Audio Manager

,功能说明为here

你的吐司不正确,应该是:

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