'How to identify the HDMI port and TV switch in android programmatically?

I want to know how to handle the HDMI switch to TV's home events. I have developed an app with exoplayer for FireTV. Issue is when I switch between HDMI port and TV, The player is still playing in background. I don't wan to play the content in the background.

I checked in Amazon developer docs. Asked me to use broadcast for the event, ACTION_HDMI_AUDIO_PLUG for the Version 5, and AudioDeviceCallbacks. ACTION_HDMI_AUDIO_PLUG only works for plugin and plugout on HDMI port.

But I want to know the exact event actions to switch between HDMI port and TV's. So that I can handle the player events.

IntentFilter filter = new IntentFilter();
filter.addAction(AudioManager.ACTION_HDMI_AUDIO_PLUG);
registerReceiver(mReceiver, filter);

Here is the reciever code:

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (AudioManager.ACTION_HDMI_AUDIO_PLUG.equals(action)) {
            int state = intent.getIntExtra("state", 0);
            Log.d(TAG, "ACTION_HDMI_AUDIO_PLUG: State"+state);
        }
    }
};


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source