在iOS中将录制的音频文件转换为.wav格式

在iOS中将录制的音频文件转换为.wav格式,第1张

概述我想录制一个音频文件并以.wav格式上传到服务器,但录制器不允许我将文件录制成wav格式. 录音我使用过代码:     NSMutableDictionary * recordSetting = [[NSMutableDictionary alloc] init]; [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4A 我想录制一个音频文件并以.wav格式上传到服务器,但录制器不允许我将文件录制成wav格式.

录音我使用过代码:
    NSMutableDictionary * recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];[recordSetting setValue:[NSNumber numberWithfloat:44100.0] forKey:AVSampleRateKey];[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];// Initiate and prepare the recorderrecorder = [[AVAudioRecorder alloc] initWithURL:outputfileURL settings:recordSetting error:nil];recorder.delegate = self;recorder.meteringEnabled = YES;[recorder preparetoRecord];

我找到的另一种方法,录音后将文件从caff转换为wav,但这对我来说也不起作用.

我已经使用从caff到wav的转换文件的代码:

-(BOol)exportAssetAsWaveFormat:(NSURL*)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,AVlinearPCMIsNonInterleaved,[ NSData data],AVChannelLayoutKey,nil ];//  Nsstring *audiofilePath = filePath;AVURLAsset * URLAsset = [[AVURLAsset alloc]  initWithURL:recorder.url 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;[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];    NSLog(@"finish %@",assetWriter);}];return YES;//  dispatch_release(queue);}

提前致谢.

任何帮助将非常感激.

解决方法 您正在将文件上载到服务器,但我建议您不要使用wav格式,因为它的大小比任何其他格式都要大.请改用caf,m4a或任何其他格式.

您可以通过将AVFormatIDKey设置为kAudioFormatlinearPCM来记录wav文件;没有其他编码格式可行.

NSURL *url = [NSURL fileURLWithPath: outPath];NSError *err = nil;audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url                                              settings: audioSetting                                                 error:&err];//prepare to record[audioRecorder setDelegate:self];[audioRecorder preparetoRecord];audioRecorder.meteringEnabled = YES;[audioRecorder recordForDuration:(NSTimeInterval)10000000000];[audioRecorder record];
总结

以上是内存溢出为你收集整理的在iOS中将录制的音频文件转换为.wav格式全部内容,希望文章能够帮你解决在iOS中将录制的音频文件转换为.wav格式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1072316.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存