Objective c 如何将AVCaptureDeviceInput从蓝牙设备添加到AVCaptureSession?

Objective c 如何将AVCaptureDeviceInput从蓝牙设备添加到AVCaptureSession?,objective-c,bluetooth,avfoundation,audio-recording,Objective C,Bluetooth,Avfoundation,Audio Recording,我需要在iOS应用程序中使用AVCaptureSession录制视频 当我将AVCaptureDeviceInput添加到当前的AVCaptureSession时,它总是添加iphone麦克风。我已将蓝牙麦克风连接到设备。但它不是从外部麦克风录制的 我正在这样做: - (BOOL)prepareAudioSession { // deactivate session BOOL success = [[AVAudioSession sharedInstance] setActive:NO err

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

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

我正在这样做:

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

但它仍然不起作用。有人知道吗?谢谢

我认为在使用选项设置类别时,如何形成AVCaptureSession存在问题。尝试以下操作以创建AVCaptureSession:

我从中获取了代码,因为它对我总是有效的

注意:此代码将在播放回耳机时重新路由音频。要在播放时使用扬声器,请查看已接受答案的评论部分。

解决方案如下:

在AppDelegate中

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
在添加AVCaptureDeviceInput后的AVCaptureSession中

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

在添加这行之前,我必须先添加这个吗?AVCaptureDevice*audioDevice=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];我猜你对AVFoundation的经验比较少。所以我给了你整个片段。检查编辑后的答案。问题是我没有使用录音机记录。我使用的是苹果的样例项目Rosy Writer,您必须添加AVCaptureDeviceInput并使用AVAssetWriter进行编写。这很容易修改。您不必使用AVCaptureDeviceInput。只要看看你在修改时面临的问题就可以了。因为我不认为AVCaptureDeviceInput将登记蓝牙设备,所以请尝试修改该项目。感谢您的帮助,如果我不必使用AVCaptureDeviceInput,那么我必须使用AVCaptureAudioDataOutput吗?谢谢!
/* 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];
    }