如何使用AVAssetWriter在ios中写出正确的WAV音频?

时间:2021-03-10 19:42:42

I'm using AVAssetWriter to output wav file in the app temp folder with these settings:

我正在使用AVAssetWriter在app temp文件夹中使用以下设置输出wav文件:

NSDictionary*  outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                                      [ NSNumber numberWithInt: 2 ], AVNumberOfChannelsKey,
                                      [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
                                      [ NSData data ], AVChannelLayoutKey,
                                      [ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
                                      nil];

It's outputting seemingly normal 45mb .wav file but the app can not read it anyways. I suspect the file header is corrupt because I opened the the .wav in audacity (which accepted it no problem) and re-exported it with again .wav extension. The exported file worked fine in the app.

它输出看似正常的45mb .wav文件,但应用程序无法读取它。我怀疑文件头已损坏,因为我在大胆中打开了.wav(接受它没问题)并再次使用.wav扩展名重新导出它。导出的文件在应用程序中运行良好。

I also opened the both .wav files with the text editor to see the headers and they were somewhat different.

我还用文本编辑器打开了两个.wav文件以查看标题,它们有些不同。

any clues?

1 个解决方案

#1


0  

You're using kAudioFormatMPEG4AAC for AVFormatIDKey when you should be using kAudioFormatLinearPCM. Wave files are a PCM format and cannot be exported with MPEG-4 packets.

当你应该使用kAudioFormatLinearPCM时,你正在为AVFormatIDKey使用kAudioFormatMPEG4AAC。 Wave文件是PCM格式,无法使用MPEG-4数据包导出。

#1


0  

You're using kAudioFormatMPEG4AAC for AVFormatIDKey when you should be using kAudioFormatLinearPCM. Wave files are a PCM format and cannot be exported with MPEG-4 packets.

当你应该使用kAudioFormatLinearPCM时,你正在为AVFormatIDKey使用kAudioFormatMPEG4AAC。 Wave文件是PCM格式,无法使用MPEG-4数据包导出。