简体   繁体   English

AVFoundation自定义录像机音频问题

[英]AVFoundation custom video recorder audio issues

I try to learn how to create custom video recorder, everything work fine except when the audio having a lot of noisy sound. 我尝试学习如何创建自定义录像机,一切正常,除非音频有很多嘈杂的声音。 I try in the system camera, instagram or any other camera app their audio quality is very good and don't have some kind of low level sound. 我尝试在系统相机,Instagram或任何其他相机应用程序,他们的音质非常好,没有某种低级别的声音。 Beside, the recorded volume of the video also significantly lower compare to other camera app. 此外,与其他相机应用相比,录制的视频音量也明显降低。 I can't found any answer on stackoverflow which most of them having answer about AVAudioSession but not on AVCaptureSession . 我无法在stackoverflow上找到任何答案,其中大部分都有关于AVAudioSession而不是AVCaptureSession答案。

Here is the code how I implement the recorder 以下是我如何实现录像机的代码

_session =  [[AVCaptureSession alloc]init];

NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput *audioInput = [[AVCaptureDeviceInput alloc] initWithDevice:[devices objectAtIndex:0] error:nil];
if ([_session canAddInput:audioInput])
    [_session addInput:audioInput];

AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:nil];
if ( [_session canAddInput:deviceInput] )
    [_session addInput:deviceInput];

_videoOutput = [[AVCaptureVideoDataOutput alloc] init];
_videoOutput.alwaysDiscardsLateVideoFrames = NO;

if ( [_session canAddOutput:_videoOutput] )
    [_session addOutput:_videoOutput];

_audioOutput = [[AVCaptureAudioDataOutput alloc] init];

if ( [_session canAddOutput:_audioOutput] )
    [_session addOutput:_audioOutput];

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

CALayer *rootLayer = [[self view] layer];
[rootLayer setMasksToBounds:YES];
[previewLayer setFrame:CGRectMake(0, 0, rootLayer.bounds.size.width, rootLayer.bounds.size.height)];
[rootLayer insertSublayer:previewLayer atIndex:0];

[_session setSessionPreset:AVCaptureSessionPresetHigh];

dispatch_queue_t queue = dispatch_queue_create("MyQueue", NULL);
[_videoOutput setSampleBufferDelegate:self queue:queue];
[_audioOutput setSampleBufferDelegate:self queue:queue];


AVCaptureConnection *videoConnection = [_videoOutput connectionWithMediaType:AVMediaTypeVideo];
[videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];

[_session startRunning];

The video quality is fine and the code working, just having trouble on the audio part. 视频质量很好,代码正常工作,只是在音频部分出现问题。 So 所以

  1. How can I improve the audio quality when recording a video? 录制视频时如何改善音频质量?
  2. How can I increase the recording volume of the video? 如何增加视频的录制音量?

I know how to increase it when just pure recording an audio with the code below but not when recording video. 我知道如何使用下面的代码纯音频录制音频,而不是录制视频时。

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:(AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetooth) error:&sessionCategoryError];

Ok I finally figure it out. 好的,我终于搞清楚了。 After create the AVCaptureSession do the following code. 创建AVCaptureSession请执行以下代码。

 AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:(AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetooth) error:nil];
[audioSession setActive:YES error:nil];

_session =  [[AVCaptureSession alloc]init];

//the 2 line below is very important. 
_session.usesApplicationAudioSession = YES;
_session.automaticallyConfiguresApplicationAudioSession = NO;

Now the ouput audio of the video is even better than the system 现在,视频的输出音频甚至比系统更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM