iphone - 转换后无法访问音频文件

标签 iphone objective-c avfoundation avaudioplayer

我一直在使用以下代码将 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/

相关文章:

ios - 处理 iOS 中动态 View 的所有旋转

macos - 如何在 Mac 上使用 AVFoundation 将图片编码为 H264,而不是使用 x264

iphone - 如何在 Objective-C 中创建一个 json 字符串?

iphone - iOS 应用程序在客户设备上崩溃,如何 react ,收集数据

ios - 如何定义将另一个作为条件的预处理器宏?

ios AVFoundation - 如何处理 UIDeviceOrientationFaceUp 和 UIDeviceOrientationFaceDown

ios - iOS 上的 VideoToolbox 示例?

iphone - 是否可以在不显示时清除显存中自定义 View 的内容?

iphone - 从 Core Data,iOS 获取最后插入的项目

ios - MR_saveToPersistentStoreAndWait 不保存数组中的数据