How do I use AVAssetWriter to write correct WAV audio out in ios?

956 views Asked by At

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

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.

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

any clues?

1

There are 1 answers

1
Pescolly On

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.