Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autohide the media player layout in android

I am designing a media player with a custom layout. I want the interface to disappear after 16s of inactivity. It should reappear if the user touches the screen. The code snippet is given below:

 public void showhideControllers(int n) {
    if (n == 1) {
        /* make layout invisible */

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                volumeBar.setVisibility(View.INVISIBLE);
                audioControllView.setVisibility(View.INVISIBLE);
                topBar.setVisibility(View.INVISIBLE);
            }
        }, 16000);

    } else {
        /* make layout visible */           
        volumeBar.setVisibility(View.VISIBLE);
        topBar.setVisibility(View.VISIBLE);
        audioControllView.setVisibility(View.VISIBLE);

        showhideControllers(1);
    }

}

    @Override
public void onUserInteraction() {
    super.onUserInteraction();
    showhideControllers(2);
}

Inside the onCreate(), I am starting the timer by calling showhideControllers(1);. Now, when I click on the screen the layout reappears and the timer is reset. But if I randomly go on clicking the screen the timer is not reset after every click and the layout disappears after 16s. Can you tell me what am I doing wrong ?

like image 411
curiousguy Avatar asked Feb 11 '12 15:02

curiousguy


People also ask

How do I hide my Youtube overlay?

⭐ Simply use the keyboard shortcut Ctrl+M to hide the controls or show them again.


1 Answers

Sorry for late response. But here is the solution. I was having similar issue. So I have made following changes in your code, please try this and let me know if it helps you.

private Runnable hideControllerThread = new Runnable() {

    public void run() {
            volumeBar.setVisibility(View.GONE);
            audioControllView.setVisibility(View.GONE);
            topBar.setVisibility(View.GONE);
    }
};


public void hideControllers() {
        hidehandler.postDelayed(hideControllerThread, 15000);
}

public void showControllers() {
        volumeBar.setVisibility(View.VISIBLE);
        topBar.setVisibility(View.VISIBLE);
        audioControllView.setVisibility(View.VISIBLE);
        hidehandler.removeCallbacks(hideControllerThread);
        hideControllers();
}

@Override
public void onUserInteraction() {
        super.onUserInteraction();

        if (audioControllView.getVisibility() == View.VISIBLE) {
            hidehandler.removeCallbacks(hideControllerThread);
            hideControllers();
        } else {
            showControllers();
        }
}
like image 65
jyotiprakash Avatar answered Nov 11 '22 17:11

jyotiprakash