1

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

I have looked into the Audio Manager and Media Router APIs, but not been able to see any way of retrieving which of the audio devices is the currently selected output.

I've seen that for example Spotify app has this capability:

Spotify Screenshot with audio source shown

3 Answers 3

0

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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.