Android 使用MediaPlayer与SurfaceView进行视频播放

时间:2021-01-18 11:58:35
代码 public class MainActivity extends Activity implements
OnBufferingUpdateListener, OnCompletionListener, OnErrorListener
{


SurfaceView surfaceView;


MediaPlayer mPlayer;
// 记录当前视频的播放位置
int position;


@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);


setContentView(R.layout.activity_main);
// 创建MediaPlayer
mPlayer = new MediaPlayer();
surfaceView = (SurfaceView) this.findViewById(R.id.SurfaceView01);
// 设置SurfaceView自己不管理的缓冲区
surfaceView.getHolder()
.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 设置播放时打开屏幕
surfaceView.getHolder().setKeepScreenOn(true);
surfaceView.getHolder().addCallback(new SurfaceListener());
}


private void play() throws IOException
{
mPlayer.reset();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
// 设置需要播放的视频
AssetFileDescriptor fileDescriptor = getAssets().openFd("welcome.mp4");
mPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),fileDescriptor.getLength());

// 把视频画面输出到SurfaceView
mPlayer.setDisplay(surfaceView.getHolder());
mPlayer.prepare();
mPlayer.start();
}


 // 当其他Activity被打开,暂停播放
    @Override
    protected void onPause()
    {
        if (mPlayer.isPlaying())
        {
            // 保存当前的播放位置
            position = mPlayer.getCurrentPosition();
            mPlayer.stop();
        }
        super.onPause();
    }
    @Override
    protected void onDestroy()
    {
        // 停止播放
        if (mPlayer.isPlaying())
            mPlayer.stop();
        // 释放资源
        mPlayer.release();
        super.onDestroy();
    }
    
    
    private class SurfaceListener implements SurfaceHolder.Callback
    {
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height)
        {
        }


        @Override
        public void surfaceCreated(SurfaceHolder holder)
        {
             try
                {
                    // 开始播放
                    play();
                    // 并直接从指定位置开始播放
                    mPlayer.seekTo(position);
                  
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            
        }
        @Override
        public void surfaceDestroyed(SurfaceHolder holder)
        {
        }
    }


@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2)
{
// TODO Auto-generated method stub
return false;
}


@Override
public void onCompletion(MediaPlayer arg0)
{
// TODO Auto-generated method stub


}


@Override
public void onBufferingUpdate(MediaPlayer arg0, int arg1)
{
// TODO Auto-generated method stub


}


}