将实时音频从线路输入路由到 AirPods?
Route live audio from line-in to AirPods?
有没有办法将来自有线线路输入的音频直接路由到 AirPods?
目前,我正在创建一个 .playAndRecord
音频会话。配对 AirPods。稍后,使用 AVAudioEngine
我将输入设备直接连接到输出设备。
engine.connect(
engine.inputNode,
to: engine.outputNode,
format: engine.inputNode.inputFormat(forBus: 0)
)
效果很好,我可以直接在耳边听到 AirPods 麦克风发出的现场 (!) 声音。但是后来我连接了线路输入电缆,不幸的是,它也覆盖了线路输入的输出(不过我可以很好地读取计量电平)。如果我连接 AirPods,那么输入和输出都会再次变为 AirPods。
有没有办法将输出设备重新路由到 AirPods,但保持输入设备不变?
UPDATE: I tried set the input port (to line-in) manually using setPreferredInput
, but as soon as the input is selected, the output is also get adjusted (to the wired device).
UPDATE: I tried routing both with AVRoutePickerView
and MPVolumeView
, but all yielded the same result. If I pick a new output, the input gets modified as well.
UPDATE: AudioBus app does this, I can set wired headphones as input, and AirPods as output, works like a charm. So it is definitely possible, I just have no idea what API to use. Actually, neither AudioBus can share the audio to two sets of AirPods.
是的!但是,它仅在使用 allowBluetoothA2DP
选项创建音频会话时有效(根据@RobNapier )。这是一个 output-only 配置文件,因此它 根本不会路由来自蓝牙设备的输入 。
A2DP is a stereo, output-only profile intended for higher bandwidth audio use cases, such as music playback. The system automatically routes to A2DP ports if you configure an app’s audio session to use the ambient
, soloAmbient
, or playback
categories.
Starting with iOS 10.0, apps using the playAndRecord
category may also allow routing output to paired Bluetooth A2DP devices. To enable this behavior, pass this category option when setting your audio session’s category.
您可以让它与 bare-bones 声音管理器一起使用,如下所示。
class Sound {
...
private var session: AVAudioSession {
AVAudioSession.sharedInstance()
}
private let engine = AVAudioEngine()
init() {
setupAudioSession()
}
func setupAudioSession() {
do {
try session.setCategory(
.playAndRecord,
options: [
.allowBluetoothA2DP, //
.allowAirPlay
]
)
try session.setActive(true)
} catch {
print("Could not configure and activate session. \(error)")
}
}
func start() {
engine.connect(
engine.inputNode,
to: engine.outputNode,
format: engine.inputNode.inputFormat(forBus: 0)
)
do {
try engine.start()
} catch {
print("Could not start engine. \(error)")
}
}
...
}
如果您将 MPVolumeView
放在 UI 的某处,那么您可以明确地 select 到蓝牙设备(以及更多)的路由。但我想即使没有它,您也可以通过简单地物理 connect/disconnect 设备本身来更改路线。
有没有办法将来自有线线路输入的音频直接路由到 AirPods?
目前,我正在创建一个 .playAndRecord
音频会话。配对 AirPods。稍后,使用 AVAudioEngine
我将输入设备直接连接到输出设备。
engine.connect(
engine.inputNode,
to: engine.outputNode,
format: engine.inputNode.inputFormat(forBus: 0)
)
效果很好,我可以直接在耳边听到 AirPods 麦克风发出的现场 (!) 声音。但是后来我连接了线路输入电缆,不幸的是,它也覆盖了线路输入的输出(不过我可以很好地读取计量电平)。如果我连接 AirPods,那么输入和输出都会再次变为 AirPods。
有没有办法将输出设备重新路由到 AirPods,但保持输入设备不变?
UPDATE: I tried set the input port (to line-in) manually using
setPreferredInput
, but as soon as the input is selected, the output is also get adjusted (to the wired device).
UPDATE: I tried routing both with
AVRoutePickerView
andMPVolumeView
, but all yielded the same result. If I pick a new output, the input gets modified as well.
UPDATE: AudioBus app does this, I can set wired headphones as input, and AirPods as output, works like a charm. So it is definitely possible, I just have no idea what API to use. Actually, neither AudioBus can share the audio to two sets of AirPods.
是的!但是,它仅在使用 allowBluetoothA2DP
选项创建音频会话时有效(根据@RobNapier
A2DP is a stereo, output-only profile intended for higher bandwidth audio use cases, such as music playback. The system automatically routes to A2DP ports if you configure an app’s audio session to use the
ambient
,soloAmbient
, orplayback
categories.
Starting with iOS 10.0, apps using theplayAndRecord
category may also allow routing output to paired Bluetooth A2DP devices. To enable this behavior, pass this category option when setting your audio session’s category.
您可以让它与 bare-bones 声音管理器一起使用,如下所示。
class Sound {
...
private var session: AVAudioSession {
AVAudioSession.sharedInstance()
}
private let engine = AVAudioEngine()
init() {
setupAudioSession()
}
func setupAudioSession() {
do {
try session.setCategory(
.playAndRecord,
options: [
.allowBluetoothA2DP, //
.allowAirPlay
]
)
try session.setActive(true)
} catch {
print("Could not configure and activate session. \(error)")
}
}
func start() {
engine.connect(
engine.inputNode,
to: engine.outputNode,
format: engine.inputNode.inputFormat(forBus: 0)
)
do {
try engine.start()
} catch {
print("Could not start engine. \(error)")
}
}
...
}
如果您将 MPVolumeView
放在 UI 的某处,那么您可以明确地 select 到蓝牙设备(以及更多)的路由。但我想即使没有它,您也可以通过简单地物理 connect/disconnect 设备本身来更改路线。