Android:需要录制麦克风输入
有没有办法在实时播放/预览过程中记录 Android 中的麦克风输入?我尝试使用 AudioRecord
和 AudioTrack
来执行此操作,但问题是我的设备无法播放录制的音频文件。实际上,任何Android播放器应用程序都无法播放录制的音频文件。
另一方面,使用Media.Recorder
进行录音会生成一个录制好的音频文件,可以由任何播放器应用程序播放。但问题是我无法在实时录制麦克风输入时进行预览/回放。
Is there a way to record mic input in android while it is being process for playback/preview in real time? I tried to use AudioRecord
and AudioTrack
to do this but the problem is that my device cannot play the recorded audio file. Actually, any android player application cannot play the recorded audio file.
On the other hand, Using Media.Recorder
to record generates a good recorded audio file that can be played by any player application. But the thing is that I cannot make a preview/palyback while recording the mic input in real time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要(几乎)实时录制和播放音频,您可以启动一个单独的线程并使用
AudioRecord
和AudioTrack
。只是要小心反馈。如果您的设备上的扬声器开得足够大,反馈可能会很快变得非常令人讨厌。
编辑
音频并未真正录制到文件中。
AudioRecord
对象将音频编码为 16 位 PCM 数据 并将其放入缓冲区中。然后,AudioTrack
对象从该缓冲区读取数据并通过扬声器播放。 SD 卡上没有您稍后可以访问的文件。您无法同时从 SD 卡读取和写入文件来实时播放/预览,因此必须使用缓冲区。
To record and play back audio in (almost) real time you can start a separate thread and use an
AudioRecord
and anAudioTrack
.Just be careful with feedback. If the speakers are turned up loud enough on your device, the feedback can get pretty nasty pretty fast.
EDIT
The audio is not really recording to a file. The
AudioRecord
object encodes the audio as 16 bit PCM data and places it in a buffer. Then theAudioTrack
object reads the data from that buffer and plays it through the speakers. There is no file on the SD card that you will be able to access later.You can't read and write a file from the SD card at the same time to get playback/preview in real time, so you have to use buffers.
需要清单中的以下权限才能正常工作:
此外,不需要 2d 缓冲区数组。即使只有一个缓冲区,代码的逻辑也是有效的,如下所示:
Following permission in manifest is required to work properly:
Also, 2d buffer array is not necessary. The logic of the code is valid even with just one buffer, like this: