设置音频压缩设置时AVAssetWriter失败

时间:2016-01-17 02:40:51

标签: ios compression avassetwriter avasset

当我在以下代码中将audioCompressionSettings设为nil时,

AVAssetWriterInput* audioWriterInput =
[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
outputSettings:nil
sourceFormatHint:(__bridge CMAudioFormatDescriptionRef)[[audioTrack formatDescriptions] objectAtIndex:0]];

一切正常。

如果我添加outputSettings:audioCompressionSettings,我的视频在iOS 7,8,9中作为0字节文件出现。 在iOS 6中,以下语句抛出错误

NSParameterAssert([mediaWriter canAddInput:audioWriterInput]);

我的audioCompressionSettings定义如下,

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

请让我知道我做错了什么。

如果audioCompressionSettings为nil,则iOS 6会发出此错误:

*** WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:
request:frame:decisionListener: delegate:
<NSInternalInconsistencyException> Invalid parameter not satisfying:
[mediaWriter canAddInput:audioWriterInput]

mediaWriter定义如下,

AVAssetWriter *mediaWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeMPEG4 error:&error];
mediaWriter.shouldOptimizeForNetworkUse = YES;
NSParameterAssert(mediaWriter);

我实际上想要解决iOS 6问题,因为我真的不想对音频进行任何转换,只是让它通过。

0 个答案:

没有答案