android Plays Video Using surfaceview+MediaPlayer

  • 2021-11-13 18:22:06
  • OfStack

There are two main ways to play video in Android:

Use its own player. Specify Action as ACTION_VIEW, Data as Uri, and Type as its MIME type Use VideoView, which comes with android. This method is too simple to introduce Using SurfaceView + MediaPlayer, this method has a better effect, which is also highlighted here

SurfaceView has been available since android 1.0, and 10 points is easy to use. 1 Generally speaking, UI refreshes need to be done in UI threads, but surfaceview refreshes can be done in non-UI threads. In this way, it is very convenient. For example, play online does not need to write handler to realize the communication between two threads, and can directly play videos in non-UI threads.

Steps:

1. Call the player. setDataSource () method to set the resource to play, which can be a file, a file path, or URL.
2. Call MediaPlayer. setDisplay (holder) to 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.

These are rough steps, but these alone are not enough

Before Step 2, you need to make sure that surfaceHolder is ready. Therefore, it is necessary to set up an callback for 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 be created in SurfaceHolder callback, here can do some initialization operations, surfaceDestroyed () will be destroyed in SurfaceHolder callback, here can do some release of resources operations to prevent memory leaks.

1, surfaceHolder is set for MediaPlayer in surfaceCreated.


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

Attach the specific code 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);
   // Video link may have expired  
   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>

Related articles: