开发者

can't access audio file after converting it

Ive been using the following code to convert aac / mp3 files into pcm. The code works fine. But right after the convert ion is done if I try to play that file or do anything with it using AVAudioPlayer nothing happens like if the file wasn't there. It doesn't give me any error and if I restart the app then it works. Ive spent ours trying to figure that out but I have not a clue. Thanks for any help

// open an ExtAudioFile
ExtAudioFileRef inputFile;
ExtAudioFileOpenURL((CFURLRef)exportURL, &inputFile);

// prepare to convert to a开发者_Go百科 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);


Sounds like there is a lock on the file. You may need to put the FileID in the AudioFileClose line...

AudioFileID fileID;
AudioFileClose(fileID);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜