SoFunction
Updated on 2025-03-11

Android multimedia tutorial: Four ways to play videos

This article mainly introduces four methods of playing videos on Android. We will share them for your reference and learning. Let’s take a look at the detailed introduction together:

1. Call the system's own player through intent

  Uri uri = ("/storage/emulated/0/DCIM/Camera/20170521_200117.mp4");  
 //Calling the system's own player  Intent intent = new Intent(Intent.ACTION_VIEW); 
  (uri, "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4"); 
  startActivity(intent);

2. Use VideoView

Layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:andro
 xmlns:tools="/tools"
 android:
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <VideoView
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>


</RelativeLayout>

Activity

public class VideoPlayByVVActivity extends AppCompatActivity {

 private VideoView mVideoView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE); // Remove title// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //Set full screen// getWindow().addFlags(.FLAG_KEEP_SCREEN_ON); //Set the screen always on  setContentView(.activity_video_play_by_vv);
  mVideoView = (VideoView) findViewById(.video_view);

  init();
 }

 private void init() {

  String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4";

  Uri uri = (path);

  (path);

  ();
  ();

 }
}

3. MediaPlayer + SurfaceView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:andro
 xmlns:tools="/tools"
 android:
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <SurfaceView
  android:
  android:layout_width="180dp"
  android:layout_height="wrap_content"/>
 <LinearLayout
  android:layout_alignParentBottom="true"
  android:layout_width="match_parent"
  android:layout_height="40dp">


  <Button
   android:
   android:text="stop"
   android:layout_weight="1"
   android:layout_width="0dp"
   android:layout_height="match_parent"/>
  <Button
   android:
   android:text="play"
   android:layout_weight="1"
   android:layout_width="0dp"
   android:layout_height="match_parent"/>
  <Button
   android:
   android:text="pasue"
   android:layout_weight="1"
   android:layout_width="0dp"
   android:layout_height="match_parent"/>

 </LinearLayout>
</RelativeLayout>

Activity

public class VideoPlayBySurActivity extends AppCompatActivity implements  {

 private SurfaceView mSurfaceView;
 private MediaPlayer mMediaPlayer;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_video_play_by_sur);
  mSurfaceView = (SurfaceView) findViewById(.surface_view);
  findViewById().setOnClickListener(this);
  findViewById().setOnClickListener(this);

  findViewById().setOnClickListener(this);

  init();
 }

 private void init() {
  mMediaPlayer = new MediaPlayer();

  ().addCallback(new () {
   @Override
   public void surfaceCreated(SurfaceHolder holder) {
    play();
   }

   @Override
   public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

   }

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {

   }
  });
 }

 @Override
 public void onClick(View v) {
  switch (()){
   case :
    stop();
    break;
   case :
    if(!()){
     play();
    }
    break;
   case :
    pasue();
    break;

  }
 }


 public void stop(){
  if(()){
   ();
  }
 }

 public void pasue(){
  if(()){
   ();
  }else{
   ();
  }
 }

 public void play(){

  String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4";
  try {
   ();
   (AudioManager.STREAM_MUSIC);
   //Set the videos to be played   (this, (path));
   //Output the video to SurfaceView   (());
   ();
   ();

  } catch (IOException e) {
   ();
  }
 }
}

4. MediaPlayer + TextureView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:andro
 xmlns:tools="/tools"
 android:
 android:layout_width="match_parent"
 android:layout_height="match_parent">


 <TextureView
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>

 <ImageView
  android:
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:src="@drawable/all_darkbackground"/>
</RelativeLayout>

Activity

public class VideoPlayByTextrueViewActivity extends AppCompatActivity implements , ,  {

 private TextureView mTextureView;
 private ImageView mImageVideo;
 private Surface mSurface;
 private MediaPlayer mMediaPlayer;
 private static String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_video_play_by_textrue_view);

  mTextureView = (TextureView) findViewById(.texture_view);

  mImageVideo = (ImageView) findViewById(.video_image);

  init();
 }

 private void init() {

  (new () {
   @Override
   public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
    mSurface = new Surface(surfaceTexture);

    ("tag", "---- onSurfaceTextureAvailable");

    play();
   }

   @Override
   public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    ("tag", "---- onSurfaceTextureSizeChanged");
   }

   @Override
   public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    mTextureView=null;
    mSurface=null;
    ();
    ();
    return false;
   }

   @Override
   public void onSurfaceTextureUpdated(SurfaceTexture surface) {

   }
  });


 }



 public void play(){


  mMediaPlayer = new MediaPlayer();

  try {
   (getApplicationContext(), (path));
   (mSurface);
   (AudioManager.STREAM_MUSIC);

   (this);
   (this);
   (this);

   ();

  } catch (IOException e) {
   ();
  }

 }

 @Override
 public void onPrepared(MediaPlayer mp) {

  ();
  ();
 }

 @Override
 public boolean onInfo(MediaPlayer mp, int what, int extra) {
  return false;
 }

 @Override
 public void onBufferingUpdate(MediaPlayer mp, int percent) {

 }
}

Reference article

Detailed explanation of the use of SurfaceView in Android

SurfaceTexture, TextureView, SurfaceView and GLSurfaceView in Android 5.0 (Lollipop)

Android TextureView simple tutorial

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.