一、MediaRecord (录音):
1)、在AndroidManifest.xml中添加下述权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
2)、手动获取麦克风权限
private void initPermission(Context context, Activity activity) {
int permission = ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO);
if (permission != PackageManager.PERMISSION_GRANTED) {
//请求权限
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.RECORD_AUDIO}, 1);
}
}
代码:
MediaRecorder mr = new MediaRecorder();
mr.setAudioSource(MediaRecorder.AudioSource.MIC); //音频输入源
mr.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB); //设置输出格式
mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB); //设置编码格式
mr.setOutputFile(soundFile.getAbsolutePath());
try {
mr.prepare();
mr.start(); //开始录制
} catch (IOException e) {
e.printStackTrace();
}
mr.stop();
mr.release();