package com.anything.sample; import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.util.AttributeSet; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import java.io.File; public class SurfaceViewDemo2 extends SurfaceView implements SurfaceHolder.Callback{ private SurfaceHolder mSurfaceHolder; private MediaPlayer mediaPlayer; public SurfaceViewDemo2(Context context) { this(context,null,0); } public SurfaceViewDemo2(Context context, AttributeSet attrs) { this(context, attrs,0); } public SurfaceViewDemo2(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mSurfaceHolder = getHolder(); mSurfaceHolder.addCallback(this); setFocusable(true); setFocusableInTouchMode(true); this.setKeepScreenOn(true); setZOrderOnTop(true); } @Override public void surfaceCreated(SurfaceHolder holder) { Log.i("guohao","=========surfaceCreated========"); //start_pause_switch(); } @override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.i("guohao","=========surfaceChanged========"); } @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.i("guohao","=========surfaceDestroyed========"); if (mediaPlayer ! = null ){ stop(); } } protected void stop() { if (mediaPlayer ! = null && mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } } protected void play() { String path = "/storage/emulated/0/Movies/weiboIntl_video/4575226783080890_weibo.mp4"; File file = new File(path); if (! file.exists()) { return; } try { mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); / / set play video source / / mediaPlayer setDataSource (file. The getAbsolutePath ()); . / / the problem encountered authority mediaPlayer setDataSource (getContext (), Uri, parse (" content: / / media/external/video/media / 126591 ")); // Set the SurfaceHolder mediaPlayer.setdisplay (getHolder()) to display the video; mediaPlayer.prepareAsync(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer {log. I ("guohao"," resolution: "+ mp.getVideowidth () + "x" + mp.getVideoHeight()); Log.i("guohao"," duration: "+ mp.getduration ()); Log. I ("guohao"," start playing "); mediaPlayer.start(); }}); mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { replay(); }}); mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { play(); return false; }}); New Thread() {@override public void run() {try {while (true) { Milliseconds to update a progress bar int current = mediaPlayer. GetCurrentPosition (); Log.i("guohao","CurrentPosition() = " + current); sleep(500); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } catch (Exception e) { e.printStackTrace(); } } protected void replay() { if (mediaPlayer! =null){ mediaPlayer.start(); }else{ play(); }} public void play_or_pause() {if (mediaPlayer! = null && mediaPlayer.isPlaying()) { mediaPlayer.pause(); }else if(mediaPlayer ! = null){ mediaPlayer.start(); }else{ new Thread(new Runnable() { @Override public void run() { play(); } }).start(); }}}Copy the code