Use the class
private MediaRecorder mediaRecorder; private MediaPlayer mediaPlayer; / / generated local file name and path of the private String path = Environment. External.getexternalstoragedirectory () + "/ 1024 / fuck. Amr";Copy the code
Audio recording
/** * Start recording */ private void startRecordVoice() {if (mediaRecorder == null) {File File = new File(Environment.getExternalStorageDirectory(),"1024"); if (! file.exists()){ file.mkdirs(); } File soundsFile = new File(file,"fuck.amr"); if (! soundsFile.exists()){ try { soundsFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); / / audio input source mediaRecorder. SetOutputFormat (mediaRecorder. OutputFormat. AMR_WB); / / set the output format mediaRecorder. SetAudioEncoder (mediaRecorder. AudioEncoder. AMR_WB); / / set the coding format mediaRecorder. SetOutputFile (soundsFile. GetAbsolutePath ()); try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); }}}Copy the code
Stop the recording
private void stopRecord() {
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
}
Copy the code
Play and stop the recording
private void startPlaying() { mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(path); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); }}Copy the code
private void stopPlaying(){
mediaPlayer.release();
mediaPlayer=null;
}
Copy the code
Remove the tape
private void deleteRecord() { File file = new File(path); if (file.exists()) { file.delete(); }}Copy the code