如何使用AVAudioPlayerNode保存文件?

时间:2016-01-25 12:12:02

标签: objective-c macos avaudioplayer avaudioplayernode

我使用avaudioplayernode为音频制作了一些效果。然后我想保存结果。我尝试这样做:

        Output = engine.outputNode;
        AudioStreamBasicDescription asbd;
        memset(&asbd, 0, sizeof(asbd));
        asbd.mSampleRate = SAMPLE_RATE;
        asbd.mFormatID = kAudioFormatLinearPCM;
        asbd.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked; 
        asbd.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
        asbd.mBitsPerChannel = 16;
        asbd.mChannelsPerFrame = 2;
        asbd.mFramesPerPacket = 1;
        asbd.mBytesPerFrame = 4;
        asbd.mBytesPerPacket = 4;
        AudioFileID OutputFile;
        CFStringRef inputFile = (__bridge CFStringRef)(save);
        CFURLRef inputFileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, inputFile, kCFURLPOSIXPathStyle,false);
        CheckError (AudioFileCreateWithURL(inputFileURL, kAudioFileWAVEType, &asbd, kAudioFileFlags_EraseFile, &OutputFile), "err");
        CFRelease(inputFileURL);
        float y = 6871947*16;//audio.length;
        myData1 = [NSData dataWithBytesNoCopy:(__bridge void * _Nonnull)(Output) length:y freeWhenDone:NO];
        NSUInteger length1 = [myData1 length]-4096; 
        UInt32 lgt = (UInt32) length1/4;
        UInt16 *cdata = (UInt16*) malloc (length1);
        [myData1 getBytes:(void*)cdata range:NSMakeRange(0, length1)];
        CheckError (  AudioFileWritePackets(OutputFile, TRUE, lgt, NULL, 0, &lgt, cdata), "err");
        AudioFileClose(OutputFile);

但是保存文件是错误的。我认为将Output设置为NSData是错误的一步。但我不知道另一种方式。并尝试这样做。我该如何保存新文件?

0 个答案:

没有答案