0

我正在编写一个应用程序,我需要在其中录制音频并向后播放。我已经使用 AVAudioRecorder 将音频录制到一个 caf 文件中,并且我已经能够使用 AVAudioPlayer 和 MPMoviePlayerController 向前播放它。我尝试将 MPMoviePlayerController.currentPlaybackRate 设置为 -1,但它不会产生任何噪音。通过研究,我发现我需要逐字节反转音频文件,但我不知道该怎么做。有没有办法将caf文件读取到数组并从数组中写入?任何帮助,将不胜感激。

4

1 回答 1

0

我开发了一个示例应用程序,它记录用户所说的内容并向后播放。我已经使用 CoreAudio 来实现这一点。链接到应用程序代码

由于每个样本的大小为 16 位(2 字节)(单声道)(这取决于您用于记录的属性)。您可以通过从记录结束开始并向后读取将其复制到不同的缓冲区来一次加载每个样本。当您到达数据的开头时,您已经反转了数据并且播放将被反转。

// set up output file
AudioFileID outputAudioFile;

AudioStreamBasicDescription myPCMFormat;
myPCMFormat.mSampleRate = 16000.00;
myPCMFormat.mFormatID = kAudioFormatLinearPCM ;
myPCMFormat.mFormatFlags =  kAudioFormatFlagsCanonical;
myPCMFormat.mChannelsPerFrame = 1;
myPCMFormat.mFramesPerPacket = 1;
myPCMFormat.mBitsPerChannel = 16;
myPCMFormat.mBytesPerPacket = 2;
myPCMFormat.mBytesPerFrame = 2;


AudioFileCreateWithURL((__bridge CFURLRef)self.flippedAudioUrl,
                       kAudioFileCAFType,
                       &myPCMFormat,
                       kAudioFileFlags_EraseFile,
                       &outputAudioFile);
// set up input file
AudioFileID inputAudioFile;
OSStatus theErr = noErr;
UInt64 fileDataSize = 0;

AudioStreamBasicDescription theFileFormat;
UInt32 thePropertySize = sizeof(theFileFormat);

theErr = AudioFileOpenURL((__bridge CFURLRef)self.recordedAudioUrl, kAudioFileReadPermission, 0, &inputAudioFile);

thePropertySize = sizeof(fileDataSize);
theErr = AudioFileGetProperty(inputAudioFile, kAudioFilePropertyAudioDataByteCount, &thePropertySize, &fileDataSize);

UInt32 dataSize = fileDataSize;
void* theData = malloc(dataSize);

//Read data into buffer
UInt32 readPoint  = dataSize;
UInt32 writePoint = 0;
while( readPoint > 0 )
{
    UInt32 bytesToRead = 2;

    AudioFileReadBytes( inputAudioFile, false, readPoint, &bytesToRead, theData );
    AudioFileWriteBytes( outputAudioFile, false, writePoint, &bytesToRead, theData );

    writePoint += 2;
    readPoint -= 2;
}

free(theData);
AudioFileClose(inputAudioFile);
AudioFileClose(outputAudioFile);

希望这可以帮助。

于 2013-02-06T07:56:12.640 回答