split stereo audio to mono streams on iOS

僤鯓⒐⒋嵵緔 提交于 2019-12-24 02:43:28

问题


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



来源:https://stackoverflow.com/questions/27895554/split-stereo-audio-to-mono-streams-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!