Android AudioRecord vs. MediaRecorder for recording audio

时光总嘲笑我的痴心妄想 提交于 2021-02-17 07:32:16

问题


I want to record human voice on my Android phone. I noticed that Android has two classes to do this: AudioRecord and MediaRecorder. Can someone tell me what's the difference between the two and what are appropriate use cases for each?

I want to be able to analyse human speech in real-time to measure amplitude, etc. Am I correct in understanding that AudioRecord is better suited for this task?

I noticed on the official Android guide webpage for recording audio, they use MediaRecorder with no mention of AudioRecord.


回答1:


If you want to do your analysis while recording is still in progress, you need to use AudioRecord, as MediaRecorder automatically records into a file. AudioRecord has the disadvantage, that after calling startRecording() you need to poll the data yourself from the AudioRecord instance. Also, you must read and process the data fast enough such that the internal buffer is not overrun (look in the logcat output, AudioRecord will tell you when that happens).




回答2:


As I understand MediaRecorder is a black box which gives compressed audio file on the output and AudioRecorder gives you just raw sound stream and you have to compress it by yourself.

MediaRecorder gives you the max amptitude from last call of getMaxAmplitude() method so you can implement a sound visualizer for example.

So in most cases MediaRecorder is the best choice except those in which you should make some complicated sound processing and you need access to the raw audio stream.




回答3:


AudioRecorderer first saves data in minBuffer then it is copied from there to the temporary buffer, in MediaRecorder it is copied to files. In AudioRecorder we need the api setRecordPosition() to copy the saved data at required position, whereas in MediaRecorder the file pointer is does this job to set the position of the marker. AudioRecorder can be used to those apps which run on an emulator this can be done by providing low sample rate such as 8000, while using MediaRecorder the audio cannot be recorded using emulator. In AudioRecord the screen sleep after sometime, while in MediaRecorder the screen does not sleep.



来源:https://stackoverflow.com/questions/5886872/android-audiorecord-vs-mediarecorder-for-recording-audio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!