Android MediaController VideoView-加载时始终显示,尽管选项卡不正确

时间:2018-07-10 04:52:16

标签: android android-videoview mediacontroller

我有一个带有不同标签的标签布局,其中包括显示视频列表的标签。在“视频”标签中,我使用videoview来显示视频并为其设置媒体控制器。

结果:

enter image description here

但是当我参加另一项活动时,又回到原来的活动。即使我站在文本标签而非视频标签上,媒体控制器也会自动弹出

enter image description here

您能告诉我如何只在tabvideo中显示mediacontroller,停止在类似位置显示吗?

好吧,我使用recyclerview显示视频列表,这是我的适配器

#description {
    display: table;
    width: 100%;
}

我认为问题出在@Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) { VideoModel item = VideoList.get(position); ((VHItem) holder).tvTittle.setText(item.getVideoTittle()); ((VHItem) holder).tvTime.setText(item.getVideoTime()); /***get clicked view and play video url at this position**/ try { //play video using android api, when video view is clicked. String url = item.getVideoUrl(); // your URL here Uri videoUri = Uri.parse(url); ((VHItem) holder).videoView.setVideoURI(videoUri); //((VHItem) holder).videoView.seekTo(10); ((VHItem) holder).videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() { @Override public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { /* * add media controller */ MediaController mc = new MediaController(mContext); mc.setMediaPlayer(((VHItem) holder).videoView); ((VHItem) holder).videoView.setMediaController(mc); /* * and set its position on screen */ mc.setAnchorView(((VHItem) holder).videoView); } }); ((VHItem) holder).videoView.seekTo(10); } }); } catch ( Exception e) { e.printStackTrace(); } } 正确吗?

谢谢您的回答

2 个答案:

答案 0 :(得分:0)

VideoView videoHolder = new VideoView(contex);
    MediaController controller=new MediaController(this);
    videoHolder.setMediaController(controller);

    controller.setVisibility(View.GONE);

以上代码将隐藏您的MediaController。

答案 1 :(得分:0)

1.setAnchorView()使用VideoView的父级来覆盖媒体控件。如果您有固定高度的视频,请确保将其放在框架布局中,以使媒体控件位于视频的顶部。
2.问题可能是您只在setAnchorView()内使用setOnVideoSizeChangedListener。请尝试以下操作:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
    VideoModel item = VideoList.get(position);
    ((VHItem) holder).tvTittle.setText(item.getVideoTittle());
    ((VHItem) holder).tvTime.setText(item.getVideoTime());
    /***get clicked view and play video url at this position**/
    try {
        //play video using android api, when video view is clicked.
        String url = item.getVideoUrl(); // your URL here
        Uri videoUri = Uri.parse(url);
        ((VHItem) holder).videoView.setVideoURI(videoUri);
        //((VHItem) holder).videoView.seekTo(10);
         if (mc == null) {//declare a private variable like "  private MediaController mc;" in the beginning of your adapter class
        mc = new MediaController(mContex);
         mc.setAnchorView(((VHItem) holder).videoView);
        ((VHItem) holder).videoView.setMediaController(mc);                                                                           
         }              
        ((VHItem) holder).videoView.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {


                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {



                        mc.setAnchorView(((VHItem) holder).videoView);



                    }
                });

                ((VHItem) holder).videoView.seekTo(10);
            }
        });


    } catch (
            Exception e)

    {
        e.printStackTrace();
    }

}