AVAssetWriter error code 561211770 - ios

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.

Related

AVAssetWriter fails when audio Compression Settings are set

When I have audioCompressionSettings as nil in the following code,
AVAssetWriterInput* audioWriterInput =
[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
outputSettings:nil
sourceFormatHint:(__bridge CMAudioFormatDescriptionRef)[[audioTrack formatDescriptions] objectAtIndex:0]];
everything works fine.
If I add outputSettings:audioCompressionSettings, my Video comes out as a 0-byte file in iOS 7, 8, 9.
And in iOS 6, the following statement throws an error,
NSParameterAssert([mediaWriter canAddInput:audioWriterInput]);
My audioCompressionSettings is defined as follows,
AudioChannelLayout stereoChannelLayout = {
.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo,
.mChannelBitmap = 0,
.mNumberChannelDescriptions = 0
};
NSData *channelLayoutAsData = [NSData dataWithBytes:&stereoChannelLayout length:offsetof(AudioChannelLayout, mChannelDescriptions)];
NSDictionary *audioCompressionSettings = #{
AVFormatIDKey : [NSNumber numberWithUnsignedInt:kAudioFormatMPEG4AAC],
AVEncoderBitRateKey : [NSNumber numberWithInteger:128000],
AVSampleRateKey : [NSNumber numberWithInteger:44100],
AVChannelLayoutKey : channelLayoutAsData,
AVNumberOfChannelsKey : [NSNumber numberWithUnsignedInteger:2]
};
Please let me know what I am doing wrong.
With audioCompressionSettings nil, iOS 6 spits out this error:
*** WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:
request:frame:decisionListener: delegate:
<NSInternalInconsistencyException> Invalid parameter not satisfying:
[mediaWriter canAddInput:audioWriterInput]
mediaWriter is defined as follows,
AVAssetWriter *mediaWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeMPEG4 error:&error];
mediaWriter.shouldOptimizeForNetworkUse = YES;
NSParameterAssert(mediaWriter);
I actually want the iOS 6 issue to be fixed as I really do not want to do any transformation of the audio but just have it pass through.

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.

AVAudioRecorder in iOS 8 does not handle kAudioFormatMPEG4AAC

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
};

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