SoFunction
Updated on 2025-04-09

Android video playback FAQ summary

In Android development, video playback problems are common. In regular videos, videoView + MediaController or mediaController + serfercie holder are used directly.

Frequently Asked Questions

1 How to handle the player's horizontal screen switching and the layout changes in copy display on the player during playback

Settings in activity

 android:configChanges="orientation|screenSize"

This way, the activity will not be rebuilt during rotation

Rewrite system method

public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    (newConfig);
    if ( == Configuration.ORIENTATION_LANDSCAPE) {
      initVideoLandLayout();
    } else {
      initVideoPortLayout();
    }
  }

It is determined by judging whether it is a horizontal or vertical screen. This is to determine whether it is a horizontal or vertical screen during the video watching. When we first play the video, we can also judge. The specific processing method is

 private void initVideoPlayerLayout() {
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    width = ;
    heigh = ;
    if (width / heigh > 0) { // Horizontal screen      initVideoLandLayout();
      fullscreen = true;
    }
    if (width / heigh == 0) { //Perpendicular screen      initVideoPortLayout();
    }
  }

When changing the layout on the player in detail, we can change it dynamically

 private void initVideoPortLayout() {
     videoLp = new (.MATCH_PARENT, .MATCH_PARENT);
    (RelativeLayout.CENTER_IN_PARENT);
    (videoLp);
    ();
     hotelInfoLp = new ((200), (55));
    (RelativeLayout.ALIGN_PARENT_BOTTOM);
    (RelativeLayout.ALIGN_PARENT_RIGHT);
     = (8);
     = (90);
    (hotelInfoLp);
  }

This is vertical screen processing

private void initVideoLandLayout() {
     layoutParams =
        new (.FILL_PARENT, .FILL_PARENT);
    (RelativeLayout.ALIGN_PARENT_TOP);
    (RelativeLayout.ALIGN_PARENT_LEFT);
    (RelativeLayout.ALIGN_PARENT_RIGHT);
    (layoutParams);
     hotelInfoLp = new ((200), (55));
    (RelativeLayout.ALIGN_PARENT_RIGHT);
    (RelativeLayout.ALIGN_PARENT_BOTTOM);
     = (8);
     = (95);
    (hotelInfoLp);
  }

This is horizontal screen processing

My door knows that the system's videoView control comes with playback, pause, wait for progress bar. This is the MediaContronller. When we click on the progress bar, it will be displayed. When the hand leaves the interface, it will not be displayed. This is the method in the videoView

 @Override
  public boolean onTouchEvent(MotionEvent ev) {
    if (isInPlaybackState() && mMediaController != null) {
      toggleMediaControlsVisiblity();
    }
    return false;
  }

Look down again

private void toggleMediaControlsVisiblity() {
    if (()) {
      ();
    } else {
      ();
    }
  }

The final adjustment is (); and (),

So if we implement our own layout and player progress bar in our own player interface, just rewrite the hide() and show() methods.

The above is a summary of the frequently asked questions about Android video playback that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support for my website!