android 全屏视频播放(SurfaceView + MediaPlayer)

介绍个第三方:

JieCaoVideoPlayer

  实现Android的全屏视频播放,支持完全自定义UI、手势修改进度和音量、hls、rtsp,设置http头信息,也能在ListView、ViewPager和ListView、ViewPager和Fragment等多重嵌套模式下全屏工作,占用空间非常小,不到60k。

本次是通过 SurfaceView + MediaPlayer 实现 横屏 全屏 播放。

//注意修改:

WindowFeature下的:Window.FEATURE_NO_TITLE, Window.FEATURE_INDETERMINATE_PROGRESS

Fullscreen

AndroidManifest.xml下activity节点添加android:screenOrientation="landscape"

1)通过 AudioManager 控制视频声音。

2)通过getWindow().getAttributes().screenBrightness 获取当前亮度,通过继承SimpleOnGestureListener手势监听onScroll,onSingleTapUp事件,

重新生成

 WindowManager.LayoutParams lpa = getWindow().getAttributes();
if (lpa.screenBrightness > 1.0f)
lpa.screenBrightness = 1.0f;
else if (lpa.screenBrightness < 0.01f)
lpa.screenBrightness = 0.01f; getWindow().setAttributes(lpa);

3)activity oncreate 进行初始化 MediaPlayer, SurfaceView Holder

 if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
// 不维持自身缓冲区,直接显示
// this value only use Hardware decoder before Android 2.3
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
&& mHardwareDecoder) {
sv_videoplayer.getHolder().setType(
SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
sv_videoplayer.getHolder().setFixedSize(
ScreenUtils.getScreenHeight(this),
ScreenUtils.getScreenWidth(this)); sv_videoplayer.getHolder().setFormat(PixelFormat.RGBA_8888); sv_videoplayer.getHolder().setKeepScreenOn(true);
sv_videoplayer.getHolder().addCallback(new SurFaceCallback());
}

4)调用 MediaPlayer 进行播放

 mediaPlayer.reset();
mediaPlayer.setDataSource(playUrl);
mediaPlayer.setDisplay(sv_videoplayer.getHolder());
// 缓冲
mediaPlayer.prepareAsync();
mediaPlayer
.setOnBufferingUpdateListener(new BufferingUpdateListener());
mediaPlayer.setOnPreparedListener(new PreparedListener(position));
mediaPlayer.setOnCompletionListener(new CompletionListener());

5)在缓冲就绪初始化MediaController

 /**
* //调用该方法,MediaPlayer就进入了"准备就绪" //状态,准备开始播放. //此处可用于动态设置SurfaceView的宽和高
*
*/
private final class PreparedListener implements OnPreparedListener {
private int position; public PreparedListener(int position) {
this.position = position;
} @Override
public void onPrepared(MediaPlayer arg0) {
// TODO Auto-generated method stub
//MediaController进行设置和使用
mMediaController.setMediaPlayer(VideoPlayerActivity.this);
mMediaController.setAnchorView(findViewById(R.id.rl_video_player));
mMediaController.setEnabled(true);
mMediaController.show();
mediaPlayer.start();
mProgressBar.setVisibility(View.GONE);
mLoadRate.setVisibility(View.GONE);
if (position > 0)
mediaPlayer.seekTo(position);
} }

6)显示缓冲百分比

 private final class BufferingUpdateListener implements
OnBufferingUpdateListener { @Override
public void onBufferingUpdate(MediaPlayer arg0, int percent) {
// TODO Auto-generated method stub
mLoadRate.setText(percent + "%");
}
}

7)使用getHolder().addCallback(new SurFaceCallback()),继承Callback,

SurfaceView 所在Activity 离开了前台,SurfaceView会被destory。当Activity又重新

回到前台时,SurfaceView会被重新创建,SurfaceView是在onResume()方法之后被创建。

防止视频过程中,电话来到,导致视频播放失败。

 private final class SurFaceCallback implements Callback {

         @Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) { } @Override
public void surfaceCreated(SurfaceHolder arg0) {
if (mediaPlayer == null) {
return;
}
if (playUrl != null && position > 0) {
play(position);
position = 0;
}
} @Override
public void surfaceDestroyed(SurfaceHolder arg0) {
if (mediaPlayer == null) {
return;
}
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
}
}
上一篇:struts2.1.6教程三、在Action获取Scope对象


下一篇:Struts2 二、为Action的参数注入值