有很多关于如何定位MediaController
的讨论,大多数答案都是使用setAnchorView
- 方法。乍一看这个解决方案似乎有效,但在我的情况下却没有。
根据此Post setAnchorView
仅作为MediaController
初始定位的参考,但实际上会在顶部创建一个新的浮动Window
。
所以我想要的是MediaController
真的绑定到父View
(例如VideoView)。
例如,如果LinearLayout
中有ScrollView
且您必须向下滚动到VideoView
所附的MediaController
,{{1}应该真正附加到此MediaController
,以便VideoView
与MediaController
一起滚动。
here讨论了这个问题困扰的另一个用例,其中VideoView
用于MediaController
。
那么如何为ViewPager
实现这样的行为?
答案 0 :(得分:8)
我最后做了一个肮脏的黑客...我只是手动将视图附加到我的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
因为它{39}应该是。
绝对是一个更好的解决方案,希望有人可以给我一个提示!
答案 1 :(得分:1)
只想添加到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;
}
}
答案 2 :(得分:1)
将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);
}