5

I'm building an Android media player application that I intend to use to play media (videos, pictures, etc.) on a TV while connected via an HDMI cable.

I want to have the media player app pause when the TV's power status is OFF and want it to play when the TV is turned ON.

How do I detect the TV's power status within my Android application when my Android device is connected to the TV via HDMI?

Both the TV and the Android device have support for HDMI-CEC. The device in question is an ODROID C2. I've seen this functionality on the KODI Android application which has a feature to pause the video when the HDMI-CEC status is OFF, I'm looking to implement this within my app as well.

Any help is appreciated. Thanks in advance!

EDIT: Progress below

I tried reading the status of the HDMI connection from within this file /sys/devices/virtual/switch/hdmi/state. However, this file holds int 1 no matter whether the power status of the connected screen / TV is ON or OFF.

2nd Progress update

I'm still working on this. Will not give up, and once I'm done I will surely post the answer here.

Raunaqss
  • 1,265
  • 12
  • 17
  • did you check: https://stackoverflow.com/questions/45639210/use-libcec-usb-dongle-in-android-app? most likely hat you're looking for can be found at https://gist.github.com/sdabet/ac4d7711d1a529806cb7b695530b1fac. – Mohammad Tabbara Mar 21 '18 at 13:14
  • @MohammadTabbara Thanks for your comment. Yes, I did. The question is about sending HDMI CEC signals to a TV using an Android box (much like this effort here: https://stackoverflow.com/questions/29670275/android-send-hdmi-cec-commands-to-tv-minix-libcec?rq=1). – Raunaqss Mar 21 '18 at 13:19

2 Answers2

4

You can listen for changes in HDMI status (0 for unplugged and 1 for plugged) by registering for ACTION_HDMI_AUDIO_PLUG. It reports with status 0 when tv is switched off, switches to any other display medium or HDMI is removed. To read into its technicality, you can check out how hot plug detection works in HDMI. Overall, your app can at all times monitor whether the display can currently play your content or not. I have myself implemented this in a solution (on X96 mini android box & amazon fire-stick) where I needed to ensure that the content was actually being played because it included paid content. Also, I have attached the sample code file. Note: This solution will only work when android device is HDMI source not sink!

Here's the documentation link too- https://developer.android.com/reference/android/media/AudioManager#ACTION_HDMI_AUDIO_PLUG

private BroadcastReceiver eventReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // pause video
        String action = intent.getAction();

        switch (action) {
            case ACTION_HDMI_AUDIO_PLUG :
                // EXTRA_AUDIO_PLUG_STATE: 0 - UNPLUG, 1 - PLUG
                Toast.makeText(getApplicationContext(),"HDMI PLUGGED OR UNPLUGGED",Toast.LENGTH_LONG).show();
                Log.d("MainActivity", "ACTION_HDMI_AUDIO_PLUG " + intent.getIntExtra(EXTRA_AUDIO_PLUG_STATE, -1));
                ((TextView)(findViewById(R.id.textView))).setText(((TextView)(findViewById(R.id.textView))).getText().toString().concat("At "+System.nanoTime()+": "+intent.getIntExtra(EXTRA_AUDIO_PLUG_STATE, -1) +"\n"));
                break;


        }
    }
};

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(eventReceiver);
}

@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter();
    filter.addAction(ACTION_HDMI_AUDIO_PLUG);
    registerReceiver(eventReceiver, filter);

}
1

In Some TV's, You need to monitor that (sys/class/amhdmitx/amhdmitx0/hpd_state) folder for changes by 500 ms Interval. because it'll change from 1 to 0 and again from 0 to 1 within 1 seconds.

  • kindly consider adding more information in your answer – Inder Aug 14 '18 at 07:36
  • @Inder In Samsung TV if you read mentioned path using linux 'cat' command through ADB Shell it'll always return 1(TV_STATE_ON). try reading that particular path continuously with ** 1 to 1.5 Seconds ** regular interval when you turn on the TV, It'll go to 0(TV_STATE_OFF) for just 1.5 Seconds. after that it'll again show 1(TV_STATE_ON). It seems like a bug from SAMSUNG TV Side(Not sure though). – Ahmed meeran Aug 14 '18 at 10:24