MediaController定位 – 绑定到VideoView

关于如何定位MediaController的讨论很多,大多数答案都是使用setAnchorView 。 乍一看这个解决方案似乎有效但在我的情况下却没有。

根据这篇文章, setAnchorView仅作为MediaController初始定位的参考,但实际上在顶部创建了一个新的浮动Window

所以我想要的是一个真正绑定到父View (例如VideoView)的MediaController

例如,如果您在ScrollView有一个LinearLayout ,并且您必须向下滚动到MediaController所连接的VideoViewMediaController应该真正附加到此VideoView以便MediaControllerVideoView一起滚动。

这里讨论了这个问题困扰的另一个用例,其中MediaControllerViewPager

那么如何为MediaController实现这样的行为呢?

我最后做了一个肮脏的黑客……我只是手动将视图附加到我的videoView来实现想要的行为:

 public void onPrepared(MediaPlayer mp) { MediaController mc = new MediaController(videoView.getContext(), false); // set correct height RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) videoView.getLayoutParams(); params.height = mp.getVideoHeight(); videoView.setLayoutParams(params); videoView.setMediaController(mc); pBar.setVisibility(View.GONE); mc.show(0); FrameLayout f = (FrameLayout) mc.getParent(); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_BOTTOM, videoView.getId()); ((LinearLayout) f.getParent()).removeView(f); ((RelativeLayout) videoView.getParent()).addView(f, lp); mc.setAnchorView(videoView); } 

这个解决方案的问题是,设置anchorView没有任何效果,因此点击VideoView不会隐藏/显示MediaController因为它应该。

绝对是一个更好的解决方案,希望有人可以给我一个提示!

只想添加到DERIIIFranz的答案。 我使用相同的方法将媒体控制器分配给我想要的视图,并使Hide()和Show()函数正常工作我只是创建了自己的MediaController类并覆盖了Hide()和Show()方法作为isShowing属性(我在C#中使用Xamarin进行此操作,因此我不知道您使用Java会遇到什么问题)。

我还在VideoView上添加了我自己的点击监听器,以确保我自己可以处理Hide()和Show()事件。

 public class MyMediaController : MediaController { private bool _isShowing { get; set; } = false; public override bool IsShowing { get { return _isShowing; } } public override void Show () { base.Show(); _isShowing = true; Native.ViewGroup parent = ((Native.ViewGroup)this.Parent); parent.Visibility = Native.ViewStates.Visible; } public override void Hide () { base.Hide(); _isShowing = false; Native.ViewGroup parent = ((Native.ViewGroup)this.Parent); parent.Visibility = Native.ViewStates.Gone; } } 

将Jonathan Hockman的答案转换为Java并将其添加到DERIIIFranz的答案中:

 public class MyMediaController extends MediaController { public MyMediaController(Context context) { super(context); } public MyMediaController(Context context, boolean useFastForward) { super (context, useFastForward); } public MyMediaController(Context context, AttributeSet attrs) { super(context, attrs); } private boolean _isShowing = false; @Override public boolean isShowing() { return _isShowing; } @Override public void show() { super.show(); _isShowing = true; ViewGroup parent = (ViewGroup) this.getParent(); parent.setVisibility(View.VISIBLE); } @Override public void hide() { super.hide(); _isShowing = false; ViewGroup parent = (ViewGroup) this.getParent(); parent.setVisibility(View.GONE); } } 

对于videoView:

  public void onPrepared(MediaPlayer mediaPlayer) { MyMediaController mediaController = new MyMediaController(videoView.getContext(), false); RelativeLayout parentLayout = (RelativeLayout) videoView.getParent(); RelativeLayout.LayoutParams parentParams = (RelativeLayout.LayoutParams) parentLayout.getLayoutParams(); parentParams.height = this.getHeight(); parentLayout.setLayoutParams(parentParams); FrameLayout frameLayout = (FrameLayout) mediaController.getParent(); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, this.getId()); ((ViewGroup)frameLayout.getParent()).removeView(frameLayout); parentLayout.addView(frameLayout, layoutParams); mediaController.setAnchorView(this); mediaController.hide(); videoView.setMediaController(mediaController); }