IOS 扬声器、蓝牙耳机、听筒切换

try session.setCategory(.playAndRecord,options: [.allowBluetooth, .allowBluetoothA2DP, .defaultToSpeaker])

try session.setActive(true)

try session.overrideOutputAudioPort(.none)

这个是激活,默认是扬声器  defaultToSpeaker

try session.overrideOutputAudioPort(.none)  这个就是默认的意思


此时当蓝牙耳机连接了,那么声音会自动切到耳机;此时把耳机断开,那么这个时候无论怎么切都是扬声器,因为上边defaultToSpeaker,所以这个时候需要监听耳机断开,然后重新设置

try session.setCategory(.playAndRecord,options: [.allowBluetooth, .allowBluetoothA2DP])
try session.setActive(true)
try session.overrideOutputAudioPort(.none)

这样就变成默认是听筒了,然后要切换为扬声器

try session.overrideOutputAudioPort(.speaker)


主要的一个原因是在耳机连接状态下,突然把耳机断开了,就出问题了

Powered By Z-BlogPHP 1.7.4

Copyright 粤ICP备2024347557号 Rights Reserved.