ios - AVAssetWriter Writes Video but does not record AUDIO

左心房为你撑大大i 提交于 2019-12-13 03:45:03

问题


I have been successfull in making viedo (though Blur) but have been Unsuccessful in recording audio Please Help.

I am using the following code Pls go through it ans suggest some solution or a working tutorial.

I am Appending the Sample Buffer using
[encoder._writerInput appendSampleBuffer:sampleBuffer]; And The App Crashes When the following statement is called [encoder.audioWriterInput appendSampleBuffer:sampleBuffer];

- (void) initPath:(NSString*)path Height:(int) height andWidth:(int) width bitrate:(int)bitrate
{
    self.path = path;
    _bitrate = bitrate;

    [[NSFileManager defaultManager] removeItemAtPath:self.path error:nil];
    NSURL* url = [NSURL fileURLWithPath:self.path];

    //Initialize Video Writer
    _writer = [AVAssetWriter assetWriterWithURL:url fileType:AVFileTypeMPEG4 error:nil];

    //Initialize Audio writer
//    audioWriter = [AVAssetWriter assetWriterWithURL:url fileType:AVFileTypeCoreAudioFormat error:nil];

//    NSDictionary* settings = @{
//        AVVideoCodecKey: AVVideoCodecH264,
//        AVVideoWidthKey: @(width),
//        AVVideoHeightKey: @(height),
//        AVVideoCompressionPropertiesKey: @{
//             AVVideoAverageBitRateKey: @(self.bitrate),
//             AVVideoMaxKeyFrameIntervalKey: @(150),
//             AVVideoProfileLevelKey: AVVideoProfileLevelH264BaselineAutoLevel,
//             AVVideoAllowFrameReorderingKey: @NO,
//             //AVVideoH264EntropyModeKey: AVVideoH264EntropyModeCAVLC,
//             //AVVideoExpectedSourceFrameRateKey: @(30),
//             //AVVideoAverageNonDroppableFrameRateKey: @(30)
//        }
//    };
    NSDictionary* settings = @{
                               AVVideoCodecKey: AVVideoCodecH264,
                               AVVideoWidthKey: [NSNumber numberWithInt:width],
                               AVVideoHeightKey: [NSNumber numberWithInt:height],
                               AVVideoCompressionPropertiesKey: @{
                                       AVVideoAverageBitRateKey: [NSNumber numberWithInt:bitrate],
                                       AVVideoMaxKeyFrameIntervalKey: @(150),
                                       AVVideoProfileLevelKey: AVVideoProfileLevelH264BaselineAutoLevel,
                                       AVVideoAllowFrameReorderingKey: @NO,
                                       AVVideoH264EntropyModeKey: AVVideoH264EntropyModeCAVLC,
                                       AVVideoExpectedSourceFrameRateKey: @(30),
                                       AVVideoAverageNonDroppableFrameRateKey: @(30)
                                       }
                               };

    AudioChannelLayout acl;
    bzero( &acl, sizeof(acl));
    acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;

    NSDictionary *audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                           [ NSNumber numberWithInt: kAudioFormatMPEG4AAC ], AVFormatIDKey,
                           [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
                           [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
                           [ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
                           [ NSData dataWithBytes: &acl length: sizeof( acl ) ], AVChannelLayoutKey,
                           nil];

    audioWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings];
    audioWriterInput.expectsMediaDataInRealTime=YES;
    _writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:settings];
    _writerInput.expectsMediaDataInRealTime = YES;

    [_writer addInput:audioWriterInput];
    [_writer addInput:_writerInput];
}

来源:https://stackoverflow.com/questions/23216610/ios-avassetwriter-writes-video-but-does-not-record-audio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!