我一直使用以下代码将aac / mp3文件转换为pcm。代码运行得很好。但是在转换完成之后,如果我尝试使用AVAudioPlayer播放该文件或对其执行任何操作,则不会发生任何事情,就像文件不在那里一样。它不会给我任何错误,如果我重新启动应用程序,它就可以工作。我花了很多时间试图弄清楚这一点,但我一点头绪都没有。谢谢你的帮助
// open an ExtAudioFile
ExtAudioFileRef inputFile;
ExtAudioFileOpenURL((CFURLRef)exportURL, &inputFile);
// prepare to convert to a plain ol' PCM format
AudioStreamBasicDescription myPCMFormat;
myPCMFormat.mSampleRate = 22050.0;
myPCMFormat.mFormatID = kAudioFormatLinearPCM ;
myPCMFormat.mFormatFlags = kAudioFormatFlagsCanonical;
myPCMFormat.mChannelsPerFrame = 2;
myPCMFormat.mFramesPerPacket = 1;
myPCMFormat.mBitsPerChannel = 16;
myPCMFormat.mBytesPerPacket = 4;
myPCMFormat.mBytesPerFrame = 4;
ExtAudioFileSetProperty(inputFile, kExtAudioFileProperty_ClientDataFormat, sizeof (myPCMFormat), &myPCMFormat);
// allocate a big buffer. size can be arbitrary for ExtAudioFile.
// you have 64 KB to spare, right?
UInt32 outputBufferSize = 0x10000;
void* ioBuf = malloc (outputBufferSize);
UInt32 sizePerPacket = myPCMFormat.mBytesPerPacket;
UInt32 packetsPerBuffer = outputBufferSize / sizePerPacket;
// set up output file
NSString *outputPath = [[self pathOfFile2] stringByAppendingPathComponent: AS(final, @".aiff")];
NSURL *outputURL = [NSURL fileURLWithPath:outputPath];
AudioFileID outputFile;
AudioFileCreateWithURL((CFURLRef)outputURL,
kAudioFileCAFType,
&myPCMFormat,
kAudioFileFlags_EraseFile,
&outputFile);
// start convertin'
UInt32 outputFilePacketPosition = 0; //in bytes
while (true) {
// wrap the destination buffer in an AudioBufferList
AudioBufferList convertedData;
convertedData.mNumberBuffers = 1;
convertedData.mBuffers[0].mNumberChannels = myPCMFormat.mChannelsPerFrame;
convertedData.mBuffers[0].mDataByteSize = outputBufferSize;
convertedData.mBuffers[0].mData = ioBuf;
UInt32 frameCount = packetsPerBuffer;
// read from the extaudiofile
ExtAudioFileRead(inputFile, &frameCount, &convertedData);
if (frameCount == 0) {
break;
}
// write the converted data to the output file
AudioFileWritePackets(outputFile,
false,
frameCount,
NULL,
outputFilePacketPosition / myPCMFormat.mBytesPerPacket,
&frameCount,
convertedData.mBuffers[0].mData);
// advance the output file write location
outputFilePacketPosition +=
(frameCount * myPCMFormat.mBytesPerPacket);
}
// clean up
ExtAudioFileDispose(inputFile);
AudioFileClose(outputFile);发布于 2011-09-11 16:55:24
听起来像是文件被锁定了。您可能需要将FileID放在AudioFileClose行中...
AudioFileID fileID;
AudioFileClose(fileID);https://stackoverflow.com/questions/7377205
复制相似问题