import android.media.AudioRecord; //导入方法依赖的package包/类
/**
* 判断是是否有录音权限
*/
public static boolean isCanUseAudio(Activity activity) {
/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){//6.0以上系统
if (ContextCompat.checkSelfPermission(activity,Manifest.permission.RECORD_AUDIO)!= PackageManager.PERMISSION_GRANTED) {
return false;
}else{
return true;
}
}else {//6.0以下系统*/
// 音频获取源
int audioSource = MediaRecorder.AudioSource.MIC;
// 设置音频采样率,44100是目前的标准,但是某些设备仍然支持22050,16000,11025
int sampleRateInHz = 44100;
// 设置音频的录制的声道CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道
int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
// 音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
// 缓冲区字节大小
int bufferSizeInBytes;
bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,
channelConfig, audioFormat);
AudioRecord audioRecord = new AudioRecord(audioSource, sampleRateInHz,
channelConfig, audioFormat, bufferSizeInBytes);
//开始录制音频
try {
// 防止某些手机崩溃,例如联想
audioRecord.startRecording();
} catch (IllegalStateException e) {
e.printStackTrace();
}
//根据开始录音判断是否有录音权限
if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
// context.startActivity(new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS));
return false;
} else {
audioRecord.stop();
audioRecord.release();
return true;
}
// }
}