将.caf转换为.wav格式所需的参数?

时间:2012-02-09 04:31:39

标签: iphone objective-c ios ipad

我希望将录制的.caf文件转换为.wav文件。我正在尝试使用

OSStatus AudioFileCreateWithURL( CFURLRef   inFileRef, AudioFileTypeID   inFileType,const AudioStreamBasicDescription *inFormat, UInt32   inFlags,AudioFileID  *outAudioFile);

上面的方法我坚持设置两个参数,我的意思是我必须在中设置哪个参数 const AudioStreamBasicDescription * inFormat和UInt32 inFlags,AudioFileID *outAudioFile

在我尝试使用以下方法的位置

- (id)initWithURL:(NSURL *)url settings:(NSDictionary *)settings error:(NSError **)outError

我再次坚持设置:(NSDictionary *)设置,我的意思是我必须设置或用于从.caf转换为.wav文件的参数。

如果iphone中存在任何其他解决方案,请告诉我

1 个答案:

答案 0 :(得分:3)

//这里发送存储.caf文件

的文件路径
-(BOOL)exportAssetAsWaveFormat:(NSString*)filePath                  
{
    NSError *error = nil ;

    NSDictionary *audioSetting = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [ NSNumber numberWithFloat:44100.0], AVSampleRateKey,
                                  [ NSNumber numberWithInt:2], AVNumberOfChannelsKey,
                                  [ NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
                                  [ NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
                                  [ NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey, 
                                  [ NSNumber numberWithBool:0], AVLinearPCMIsBigEndianKey,
                                  [ NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
                                  [ NSData data], AVChannelLayoutKey, nil ];

    NSString *audioFilePath = filePath;
    AVURLAsset * URLAsset = [[AVURLAsset alloc]  initWithURL:[NSURL fileURLWithPath:audioFilePath] options:nil];

    if (!URLAsset) return NO ;

    AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:URLAsset error:&error];
    if (error) return NO;

    NSArray *tracks = [URLAsset tracksWithMediaType:AVMediaTypeAudio];
    if (![tracks count]) return NO;

    AVAssetReaderAudioMixOutput *audioMixOutput = [AVAssetReaderAudioMixOutput
                                                   assetReaderAudioMixOutputWithAudioTracks:tracks
                                                   audioSettings :audioSetting];

    if (![assetReader canAddOutput:audioMixOutput]) return NO ;

    [assetReader addOutput :audioMixOutput];

    if (![assetReader startReading]) return NO;



    NSString *title = @"WavConverted";
    NSArray *docDirs = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [docDirs objectAtIndex: 0];
    NSString *outPath = [[docDir stringByAppendingPathComponent :title]
                         stringByAppendingPathExtension:@"wav" ];

    NSURL *outURL = [NSURL fileURLWithPath:outPath];
    AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:outURL
                                                          fileType:AVFileTypeWAVE
                                                             error:&error];
    if (error) return NO;

    AVAssetWriterInput *assetWriterInput = [ AVAssetWriterInput assetWriterInputWithMediaType :AVMediaTypeAudio
                                                                                outputSettings:audioSetting];
    assetWriterInput. expectsMediaDataInRealTime = NO;

    if (![assetWriter canAddInput:assetWriterInput]) return NO ;

    [assetWriter addInput :assetWriterInput];

    if (![assetWriter startWriting]) return NO;


    [assetReader retain];
    [assetWriter retain];

    [assetWriter startSessionAtSourceTime:kCMTimeZero ];

    dispatch_queue_t queue = dispatch_queue_create( "assetWriterQueue", NULL );

    [assetWriterInput requestMediaDataWhenReadyOnQueue:queue usingBlock:^{

        NSLog(@"start");

        while (1)
        {
            if ([assetWriterInput isReadyForMoreMediaData]) {

                CMSampleBufferRef sampleBuffer = [audioMixOutput copyNextSampleBuffer];

                if (sampleBuffer) {
                    [assetWriterInput appendSampleBuffer :sampleBuffer];
                    CFRelease(sampleBuffer);
                } else {
                    [assetWriterInput markAsFinished];
                    break;
                }
            }
        }

        [assetWriter finishWriting];
        [assetReader release ];
        [assetWriter release ];

        NSLog(@"finish");
    }];

    dispatch_release(queue);
}