Objective-C的/ IOS:什么是倒退播放音频文件的最简单方法

问题描述:

我一直在挣扎与这一个相当长一段时间,并有在网络上正在做它没有code的例子。谁能帮我?

I've been struggling with this one for quite a while now, and there are no code examples of it being done on the net. Can anyone help me?

我的应用程序使用AVFoundation录制的声音。
@ 16位深度,2通道,WAV

My app uses AVFoundation to record the audio. @16 bit depth , 2 channels , WAV

我可以访问音频的字节,我只是不知道如何扭转这种局面。

I can access the bytes of the audio, I just don't know how to reverse it.

在波数据采样交错。这意味着数据是这样组织的。

In wave data samples are interleaved. This means the data is organised like this.

Sample 1 Left | Sample 1 Right | Sample 2 Left | Sample 2 right ... Sample n Left | Sample n right

由于每个样本为16比特(2字节)的第2信道的样品(即两个左和右)的大小4个字节。

As each sample is 16-bits (2 bytes) the first 2 channel sample (ie for both left and right) is 4 bytes in size.

在知道这种方式,在波形数据的一个块中的最后样本是如下:

This way you know that the last sample in a block of wave data is as follows:

wavDataSize - 4

可以然后通过起始于记录结束和阅读向后其复制到不同的缓冲器在一个时间加载每个样品。当你到了波形数据的开始你已经扭转了波数据及播放将发生逆转。

You can then load each sample at a time by copying it into a different buffer by starting at the end of the recording and reading backwards. When you get to the start of the wave data you have reversed the wave data and playing will be reversed.

编辑:如果你想简单的方法来阅读iOS上的波形文件签出的音频文件服务参考

If you want easy ways to read wave files on iOS check out the Audio File Services Reference.

编辑2:

readPoint  = waveDataSize;
writePoint = 0;
while( readPoint > 0 )
{
    readPoint -= 4;
    Uint32 bytesToRead = 4;
    Uint32 sample;
    AudioFileReadBytes( inFile, false, maxData, &bytesToRead &sample );
    AudioFileWriteBytes( outFile, false, writePoint, &bytesToRead, &sample );

    writePoint += 4;
}