Reverse audio file in Android

前端 未结 1 2019
攒了一身酷
攒了一身酷 2020-12-31 23:19

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

1条回答
  •  时光说笑
    2020-12-31 23:40

    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.

    0 讨论(0)
提交回复
热议问题