Android AudioRecord 与 MediaRecorder 录制音频
我想在我的 Android 手机上录制人声。我注意到 Android 有两个类可以执行此操作: AudioRecord 和 MediaRecorder。有人可以告诉我两者之间有什么区别以及两者的合适用例是什么?
我希望能够实时分析人类语音以测量幅度等。我是否正确理解 AudioRecord 更适合此任务?
我在官方 Android 录制音频的指南网页上注意到,他们使用 MediaRecorder没有提到 AudioRecord。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想在录制过程中进行分析,则需要使用
AudioRecord
,因为MediaRecorder
会自动录制到文件中。AudioRecord
的缺点是,在调用startRecording()
后,您需要自己从AudioRecord
实例中轮询数据。另外,您必须足够快地读取和处理数据,以便内部缓冲区不会溢出(查看 logcat 输出,AudioRecord
会告诉您何时发生这种情况)。If you want to do your analysis while recording is still in progress, you need to use
AudioRecord
, asMediaRecorder
automatically records into a file.AudioRecord
has the disadvantage, that after callingstartRecording()
you need to poll the data yourself from theAudioRecord
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).据我了解
MediaRecorder
是一个黑匣子,它在输出上提供压缩的音频文件,而AudioRecorder
只为您提供原始声音流,您必须自己压缩它。MediaRecorder
为您提供上次调用getMaxAmplitude()
方法的最大振幅,以便您可以实现声音可视化工具。因此,在大多数情况下,
MediaRecorder
是最佳选择,除非您需要进行一些复杂的声音处理并且需要访问原始音频流。As I understand
MediaRecorder
is a black box which gives compressed audio file on the output andAudioRecorder
gives you just raw sound stream and you have to compress it by yourself.MediaRecorder
gives you the max amptitude from last call ofgetMaxAmplitude()
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.AudioRecorderer 首先将数据保存在 minBuffer 中,然后将其从那里复制到临时缓冲区,在 MediaRecorder 中将其复制到文件。
在 AudioRecorder 中,我们需要 api setRecordPosition() 将保存的数据复制到所需位置,而在 MediaRecorder 中,文件指针完成此工作来设置标记的位置。
AudioRecorder 可用于在模拟器上运行的应用程序,这可以通过提供低采样率(例如 8000)来完成,而使用 MediaRecorder 时无法使用模拟器录制音频。
在 AudioRecord 中,屏幕会在一段时间后休眠,而在 MediaRecorder 中,屏幕不会休眠。
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.