Trouble getting video to play on Recyclerview

前端 未结 2 783
北恋
北恋 2021-02-06 09:52

I have a VideoView inside a RecyclerView. I want to eventually have a list of videos to play on a Recyclerview. I decided to start out with one video, then move on to having mul

2条回答
  •  日久生厌
    2021-02-06 10:48

    I was stuck on this for far too long as well. After searching everywhere and still struggling I looked at my older code base with the exact same code bar one difference. The VideoView was having its height programatically set. android:layout_height="wrap_content" doesn't appear to make a height adjustment.

    So I used a self made utility method to get the screen width and set the height to be in a 16:9 ratio like so. (Maybe just set your height and width to some arbitrary numbers first to see if it's the issue)

    public static int getScreenWidth(Context c) {
        int screenWidth = 0; // this is part of the class not the method 
        if (screenWidth == 0) {
            WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            screenWidth = size.x;
        }
    
        return screenWidth;
    }
    

    Then all it takes is to set the VideoViews height as such

    holder.mVideoView.getLayoutParams().height = getScreenWidth(mContext) * 9 /16;
    

    NOTE: My width is set to match_parent, so wrap_content may also fail there too.

    Hope this helps.

提交回复
热议问题