使用 Android SDK 从原始 PCM 数据创建 WAV 文件
我正在尝试使用 AudioRecord 类来录制 WAV 文件。问题是它只提供原始 PCM 数据,如果我将其写入文件,则没有标头信息,因此它不会在任何媒体播放器中播放。如何从这些原始数据创建 WAV 文件?
或者,有没有其他方法可以将 Android 中的声音录制到 WAV 文件(或者 MP3)?
哦,我知道 MediaRecorder 不能使用,因为它不支持 WAV 或 MP3 格式。
I'm trying to use the AudioRecord class to record a WAV file. The problem is that it only supplies the raw PCM data, and if I write it to a file, there is no header information, so it will not play in any media player. How can I create a WAV file from this raw data?
Or alternatively, is there any other way to record sound in Android to a WAV file (or, alternatively MP3)?
Oh, and I know that MediaRecorder can'y be used because it doesn't support either WAV or MP3 formats.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我已经弄清楚了。这篇文章对我的帮助至关重要:
http://computermusicblog.com/blog /2008/08/29/reading-and-writing-wav-files-in-java
基本上,我使用 ByteArrayOutputStream 从 AudioRecord 写入原始 PCM 数据,然后让我在以下情况下获取字节数组及其大小:该过程已完成。然后,我可以将该数据与 SampleRate、BitRate 和 Stereo/Mono 设置结合使用,按照上面的链接创建 WAV 标头。生成的文件完美运行!
OK, I've got this figured out. This post was crucial in helping me:
http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java
Basically, I used ByteArrayOutputStream to write the raw PCM data from AudioRecord, which then lets me get the byte array and its size when the process is done. I can then use that data in conjunction with the SampleRate, BitRate, and Stereo/Mono settings to create the WAV header as per the link above. The resulting file works perfectly!
检查MediaRecorder.setOutputFormat(),您可以为您的录制设置不同的容器格式;有 MediaRecorder.OutputFormat.MPEG_4 和 MediaRecorder.OutputFormat.THREE_GPP; RAW 中唯一允许的格式是 setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
抱歉,MP3 不可用。您真的需要 mp3 来录音吗?
与 MP3 相反的 WAV 是一个容器,而不是格式; WAV 可以是任何类型的编码格式。
您始终可以在原始 pcm 数据前面添加一些 WAV RIFF 标头(只要您确切知道格式)。在这里查看它的外观:
http://www-mmsp.ece.mcgill.ca/Documents /AudioFormats/WAVE/WAVE.html
Check the MediaRecorder.setOutputFormat(), you can set different container formats for your recording; there is MediaRecorder.OutputFormat.MPEG_4 and MediaRecorder.OutputFormat.THREE_GPP; the only allowed format along RAW is setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
Sorry but MP3 is not avail. You really need mp3 for recording?
WAV on the opposite of MP3 is a container, not a format; WAV can be any kind of encoding format.
You are always free to prepend some WAV RIFF header in front of your raw pcm data (as long as you exactly know the format). Check here for how it has to look like:
http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
您可能想使用 mediarecord 类
You may want to use mediarecord class