Android uses surfaceview + mediaplayer to play videos

There are two main ways to play video in Android:

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

Steps:

1. Call the player. Setdatasource () method to set the resource to be played, which can be a file, a file path, or a URL. 2. Call mediaplayer.setdisplay (holder) to set the surfaceholder, which can be obtained through the getholder () method of surfaceview. 3. Call mediaplayer. Prepare() to prepare. 4. Call mediaplayer. Start() to play the video.

This is a rough step, but only these are not enough

Before the second step, you need to ensure that the surfaceholder is ready. Therefore, you need to set a callback for 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 the surfaceholder is created. Here you can do some initialization operations. Surfacecreated() will call back when the surfaceholder is destroyed. Here you can do some operations to release resources to prevent memory leakage.

Generally, the surfaceholder will be 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);
   //视频链接可能已失效
   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 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 it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>