简体   繁体   English

Android - API 检查哪个设备(有线耳机或 USB 耳机)正在播放音频(如果两者都已连接)

[英]Android - API to check which device, wired headset or USB headset, is playing audio if both are connected

I have a problem detecting which device, wired headset or USB headset is playing audio when both of them are connected to an Android device.当有线耳机或 USB 耳机都连接到 Android 设备时,我无法检测到哪个设备正在播放音频。

Is there an API which will allow checking that?是否有允许检查的API

I have noticed that different devices behave in a different way.我注意到不同的设备以不同的方式运行。 Eg on Samsung S10+ with Android 9 the peripheral attached later is used to play audio.例如,在带有 Android 9 的三星 S10+ 上,稍后连接的外围设备用于播放音频。 However on Moto G6 Android 7 and Samsung S8+ Android 9 wired headset is always used, no matter which peripheral was connected later.但是在 Moto G6 Android 7 和 Samsung S8+ Android 9 上始终使用有线耳机,无论以后连接哪个外围设备。

I need this information to correctly configure the audio stream and to show which device is currently being used.我需要这些信息来正确配置音频 stream 并显示当前正在使用的设备。

Please have a look at MediaRouter API.请查看MediaRouter API。

MediaRouter.RouteInfo getSelectedRoute() is probably what you are looking for. MediaRouter.RouteInfo getSelectedRoute() 可能是您正在寻找的。

Also it allows you to register a listener to be notified when current audio device configuration changes(MediaRouter.Callback interface).它还允许您注册一个侦听器,以便在当前音频设备配置更改时收到通知(MediaRouter.Callback 接口)。 Its onRouteSelected and onRouteUnselected callbacks are called when an audio device starts/stops to play audio respectively.它的 onRouteSelected 和 onRouteUnselected 回调分别在音频设备开始/停止播放音频时被调用。

Here's a log (abridged) from my MTK device demonstrating attributes of MediaRouter.RouteInfo and flow of events that occurred when I plugged in wired headphones while listening to music via Jabra Bluetooth headset.这是来自我的 MTK 设备的日志(删节),展示了 MediaRouter.RouteInfo 的属性以及我在通过 Jabra 蓝牙耳机听音乐时插入有线耳机时发生的事件流。

onRouteChanged RouteInfo{ name=Headphones, description=null, status=null, category=RouteCategory{ name=System types=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO groupable=false }, supportedTypes=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO, presentationDisplay=null } onRouteChanged RouteInfo{ name=Headphones, description=null, status=null, category=RouteCategory{ name=System types=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO groupable=false }, supportedTypes=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO,presentationDisplay=null }

onRouteUnselected RouteInfo{ name=Jabra MINI v1.0.5, description=Bluetooth audio, status=null, category=RouteCategory{ name=System types=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO groupable=false }, supportedTypes=ROUTE_TYPE_LIVE_AUDIO, presentationDisplay=null } onRouteUnselected RouteInfo{ name=Jabra MINI v1.0.5, description=蓝牙音频, status=null, category=RouteCategory{ name=System types=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO groupable=false }, supportedTypes=ROUTE_TYPE_LIVE_AUDIO, presentationDisplay=null }

onRouteSelected RouteInfo{ name=Headphones, description=null, status=null, category=RouteCategory{ name=System types=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO groupable=false }, supportedTypes=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO, presentationDisplay=null } onRouteSelected RouteInfo{ name=Headphones, description=null, status=null, category=RouteCategory{ name=System types=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO groupable=false }, supportedTypes=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO, presentationDisplay=null }

onRouteRemoved RouteInfo{ name=Jabra MINI v1.0.5, description=Bluetooth audio, status=null, category=RouteCategory{ name=System types=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO groupable=false }, supportedTypes=ROUTE_TYPE_LIVE_AUDIO, presentationDisplay=null } onRouteRemoved RouteInfo{ name=Jabra MINI v1.0.5, description=蓝牙音频, status=null, category=RouteCategory{ name=System types=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO groupable=false }, supportedTypes=ROUTE_TYPE_LIVE_AUDIO, presentationDisplay=null }

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

相关问题 检查耳机是否已在Android应用程序中连接。 蓝牙和有线 - Check if headset is connected in Android application. Bluetooth and wired Android-SpeechRecognizer和有线耳机 - Android - SpeechRecognizer and wired headset 如何在Android中检测BT耳机或有线耳机 - How to detect the BT headset or wired headset in android 如何检查和重定向有线耳机和扬声器电话之间的音频? - How to check and redirect audio between wired headset and speaker phone? 如何录制耳机中播放的android音频 - How to record android audio playing in headset 收听Android中的有线耳机按钮 - Listen Wired Headset Buttons in Android 检查耳机是否在不同的API级别上连接 - Check if headset is connected on different API levels 有没有办法判断有线耳机是否已连接到电话 - Is there a way to tell if a wired headset is connected to phone 在Android中连接蓝牙耳机后,如何在内置麦克风和耳机麦克风之间切换音频输入? - how to switch audio input between inbuilt mic and headset mic after a bluetooth headset is connected in android? 如何在 React Native 中检测耳机/耳机是否连接到 Android 设备 - How to detect if a headphones / a headset is connected to an Android device in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM