SoFunction
Updated on 2025-03-11

Use of Android native video playback VideoView

This example shares the specific code for Android native video playback VideoView for your reference. The specific content is as follows

Layout file activity_video.xml

<RelativeLayout xmlns:andro
 xmlns:tools="/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">
 
 <VideoView
  android:
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
 <ProgressBar
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true" />
</RelativeLayout>

Corresponding Activity:

package ;
 
import ;
import ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
 
public class VideoActivity extends AppCompatActivity {
 private ProgressBar progressBar;
 private VideoView videoView;
 private MediaController mediaController;
 private int intPositionWhenPause = -1;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_video);
 
  //Call the third-party player that comes with video playback or installed in the system//  Uri uri=("/mda-ig4tp6gnqwu5we8i/mda-ig4tp6gnqwu5we8i.mp4");
//  Intent intent=new Intent(Intent.ACTION_VIEW);
//  (uri,"video/*");
 // startActivity(intent);
 
   initVideoView();
  }
 
  /**
   * Initialize videoview playback
   */
 public void initVideoView() {
  //Initialization progress bar  progressBar = (ProgressBar) findViewById();
  //Initialize VideoView  videoView = (VideoView) findViewById();
  //Initialize the videoview control bar  mediaController = new MediaController(this);
  //Set the videoview control bar  (mediaController);
  //Set the display control bar  (0);
  //Steps after the playback is completed  (new () {
   @Override
   public void onCompletion(MediaPlayer mp) {
 
   }
  });
  //Set error listening. If you do not set the videoview, it will prompt the user for an error.  (new () {
   @Override
   public boolean onError(MediaPlayer mp, int what, int extra) {
    return false;
   }
  });
  //Set the callback function after the video file is loaded  (new () {
   @Override
   public void onPrepared(MediaPlayer mp) {
    ();
    ();
   }
  });
  //Set the videoView's click monitoring  (new () {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    return false;
   }
  });
  //Set the network video path  Uri uri = ("/mda-ig4tp6gnqwu5we8i/mda-ig4tp6gnqwu5we8i.mp4");
  (uri);
  //Set to full screen mode playback  setVideoViewLayoutParams(2);
 }
 
 /**
   * Set the videoview full screen and window modes
   *
   * @param paramsType Identification 1 is full screen mode 2 is window mode
   */
 public void setVideoViewLayoutParams(int paramsType) {
  //Full screen mode  if (1 == paramsType) {
   //Set full of the entire parent layout    LayoutParams = new (.MATCH_PARENT, .MATCH_PARENT);
   //Set the four sides aligned relative to the parent layout   (RelativeLayout.ALIGN_PARENT_BOTTOM);
   (RelativeLayout.ALIGN_PARENT_TOP);
   (RelativeLayout.ALIGN_PARENT_LEFT);
   (RelativeLayout.ALIGN_PARENT_RIGHT);
   //Add attributes for VideoView   (LayoutParams);
  } else {
   //Window mode   //Get the width and height of the entire screen   DisplayMetrics DisplayMetrics = new DisplayMetrics();
   ().getDefaultDisplay().getMetrics(DisplayMetrics);
   //Set the window mode distance 50 border   int videoHeight = ;
   int videoWidth = ;
    LayoutParams = new (videoWidth, videoHeight);
   //Set centered   (RelativeLayout.ALIGN_TOP);
   //Add attributes for VideoView   (LayoutParams);
  }
 }
 
 /**
   * Page pause effect processing
   */
 @Override
 protected void onPause() {
  ();
  //If the current page is paused, save the current playback position and save the global variable  intPositionWhenPause = ();
  //Stop playing back video files  ();
 }
 
 /**
   * Page resumes from pause
   */
 @Override
 protected void onResume() {
  ();
  //Jump to the saved position when paused  if (intPositionWhenPause &gt;= 0) {
   (intPositionWhenPause);
   //Initial playback position   intPositionWhenPause = -1;
  }
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.