简体   繁体   English

AVPlayer 不通过蓝牙耳机输出音频

[英]AVPlayer Does Not Output Audio Through Bluetooth Earphones

I have an avassetwriter capture session to record a video and 2 avplayerqueues to playback the immediately recorded video and the other, to playback past saved videos.我有一个avassetwriter捕获会话来录制视频和 2 个avplayerqueues来播放立即录制的视频,另一个用于播放过去保存的视频。

My problem is the audio input does not use my bluetooth earphones, and performs playback and record via the iphone device inputs/outputs.我的问题是音频输入不使用我的蓝牙耳机,而是通过 iphone 设备输入/输出执行播放和录制。

I did not implement any override to default to the speaker, understand I need to handle a route change via notification observer in the case of toggling between bluetooth and device, and also tried setting the AVAudioSession.sharedInstance() .playbackAndRecording category to .allowBluetooth to no avail.我没有实现任何覆盖默认扬声器,明白我需要在蓝牙和设备之间切换的情况下通过通知观察器处理路由更改,并且还尝试将AVAudioSession.sharedInstance() .playbackAndRecording类别设置为.allowBluetooth徒劳无功。

Any guidance would be appreciated as I have not come across an existing answer online..任何指导将不胜感激,因为我还没有在网上遇到现有的答案..

let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSession.Category.playAndRecord, options: [.allowBluetooth, .allowBluetoothA2DP])
        try audioSession.setActive(true)
    } catch {
        fatalError("Error Setting Up Audio Session")
    }

Did you read carefully the documentation for .allowBluetooth and .allowBluetoothA2DP ?您是否仔细阅读了.allowBluetooth.allowBluetoothA2DP的文档? These do not mean what you likely think they mean, since passing both is rarely what developers mean.这些并不意味着您可能认为它们的意思,因为通过这两者很少是开发人员的意思。 Do not guess what AVFoundation will do based on the names of things;不要根据事物的名称猜测 AVFoundation 会做什么; you must read the documentation.您必须阅读文档。 The names of things are not obvious, and are actively misleading in several places.事物的名称并不明显,并在几个地方积极误导。

If you want to record from Bluetooth headphones, you cannot support A2DP.如果要从蓝牙耳机录音,则不支持 A2DP。 A2DP is a unidirectional protocol (playback only). A2DP 是单向协议(仅播放)。 You will need to use HFP, which is what .allowBluetooth means.您将需要使用 HFP,这就是.allowBluetooth意思。 Remove .allowBluetoothA2DP .删除.allowBluetoothA2DP Note that this is significantly reduce your audio quality.请注意,这会显着降低您的音频质量。

If you have distinct periods of recording vs playback, then you want to change your category when you change modes.如果您有不同的录制和播放时间段,那么您希望在更改模式时更改您的类别。 Do not just set .playAndRecord because you will record at some point and playback at another.不要只设置.playAndRecord因为您将在某个时间点录制并在另一个时间点播放。 If you switch to a playback-only situation, switch to .playback .如果您切换到仅播放情况,请切换到.playback It is legal to change categories while the session is active (again, see the docs; there are many subtle rules).在会话处于活动状态时更改类别是合法的(同样,请参阅文档;有许多微妙的规则)。

You haven't listed what your Bluetooth earphones are, so it's not clear whether they support both A2DP and HFP.你没有列出你的蓝牙耳机是什么,所以不清楚它们是否同时支持A2DP和HFP。 That has significant impact on how routing occurs.这对路由的发生方式有重大影响。 See the docs for .allowBluetoothA2DP on this.请参阅有关.allowBluetoothA2DP的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM