如何查找当前所选媒体输出的名称?

问题描述 投票:0回答:3

在 Android 上检索当前所选媒体音频输出的名称的正确方法是什么?

我已经研究了 Audio ManagerMedia Router API,但无法找到任何方法来检索哪个音频设备是当前选定的输出。

我已经看到例如 Spotify 应用程序具有此功能:

android android-audiomanager mediarouter
3个回答
0
投票

我没有在有线设备上尝试过这个,但我对像这样的蓝牙设备做了这个-

BluetoothManager btManager = (BluetoothManager)   
                             getSystemService(Context.BLUETOOTH_SERVICE);

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

bluetoothAdapter.cancelDiscovery();

               bluetoothAdapter.getProfileProxy(this, listener, BluetoothProfile.HEADSET);

public final BluetoothProfile.ServiceListener listener = new  BluetoothProfile.ServiceListener() {
    @Override
    public void onServiceConnected(int i, final BluetoothProfile      bluetoothProfile) {

        final TextView device = (TextView) findViewById(R.id.device);
        List<BluetoothDevice> b = bluetoothProfile.getConnectedDevices();
        StringBuilder stringBuilder = new StringBuilder();
        for(BluetoothDevice getConnectedDevice : b){
            stringBuilder.append(getConnectedDevice.getName());
        }
       device.setText(stringBuilder); 
    }

    @Override
    public void onServiceDisconnected(int i) {
        final TextView device = (TextView) findViewById(R.id.device);
        device.setText(String.valueOf(i));
    }
};

您可以在蓝牙设备上尝试此操作,希望对您有所帮助。


0
投票
  private fun getOutputDeviceName(): String {
    val am = getSystemService(AUDIO_SERVICE) as AudioManager
        ?: return ""
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        if (am.isWiredHeadsetOn || am.isBluetoothScoOn || am.isBluetoothA2dpOn)
            return "Headset"
    } else {
        val devices = am.getDevices(AudioManager.GET_DEVICES_OUTPUTS)
        for (device in devices) {
            if (device.type == AudioDeviceInfo.TYPE_WIRED_HEADSET || device.type == AudioDeviceInfo.TYPE_WIRED_HEADPHONES || device.type == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP || device.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
                return device.productName.toString()
            }
        }
    }
    return ""
}

0
投票
val mediaRouter = context.getSystemService(Context.MEDIA_ROUTER_SERVICE) as MediaRouter
                        // Assuming you want to check the route for media playback
                        val route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_AUDIO)

//route.name.toString() <- name

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.