我正在写一个应用程序,我需要在其中录制音频并向后播放。我已经使用AVAudioRecorder将音频录制到caf文件中,并且能够使用AVAudioPlayer和MPMoviePlayerController向前播放它。我尝试将MPMoviePlayerController.currentPlaybackRate设置为-1,但不会发出任何噪音。通过研究,我发现我需要逐个字节地反转音频文件,但我不确定如何做到这一点。有没有一种方法可以将caf文件读到数组中并从数组中写入?任何帮助都将不胜感激。
发布于 2013-02-06 15:56:13
我在一个示例应用程序上工作,它记录用户所说的话并向后播放它们。我已经使用CoreAudio实现了这一点。Link to app code。
因为每个样本的大小是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);希望这能有所帮助。
https://stackoverflow.com/questions/11912594
复制相似问题