我正在使用MediaController
来控制我VideoView
的视频播放。我已覆盖VideoView.setOnPreparedListener
,以便在视频首次完成缓存后隐藏ActionBar / Toolbar
(hide()
)。当MediaController
发生时,我希望工具栏能够回归。我已尝试覆盖 MediaController
show()
和hide()
方法,如下所示:
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();
}
});
此功能正常,但播放控件停止显示除外!当然,在重写方法中调用递归mMediaController.show()
是行不通的......我可以把蛋糕吃掉吗?
修改
所以我也没有尝试过利用VideoView.setOnTouchListener
和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();
}
});
它确实显示,但它不会再次隐藏,也许我会误解OnCompletetionListener
?
编辑2
根据Amir的建议,我覆盖了onTouch
的{{1}},而不是完美,但它就像正确的轨道一样:
VideoView
这主要有效,它会切换 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;
}
});
,因此有时Toolbar
会显示Toolbar
,而每次切换时,MediaController
会定期执行MediaController
“出现几秒钟然后消失。”无论如何,这是一个有效的解决方案。
答案 0 :(得分:2)
您应该稍微更改OnTouchListener
。我在我的项目中做了以下工作并且工作正常:
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"));
hideToolbar(),带有一些traslate动画:
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
<强> showToolbar():强>
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
此外,如果您需要 HideStatusBar ,请使用true调用setUiFlag()
:
@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;
}
答案 1 :(得分:2)
如果您添加对相应超级方法的调用,则原始代码似乎有效。
mediaController = new MediaController(this){
@Override
public void show() {
super.show();
getSupportActionBar().show();
Toast.makeText(activity_media_player.this, "SHOW TOOLBAR", Toast.LENGTH_SHORT).show();
}
@Override
public void hide() {
super.hide();
getSupportActionBar().hide();
Toast.makeText(activity_media_player.this, "HIDE TOOLBAR", Toast.LENGTH_SHORT).show();
}
};