Android Audio Recording (1) - MediaRecord
Android provides us with three ways to record audio
- MediaRecord( Java API)
- AudioRecord( Java API)
- OpenSL ES( Native API)
Let's start with the simplest MediaRecord this time.
This system provides the simplest recording API. Do not care about coding, do not operate byte code, all data processing has been implemented inside, so the use is also the most convenient.
Of course, there are reasons and results, your reward is me. Easy to use, the consequence is poor scalability.
(GOG: I mean, I think it's perfect.)
Don't talk about it. It's exposed directly. The comments in the code are already marked.
/** * Author silence. * Time: 2019-09-25. * Desc: Aac Audio format recording */ public class AacRecord { private String recordFilePath = applicationContext.getExternalFilesDir("pcm") + "/record.aac"; private MediaRecorder mediaRecorder = new MediaRecorder(); public AacRecord(){ //Configuration collection mode, here is the microphone collection mode. mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //Configure the output mode. MP4 is used here. mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS); //Configure the sampling frequency. The higher the frequency, the closer to the original sound. The sampling frequency supported by all Android devices is 44100. mediaRecorder.setAudioSamplingRate(44100); //The encoding format of the configuration file, AAC is a more general encoding format. mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); //Configure the bit rate. The general purpose here is 96000. mediaRecorder.setAudioEncodingBitRate(96000); //Configure the location of the recording file mediaRecorder.setOutputFile(recordFilePath); } public void start(){ try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } } public void stop(){ mediaRecorder.stop(); } }
Calling mode
AacRecord aacRecord = new AacRecord(); //start recording aacRecord.start(); //stop recording aacRecord.stop();