AVAudioRecorder in iOS 8 does not handle kAudioFormatMPEG4AAC - ios

My code worked fine until iOS 8 kAudioFormatMPEG4AAC but now it creates an empty file. No errors are reported. When I changed it to kAudioFormatLinearPCM it works. This is my code:
recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
[NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithInt:16], AVEncoderBitRateKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithFloat:32000.0], AVSampleRateKey,
nil];

Please remove AVEncoderBitRateKey key from your settings dictionary and it will work on iOS8 with kAudioFormatMPEG4AAC.
It could be a specific correlation between AVEncoderBitRateKey and AVNumberOfChannelsKey. But I didn't play with parameters but used default bitrate value to get working recorder.

That bitrate looks suspiciously low, even for kAudioFormatMPEG4AAC (16 bytes per second), maybe try 16000 or 64000 or more.
p.s. try the new Objective-C literals for your NSDictionarys, NSArrays and NSNumbers, I think they're an improvement:
recordSettings = #{
AVEncoderAudioQualityKey : AVAudioQualityMin,
AVFormatIDKey : #(kAudioFormatLinearPCM),
AVEncoderBitRateKey : #16000,
AVNumberOfChannelsKey : #1,
AVSampleRateKey : #32000
};

Related

AVAssetWriter error code 561211770

I'm trying create a movie file with image/audio. The audio output format properties,
{ AVFormatIDKey : #(kAudioFormatMPEG4AAC), AVEncoderBitRateKey : #120000, AVNumberOfChannelsKey : #1, AVSampleRateKey : #44100.0 };
Video output format properties,
{ AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : #(600), AVVideoHeightKey : #(400) };
When I do finishWritingWithCompletionHandler on AVAssetWriter I get this error message on iPhone 7. Exactly same code works on iPhone 4s/5s.
did not save due to error Error Domain=AVFoundationErrorDomain
Code=-11800 "The operation could not be completed"
UserInfo={NSLocalizedDescription=The operation could not be completed,
NSUnderlyingError=0x14e1eb90 {Error Domain=NSOSStatusErrorDomain
Code=561211770 "(null)"}, NSLocalizedFailureReason=An unknown error
occurred (561211770)}
What's the meaning of this specific error code? Thanks.
Some time ago I worked with it. This is settings which I used:
settings = [NSDictionary dictionaryWithObjectsAndKeys:
[ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
[ NSNumber numberWithInt: ch], AVNumberOfChannelsKey,
[ NSNumber numberWithFloat: rate], AVSampleRateKey,
[ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
nil];
NumberOfChannels and SampleRate received from base video.

AVAssetWriterInput does not currently support AVVideoScalingModeFit - IOS Error

I'm attempting to use the AVAssetWriterInput to crop a video that I read in a screencast of my application. Here is my current configuration.
NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:320], AVVideoCleanApertureWidthKey,
[NSNumber numberWithInt:480], AVVideoCleanApertureHeightKey,
[NSNumber numberWithInt:10], AVVideoCleanApertureHorizontalOffsetKey,
[NSNumber numberWithInt:10], AVVideoCleanApertureVerticalOffsetKey,
nil];
NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:3], AVVideoPixelAspectRatioHorizontalSpacingKey,
[NSNumber numberWithInt:3],AVVideoPixelAspectRatioVerticalSpacingKey,
nil];
NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:960000], AVVideoAverageBitRateKey,
[NSNumber numberWithInt:1],AVVideoMaxKeyFrameIntervalKey,
videoCleanApertureSettings, AVVideoCleanApertureKey,
videoAspectRatioSettings, AVVideoPixelAspectRatioKey,
AVVideoProfileLevelH264BaselineAutoLevel, AVVideoProfileLevelKey,
nil];
NSDictionary *videoSettings = #{AVVideoCodecKey:AVVideoCodecH264,
AVVideoCompressionPropertiesKey:codecSettings,
AVVideoScalingModeKey:AVVideoScalingModeResizeAspectFill,
AVVideoWidthKey:[NSNumber numberWithInt:320],
AVVideoHeightKey:[NSNumber numberWithInt:480]};
_videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
I'm receiving the following error: "AVAssetWriterInput does not currently support AVVideoScalingModeFit"
This is a common error for anyone using this library, but I can't find the actual solution to it. I just see people saying: "I figured it out eventually" without explaining it. The problem is definitely related to this line: "AVVideoScalingModeKey:AVVideoScalingModeResizeAspectFill," which tells the AVAssetWriter to crop the video and maintain the aspect ratio. Anyone know the solution to this?
There is no "solution to it" per se. It's simply unsupported. You'll need to scale the video frames yourself using Core Image or a VTPixelTransferSession or whatever is appropriate for your pipeline.

AVRecorder can't set bit depth

I'm trying to control the bit depth of a .m4a file recorded with AVRecorder :
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: 8], AVEncoderBitDepthHintKey,
nil];
But AVEncoderBitDepthHintKey has no effect, same for AVEncoderAudioQualityKey.
It seems that iOS7 and 8 have different defaults : iOS7 always gives a s16p format (according to ffmpeg) and iOS8 a s32p.
I don't want to use any other format than AppleLossless.
I don't think AVEncoderAudioQualityKey is applicable to kAudioFormatAppleLossless and AVEncoderBitDepthHintKey is only a hint.
Maybe you'd have more luck getting 8bit audio in ALAC via the lower level CoreAudio AudioConverter API.

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

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?
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.

Audio which is recorded in iOS programmatically could not be playable in Windows

Scenario: The App which I am working on I am suppose to recored voice memos and send them to the Windows client. Recording audio in iOS is not that much hard but the recorded file (in my case in the form of .mp3) could not be playable in Windows but the same file could be easily playable in Mac.
I strongly suspect on my audio recording settings which I am using in the following code,
_audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioFileURL settings:_recordingSettings error:&error];
and the settings that I am using is,
_recordingSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
[NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16], AVEncoderBitRateKey,
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
nil];
I have already tried the various AVFormatIDKeys such as
kAudioFormatLinearPCM - .lpcm
kAudioFormatMPEG4AAC - .acc
kAudioFormatMPEGLayer3 - .mp3
but no gain.
I appreciate any help. In case if it is not possible I need the explanation why!
After some random tries I have managed myself with the settings,
_recordingSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsNonInterleaved,
[NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithFloat:2000.0], AVSampleRateKey,
nil];
Important: The filename should be "someName.wav"

Resources