Home > Back-end >  How to find the name of the currently selected media output?
How to find the name of the currently selected media output?

Time:08-10

What is the correct way to retrieve the name of the currently selected media audio output on Android?

I have looked into the Spotify Screenshot with audio source shown

CodePudding user response:

Spotify has its own API. The endpoint you need to use to get the device name of the current device is Get Playback State (demo here).
To get the names of all available devices, you can use the Get Available Devices endpoint (demo here).

CodePudding user response:

I didn't try this with wired devices but I did this for Bluetooth devices like this-

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));
    }
};

You can try this for Bluetooth devices, I hope it helps.

  • Related