I'm using a MediaController
to control video play back for my VideoView
. I've overriden VideoView.setOnPreparedListener
so that the ActionBar/Toolbar
is hidden (hide()
) after the video first finishes buffering. And I'd like the toolbar to comeback when the MediaController
does. I've tried overriding the MediaController
show()
and hide()
methods, like so:
mVideoView.start();
// Media Controller
mMediaController = new MediaController(this){
@Override
public void show() {
getSupportActionBar().show();
}
@Override
public void hide() {
getSupportActionBar().hide();
}
};
mMediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mMediaController);
// Hide toolbar once video starts
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// Hide support bar
getSupportActionBar().hide();
}
});
This works, except the playback controls stop showing up! And of course, calling a recursive mMediaController.show()
within the overridden method doesn't work... Can I have my cake an eat it too?
Edit
So I've also, unsucessfully tried taking advantage of the VideoView.setOnTouchListener
and VideoView.setOnCompletetionListener
:
mVideoView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
getSupportActionBar().show();
return false;
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
getSupportActionBar().hide();
}
});
It does show, but it won't hide again, perhaps I misinterpret OnCompletetionListener
?
Edit 2
From Amir's suggestion, I override the onTouch
for VideoView
, not perfect, but it's on like the right track:
mMediaController = new MediaController(this);
mVideoView = (VideoView) findViewById(R.id.media_player);
mVideoView.setOnTouchListener(new View.OnTouchListener() {
boolean flag;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if(flag) {
mMediaController.hide();
getSupportActionBar().hide();
} else {
mMediaController.show();
getSupportActionBar().show();
}
flag = !flag;
return true;
}
return false;
}
});
This mostly works, it toggles the Toolbar
and so sometimes the Toolbar
will appear without the MediaController
, and each time I toggle it, the MediaController
does it's regular 'appear for a few seconds and then disappear.' In any case, it is a working solution.
You should change your OnTouchListener
a bit. I do following in my project and works fine:
final MediaController mediaController = new MediaController(this);
VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.setOnTouchListener(new View.OnTouchListener() {
boolean flag = true;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
if (flag) {
mediaController.hide();
hideToolbar();
}
else {
mediaController.show(0);
showToolbar();
}
flag = !flag;
return true;
}
return false;
}
});
videoView.setMediaController(mediaController);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
}
});
videoView.setVideoURI(Uri.parse("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"));
And hideToolbar() with some traslate animation:
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
showToolbar():
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
Also if you need to HideStatusBar call setUiFlag()
with true:
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void setUiFlags(boolean fullscreen) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
View decorView = getWindow().getDecorView();
if (decorView != null) {
decorView.setSystemUiVisibility(fullscreen ? getFullscreenUiFlags() : View.SYSTEM_UI_FLAG_VISIBLE);
}
}
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private int getFullscreenUiFlags() {
int flags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
}
return flags;
}