首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换后无法访问音频文件

转换后无法访问音频文件
EN

Stack Overflow用户
提问于 2011-09-11 16:42:24
回答 1查看 327关注 0票数 2

我一直使用以下代码将aac / mp3文件转换为pcm。代码运行得很好。但是在转换完成之后,如果我尝试使用AVAudioPlayer播放该文件或对其执行任何操作,则不会发生任何事情,就像文件不在那里一样。它不会给我任何错误,如果我重新启动应用程序,它就可以工作。我花了很多时间试图弄清楚这一点,但我一点头绪都没有。谢谢你的帮助

代码语言:javascript
复制
// 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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-11 16:55:24

听起来像是文件被锁定了。您可能需要将FileID放在AudioFileClose行中...

代码语言:javascript
复制
AudioFileID fileID;
AudioFileClose(fileID);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7377205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档