使用AVAudioPlayer录制视频并播放音频,当设备连接到蓝牙时,电话不会通过蓝牙播放音频

时间:2020-04-14 11:19:57

标签: ios objective-c bluetooth avfoundation avaudiosession

- (void)initAVCaptureSession {

    [self setupVideo];
    [self setupAudio];

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

- (void)setupAudio {
    NSError *error = nil;
    self.audioInput = [[AVCaptureDeviceInput alloc] initWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio] error:&error];
    if (error) {
        return;
    }
    if ([self.session canAddInput:self.audioInput]) {
        [self.session addInput:self.audioInput];
    }
    self.audioOutput = [[AVCaptureAudioDataOutput alloc] init];
    dispatch_queue_t audioQueue = dispatch_queue_create("com.haha.com", DISPATCH_QUEUE_SERIAL);
    [self.audioOutput setSampleBufferDelegate:self queue:audioQueue];
    if([self.session canAddOutput:self.audioOutput]) {
        [self.session addOutput:self.audioOutput];
    }
}
......

使用AVAudioPlayer录制视频并播放音频,当设备连接到蓝牙时,手机不会通过蓝牙播放音频,而是继续使用手机扬声器。

初始化所有数据后,当我不调用setupaudio method, the audio can be played through the Bluetooth speaker. When I call setupaudio`时,音频将仅在电话扬声器上播放。有人知道为什么吗?

0 个答案:

没有答案