Android 中的反向音频文件

发布于 2024-10-11 08:28:53 字数 308 浏览 8 评论 0原文

我正处于开发这个应用程序的早期阶段,但研究它我已经遇到了一个问题。我需要能够向后播放音频文件(你知道喜欢揭示隐藏的消息;))。我没有在 Android 上处理音频的经验,也不知道这是否可能。

我在这里发现了一个问题,它解决了java中的问题(Click Here For Question

但是这个使用android不支持的javax.sound库。我需要这个库来解决这个问题还是有其他方法来反转音频文件?

I am in the very ealy stages of developing this app but looking into it I have already reached a problem. I need to be able to play an audio file backwards (you know like to reveal hidden messages ;)). I have no experience working with audio on android and have no idea if this is even possible.

I found a question on here which solves the problem in java (Click Here For Question)

But this makes use of the javax.sound library which android does not support. Will I need this library to solve this problem or is there another way to reverse an audio file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2024-10-18 08:28:59

典型的 WAV 文件由 44 字节标头和后面的实际样本值组成。 “帧”的大小取决于 WAV 文件的属性:立体声且每样本 16 位的文件将具有 4 字节的帧大小(左样本为 2 个字节,右样本为 2 个字节) )。

因此,在代码中,您可以通过创建与原始大小相同的字节数组来创建第二个 WAV 文件。您将 44 字节标头从原始帧复制到副本中,然后从最后一个帧开始迭代到第一个帧。将每个帧复制到复制数组中的相反位置(即,最后一个原始帧在标题之后立即复制到复制数组中;倒数第二帧在第一帧之后复制,等等)。然后播放反转的文件即可。

因此,您不需要 javax.sound 库来执行此操作 - 您只需要能够复制和操作字节。仅供参考,并非所有 WAV 文件都是这样“规范”的(规范意味着 44 字节标头加上样本值,仅此而已)。 WAV 格式实际上是一种 RIFF 格式,这意味着理论上您需要对样本值进行更复杂的提取。在实践中(特别是如果您自己创建 WAV 文件),您通常可以采用我在此处描述的更简单的方法。

注意:如果您的声音是 MP3 文件,则反转是一项更复杂的任务,因为样本数据不会作为样本存储在 MP3 文件中。如果您使用的是 MP3,反转它们的一种方法是将它们转换为 WAV,然后反转 WAV 文件。

A typical WAV file consists of a 44-byte header followed by the actual sample values. The size of a "frame" is dependent upon the WAV file's properties: a file that is stereo and 16-bits-per-sample will have a 4-byte frame size (two bytes for the left sample and two bytes for the right sample).

So in code, you would create a second WAV file by creating a byte array the same overall size as the original. You copy the 44-byte header from the original into the copy, and then iterate through the original frames starting at the last and working forward to the first. You copy each frame into the inverse location in the copy array (i.e. last original frame is copied into the copy array immediately after the header; second-to-last frame is copied after the first frame etc.). Then just play the reversed file.

So you don't need the javax.sound library to do this - you just need to be able to copy and manipulate bytes. FYI, not all WAV files are "canonical" like this (canonical means 44-byte header plus sample values, and nothing else). The WAV format is actually a RIFF format, which means in theory you need to do more complex extraction of the sample values. In practice (especially if you're creating the WAV files yourself) you can usually get away with a much simpler approach as I've described here.

NOTE: if your sounds are MP3 files, then reversing is a more complicated task, since the sample data are not stored as samples in an MP3 file. If you're using MP3s, one way to reverse them is to convert them to WAV and then reverse the WAV file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文