A Brief analysis of Android's MediaPlayer class

  • 2020-06-07 05:17:13
  • OfStack

MediaPlayer is commonly used in Android to play some media files. For audio files, you can simply use MeidaPlayer combined with a few lines of code, but for video files it is a little more complicated. MediaPlayer alone can only play audio files, and SurfaceView is needed to match the display to play video. For SurfacView, the separation mechanism of display and control is adopted in Android, that is, SurfaceView is only responsible for displaying the screen, but not controlling the video stream. Therefore, SurfaceHolder is also needed to control the video stream. The key codes are as follows:


MediaPlayer player = new MediaPlayer();
SurfaceView view = (SurfaceView)findViewById(R.id.surfaceview);
SurfaceHolder holder = view.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
player.setDisplay(holder);


Related articles: