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

标签 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中存在其他解决方案,请给我?

最佳答案

//在此处发送您存储.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);
}

关于iphone - 将.caf转换为.wav格式所需的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9205446/

相关文章:

iphone - 将数组索引传输到下一个 Controller

ios - 仅在 iOS 上声明属性

ios - 从 NavigationController 移除 ViewController 后 AVPlayer 继续播放

javascript - 是否有涵盖 iPhone、Android、WebOS 和 Blackberry OS 的 jQ Touch 等价物?

ios - 在不关闭键盘的情况下平滑地调整大小和移动 UITableViewCell

ios - 当我收到 SIGABRT 错误时,如何了解我的错误在哪里?

ios - RestKit - 数据不会在应用程序启动之间持续存在

iphone - 在 UITableViewController 中对行进行排序

iphone - CGMutablePathRef 改变颜色

iOS 应用程序开发- NSMutableURLRequest 为零