iPhone向后播放咖啡音频
问题描述:
我在写一个应用程序,我需要录制音频并向后播放。我使用AVAudioRecorder将音频录制到caf文件中,并且我已经能够使用AVAudioPlayer和MPMoviePlayerController转发它。我试着将MPMoviePlayerController.currentPlaybackRate设置为-1,但它不会产生任何噪音。从研究中,我发现我需要逐字节地反转音频文件,但我不知道该怎么做。有没有办法将caf文件读取到数组中并从数组中写入?任何帮助,将不胜感激。iPhone向后播放咖啡音频
答
我已经开发了一个示例应用程序,它记录了用户所说的并向后播放它们。我已经使用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);
希望这会有所帮助。