我一直在使用以下代码将 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);
最佳答案
听起来文件上有锁。您可能需要将 FileID 放入 AudioFileClose 行...
AudioFileID fileID;
AudioFileClose(fileID);
关于iphone - 转换后无法访问音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7377205/