当前位置: 首页 > 知识库问答 >
问题:

AVFoundation自定义录像机音频问题

艾浩广
2023-03-14

我试图学习如何创建自定义录像机,一切工作良好,除了音频有很多嘈杂的声音。我在系统相机、instagram或任何其他相机应用程序中尝试,它们的音频质量非常好,没有某种低音。此外,视频的录制量也明显低于其他相机应用。我在stackoverflow上找不到任何答案,其中大多数有关于AvAudioSession的答案,但在AvCapturesession上没有。

_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];
  1. 如何提高录制视频时的音频质量?
  2. 如何增加视频的录制量?

我知道如何增加它时,只是纯录制音频与下面的代码,但不是当录制视频。

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

共有1个答案

翟淮晨
2023-03-14

好吧,我终于想通了。创建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;

现在视频的输出音频甚至比系统更好

 类似资料:
  • 我已经建立了一个自定义的相机应用程序。面对屏幕方向的问题,我不希望在方向改变时重新创建活动。请用完整的解决方案帮助我。 在预览类中的surfaceChanged方法内获取空指针异常。 获取以下行中的空指针异常: 任何帮助将不胜感激。 以下是错误日志: 12-23 16:07:34.962 8845-8845/com。清晰摄像机E/AndroidRuntime﹕ 致命异常:主进程:com。清晰摄像头

  • 我正在准备一个小的Android应用程序,捕捉照片和录音从用户。类似用户会用他的相机拍一张照片,然后在观看该照片时添加音频评论。

  • 本文向大家介绍Android自定义录制视频功能,包括了Android自定义录制视频功能的使用技巧和注意事项,需要的朋友参考一下 Android录制视频MediaRecorder+SurfaceView的使用方法,供大家参考,具体内容如下 先看效果图: <1>将视频动画显示到SurfaceView控件上 <2>使用MediaRecorder类进行视频的录制 常用的方法: 下面看代码: 以上就是本文的

  • 问题内容: 我想知道这里是否有人知道如何将NAO机器人音频流传输到python中的远程模块,以及您是否有示例。我想直接在笔记本电脑上获取NAO音频缓冲区以进行处理,因此无需录制和scp或其他操作。 现在我的问题是这样的:aldebaran网站说,为了远程(在我的笔记本电脑上)接收音频,我必须编写一个继承自ALSoundExtractor的模块。事实是,此类未包含在python SDK中,因此,如果

  • 问题内容: 我目前正在尝试使用Android实现一些代码,以检测何时通过手机的麦克风播放了多个特定音频频率范围。我已经使用AudioRecord该类设置了该类: 然后读取音频: 执行FFT是我遇到的困难,因为我在这方面的经验很少。我一直在尝试使用此类: Java和Complex类中的FFT一起使用 然后,我发送以下值: 这很容易让我误解了此类的工作原理,但是返回的值到处都是跳跃的,即使在沉默中也不

  • 我正在尝试设置一个树莓pi盒与一个usb摄像头作为一个IP摄像头,可以从一个通用的android IP摄像头监控应用程序查看。我找到了一些关于如何获取视频流的例子,这很管用,但我还需要双向音频。这似乎在独立的网络摄像机中是一个很好的选择--有什么想法吗?我想以一种与典型网络摄像机兼容的方式设置它,以便我的cam可以被任何通用的ip摄像机查看器应用程序使用。