1. apk触发录音启动或录音停止按键: public void onClick(View v) { switch (v.getId()) { case R.id.btn_Record: //启动录音 startRecording(); break; case R.id.btn_stop: //停止录音 stopRecording(); break; default: break; } } 2. 创建MediaRecorder对象并初始化,启动录音: private void startRecording() { recorder = new MediaRecorder(); // 创建MediaRecorder对象 recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setOutputFile(fileName); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { recorder.prepare(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() failed"); } // 启动录音 recorder.start(); } private void stopRecording() { recorder.stop(); recorder.release(); recorder = null; } @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); // 录音文件保存路径 fileName = getExternalCacheDir().getAbsolutePath(); fileName += "/audiorecordtest.3gp"; // 权限请求 ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION); } // 权限请求 private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200; // Requesting permission to RECORD_AUDIO private boolean permissionToRecordAccepted = false; private String [] permissions = {Manifest.permission.RECORD_AUDIO}; @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode){ case REQUEST_RECORD_AUDIO_PERMISSION: permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED; break; } if (!permissionToRecordAccepted ) finish(); } manifest xml权限声明: <uses-permission android:name="android.permission.RECORD_AUDIO" />
3. 按照,如上步骤进行配置,可通过MediapPlayer对象api拉起音频录音。