【问题标题】:How add AVCaptureDeviceInput to AVCaptureSession from bluetooth device?如何将 AVCaptureDeviceInput 从蓝牙设备添加到 AVCaptureSession?
【发布时间】:2015-04-23 21:53:37
【问题描述】:

我需要在 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;
}

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

【问题讨论】:

    标签: objective-c bluetooth avfoundation audio-recording


    【解决方案1】:

    解决办法是这样的:

    在您的 AppDelegate 中

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

    在您的 AVCaptureSession 添加您的 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];
        }
    

    【讨论】:

      【解决方案2】:

      我认为在使用选项设置类别时如何形成AVCaptureSession 存在问题。尝试这样的事情来制作AVCaptureSession

          // create and set up the audio session
      AVAudioSession* audioSession = [AVAudioSession sharedInstance];
      [audioSession setDelegate:self];
      [audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
      [audioSession setActive: YES error: nil];
      
      // set up for bluetooth microphone input
      UInt32 allowBluetoothInput = 1;
      OSStatus stat = AudioSessionSetProperty (
                               kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
                               sizeof (allowBluetoothInput),
                               &allowBluetoothInput
                              );
      NSLog(@"status = %x", stat);    // problem if this is not zero
      
      // check the audio route
      UInt32 size = sizeof(CFStringRef);
      CFStringRef route;
      OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
      NSLog(@"route = %@", route);    
      // if bluetooth headset connected, should be "HeadsetBT"
      // if not connected, will be "ReceiverAndMicrophone"
      
      // now, play a quick sound we put in the bundle (bomb.wav)
      CFBundleRef mainBundle = CFBundleGetMainBundle();
      CFURLRef        soundFileURLRef;
      SystemSoundID   soundFileObject;
      soundFileURLRef  = CFBundleCopyResourceURL (mainBundle,CFSTR ("bomb"),CFSTR ("wav"),NULL);
      
      NSError *error = nil;
      
      audioRecorder = [[AVAudioRecorder alloc]
                       initWithURL:soundFileURLRef
                       settings:recordSettings
                       error:&error];
      if (error)
      {
          NSLog(@"error: %@", [error localizedDescription]);
      } else {
          [audioRecorder prepareToRecord];
      }
      

      我从here 获取代码,因为它总是对我有用。

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

      【讨论】:

      • 在添加这行之前我必须把它放上去吗? AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
      • 我猜你对 AVFoundation 的经验较少。所以我给了你整个sn-p。检查编辑的答案。
      • 问题是我没有使用 AVAudioRecorder 进行记录。我正在使用苹果示例项目 Rosy Writer,您必须添加 AVCaptureDeviceInput 并使用 AVAssetWriter 编写。
      • 可以轻松修改。您不必使用 AVCaptureDeviceInput。只需四处寻找您在修改时遇到的问题。因为我不认为 AVCaptureDeviceInput 会加入蓝牙设备,所以只需尝试修改该项目。
      • 感谢您的帮助,那么如果我不必使用 AVCaptureDeviceInput,我是否必须使用 AVCaptureAudioDataOutput?非常感谢!
      猜你喜欢
      • 2016-10-03
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多