Android uses surfaceview + mediaplayer to play video

Time:2021-3-19

There are two main ways to play video in Android

  • Use its own player. Specify action as action_ View, data is URI and type is its MIME type
  • Using the videoview of Android, this method is too simple to introduce
  • Using surfaceview + mediaplayer, the effect of this method is better, which is also highlighted here

Surfaceview has been available since Android 1.0, which is very easy to use. Generally speaking, UI refresh needs to be done in UI thread, but surface view can be done in non UI thread. In this way, it’s very convenient. For example, if you play online, you don’t need to write your own handler to realize the communication between two threads. You can directly play videos in non UI threads.

Steps:

1. Call player.setDataSource The () method sets the resource to play, which can be a file, a file path, or a URL.
2. Call MediaPlayer.setDisplay (holder) set surfaceholder, which can be obtained by getholder() method of surfaceview.
3. Call MediaPlayer.prepare () to prepare.
4. Call MediaPlayer.start () to play the video.

This is a general step, but these are not enough

Before the second step, you need to make sure that the surface holder is ready. Therefore, you need to set a callback to the surfaceholder,

Call the addcallback() method. Callback has three callback functions, as follows:


SurfaceHolder.Callback {
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    }

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

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }
}

Surfacecreated() will call back when surfaceholder is created. Here you can do some initialization operations. Surfacecreated() will call back when surfaceholder is destroyed. Here you can do some operations to release resources to prevent memory leakage.

Generally, the surfaceholder is set for mediaplayer in surfacecreated.


@Override
    public void surfaceCreated(SurfaceHolder holder) {
      player.setDisplay(holder);
    }

The specific code is pasted below:

public class VideoActivity extends Activity {
  private SurfaceView surfaceView;
  private MediaPlayer player;
  private SurfaceHolder holder;
  private ProgressBar progressBar;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.surfaceview_item);
    surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    progressBar= (ProgressBar) findViewById(R.id.progressBar);
   //The video link may have failed 
   String uri="http://video.dispatch.tc.qq.com/77613075/x0021o8d3g3.mp4?sdtfrom=v1001&type=mp4&vkey=23289E4B8D0F4B6CF18703222DFD0038845D8F56A75EEC20D5D4FDE678093D9AB211EFD7F4C99E5A612A96A04F46CEEB483628CFFBEA493D3AADBFCB81A540F7A92193874192FA0F70D1099DF330B2B419D45736554CB9BB3435019C985F530C5960E4B20FEBD5FAED17DC9F1FCE1C73&platform=10902&fmt=auto&sp=350&guid=1175defd049d3301e047ce50d93e9c7a";

    player=new MediaPlayer();
    try {
      player.setDataSource(this, Uri.parse(uri));
      holder=surfaceView.getHolder();
      holder.addCallback(new MyCallBack());
      player.prepare();
      player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
          progressBar.setVisibility(View.INVISIBLE);
          player.start();
          player.setLooping(true);
        }
      });
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private class MyCallBack implements SurfaceHolder.Callback {
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
      player.setDisplay(holder);
    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
  }
}

XML file:


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <SurfaceView
    android:id="@+id/surfaceView"
    android:layout_width="match_parent"
    android:layout_height="400dp" />
  <ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />
  <TextView
    android:id="@+id/numText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="3dp"
    android:layout_gravity="bottom|left"
    android:text="1"
    android:textSize="30dp"
    android:textColor="#f00"/>
</FrameLayout>

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]