split stereo audio to mono streams on iOS

2019-07-19 16:23发布

问题:

Apologies if this has been answered. I've seen lots of questions but no good answers.

I'm trying to export stereo music from my iPod library to two mono caf files. How can I do this on iOS? I'm currently using Objective C.

Thanks!

Update: I've managed to get the sample code working from apple thats here: https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/05_Export.html

My code will now import a media file and output as a valid caf file which plays fine. My problem is I can't work out how to modify the buffer before writing it. Here is my code I have so far:

// Get the next audio sample buffer, and append it to the output file.
CMSampleBufferRef sampleBuffer = [self.assetReaderAudioOutput copyNextSampleBuffer];
if (sampleBuffer != NULL)
{
    /////////////////////////////////////////
    CMBlockBufferRef blockBuffer;
    AudioBufferList audioBufferList;
    NSMutableData *data= [NSMutableData data];

    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &audioBufferList, sizeof(AudioBufferList), NULL, NULL, kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, &blockBuffer);

    AudioBuffer audioBuffer = audioBufferList.mBuffers[0];

    for( int y=0; y< audioBufferList.mNumberBuffers; y=y+4 ){
        [data appendBytes:(audioBuffer.mData+y) length:2];
    }

    // how do I replace data in sampleBuffer with new data?

    CFRelease(blockBuffer);

    //////////////////////////////////////////

    BOOL success = [self.assetWriterAudioInput appendSampleBuffer:sampleBuffer];

    CFRelease(sampleBuffer);
    sampleBuffer = NULL;
    completedOrFailed = !success;
}
else
{
    completedOrFailed = YES;
}

回答1:

I've managed to do this by manually creating the header and writing to the file. This post gave me most of what I needed: https://stackoverflow.com/a/8677726/313272