gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:23:09 24 4
gpt4 key购买 nike

我想将录制的.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com