Pengfei Zeng
Pengfei Zeng

Reputation: 31

how to know my app's AudioRecord permission is disallowed or not

My app's record audio permission was disallowed by user.The problem is some android os devices `audio_recorder.getRecordingState()` always returns `AudioRecord.RECORDSTATE_RECORDING`.It's not a right result.

How can I know the record audio permission is available ?

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

Upvotes: 3

Views: 2403

Answers (1)

Elior
Elior

Reputation: 3266

I think you should use the AudioManager instead. This helps you to check if the microphone was muted by the user. The method you're probably search is isMicrophoneMute() which returns boolean.

Here is the link for the AudioManager in android developer site. Android Audio Manager

and the function description is here

and your toast is incorrect, it should be :

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

Update: you can use Context.checkCallingOrSelfPermission(String permission);

permission is the string that describes the permission you need: android.permission.RECORD_AUDIO

it should return integer result which is PERMISSION_GRANTED or PERMISSION_DENIED

look here for the description in android developers site : check permission

for example :

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

Upvotes: 1

Related Questions