如何从蓝牙设备将AVCaptureDeviceInput添加到AVCaptureSession? [英] How add AVCaptureDeviceInput to AVCaptureSession from bluetooth device?

查看:114
本文介绍了如何从蓝牙设备将AVCaptureDeviceInput添加到AVCaptureSession?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在iOS应用中使用AVCaptureSession录制视频.

I need to record a video with AVCaptureSession in an iOS app.

当我将AVCaptureDeviceInput添加到当前的AVCaptureSession时,它将始终添加iphone麦克风.我已将蓝牙麦克风连接到设备.但是它不是从外部麦克风录制的.

When I add AVCaptureDeviceInput to my current AVCaptureSession, it always adds the iphone microphone. I have the bluetooth microphone connected to the device. But it is not recording from the external microphone.

我正在这样做:

- (BOOL)prepareAudioSession {

// deactivate session
BOOL success = [[AVAudioSession sharedInstance] setActive:NO error: nil];
if (!success) {
    NSLog(@"deactivationError");
}

// Bluetooth support enable
UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);
// set audio session category AVAudioSessionCategoryPlayAndRecord options AVAudioSessionCategoryOptionAllowBluetooth

success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionMixWithOthers error:nil];
//success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];

if (!success) {
    NSLog(@"setCategoryError");
}

// activate audio session
success = [[AVAudioSession sharedInstance] setActive:YES error: nil];
if (!success) {
    NSLog(@"activationError");
}
return success;
}

但是它仍然无法正常工作.有人知道吗谢谢

But it's still not working. Anyone have any idea? Thanks

推荐答案

解决方案是这样的:

在您的AppDelegate中

In your AppDelegate

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];

在添加AVCaptureDeviceInput之后在您的AVCaptureSession中

in your AVCaptureSession after add your AVCaptureDeviceInput

self.captureSession.usesApplicationAudioSession = true;
    self.captureSession.automaticallyConfiguresApplicationAudioSession = false;

我的音频设置:

/* Audio */
    AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];


    audioIn = [[AVCaptureDeviceInput alloc] initWithDevice:audioDevice error:nil];
    if ( [_captureSession canAddInput:audioIn] ) {
        [_captureSession addInput:audioIn];
    }

    audioOut = [[AVCaptureAudioDataOutput alloc] init];
    // Put audio on its own queue to ensure that our video processing doesn't cause us to drop audio
    dispatch_queue_t audioCaptureQueue = dispatch_queue_create( "com.apple.sample.capturepipeline.audio", DISPATCH_QUEUE_SERIAL );
    [audioOut setSampleBufferDelegate:self queue:audioCaptureQueue];


    if ( [self.captureSession canAddOutput:audioOut] ) {
        [self.captureSession addOutput:audioOut];
    }
    _audioConnection = [audioOut connectionWithMediaType:AVMediaTypeAudio];

    //AVAudioSessionRouteDescription *current =[[AVAudioSession sharedInstance] currentRoute];*/
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){
        self.captureSession.usesApplicationAudioSession = true;
        self.captureSession.automaticallyConfiguresApplicationAudioSession = false;
        //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
    }

这篇关于如何从蓝牙设备将AVCaptureDeviceInput添加到AVCaptureSession?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆