版权声明:本文为博主原创文章,未经博主允许不得转载。使用开源项目Vitamio(这是国产项目,有兴趣的童鞋可以去支持下)。官网地址参考博客在做的这个demo中主要功能:1.改变屏幕亮度2.改变音量大小3.改变视频尺寸4.改变进度下载地址使用过程中的小细节:1.引入权限:[html] view plain copy print?在CODE上查看代码片派生到我的代码片 这几个权限要写全,不然你会难受的。SDK的最低版本是11,这个在使用中就知道了。2.添加库文件的活动:[html] view plain copy print?在CODE上查看代码片派生到我的代码片 写进清单中,也许你还没注意这个问题。3.在配置清单中添加库:[html] view plain copy print?在CODE上查看代码片派生到我的代码片target=android-17 android.library.reference.1=../InitActivity 在提醒一句,项目路径和引用的库路径要一致。主要的代码:[html] view plain copy print?在CODE上查看代码片派生到我的代码片public class PlayAcy extends Activity { /** 当前视频路径 */ private String path = Environment.getExternalStorageDirectory() + "/hd.mp4"; /** 当前声音 */ private int mVolume = -1; /** 最大音量 */ private int mMaxVolume; /** 当前亮度 */ private float mBrightness = -1f; /** 手势数目 */ private int finNum = 0; /** 当前进度 */ private long mProgress = 0; private View mVolumeBrightnessLayout; private ImageView mOperationBg; private ImageView mOperationPercent; private VideoView mVideoView; private GestureDetector gestDetector; private ScaleGestureDetector scaleDetector; private ScreenBean screenBean; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); if (!LibsChecker.checkVitamioLibs(this)) return; requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.acy_play); init(); } private void init() { mVideoView = (VideoView) findViewById(R.id.surface_view); mVolumeBrightnessLayout = findViewById(R.id.operation_volume_brightness); mOperationBg = (ImageView) findViewById(R.id.operation_bg); mOperationPercent = (ImageView) findViewById(R.id.operation_percent); mMaxVolume = LocUtil.getMaxVolume(this); gestDetector = new GestureDetector(this, new SingleGestureListener()); scaleDetector = new ScaleGestureDetector(this, new MultiGestureListener()); screenBean = LocUtil.getScreenPix(this); if (path == "") { return; } else { mVideoView.setVideoPath(path); mVideoView.setMediaController(new MediaController(this)); mVideoView.requestFocus(); mVideoView .setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mediaPlayer.setPlaybackSpeed(1.0f); } }); } } /** 定时隐藏 */ private Handler mDismissHandler = new Handler() { @Override public void handleMessage(Message msg) { mVolumeBrightnessLayout.setVisibility(View.GONE); } }; @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub finNum = event.getPointerCount(); if (1 == finNum) { gestDetector.onTouchEvent(event); switch (event.getAction()) { case MotionEvent.ACTION_UP: endGesture(); } } else if (2 == finNum) { scaleDetector.onTouchEvent(event); } return true; } /** 手势结束 */ private void endGesture() { mVolume = -1; mBrightness = -1f; finNum = 0; mProgress = -2; // 隐藏 mDismissHandler.removeMessages(0); mDismissHandler.sendEmptyMessageDelayed(0, 500); } /** * 视频缩放 */ public void changeLayout(int size) { mVideoView.setVideoLayout(size, 0); } /** * 声音大小 * * @param percent */ public void changeVolume(float percent) { if (mVolume == -1) { mVolume = LocUtil.getCurVolume(this); if (mVolume < 0) mVolume = 0; // 显示 mOperationBg.setImageResource(R.drawable.video_volumn_bg); mVolumeBrightnessLayout.setVisibility(View.VISIBLE); } int index = (int) (percent * mMaxVolume) + mVolume; if (index > mMaxVolume) index = mMaxVolume; else if (index < 0) index = 0; // 变更声音 LocUtil.setCurVolume(this, index); // 变更进度条 ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams(); lp.width = findViewById(R.id.operation_full).getLayoutParams().width * index / mMaxVolume; mOperationPercent.setLayoutParams(lp); } /** * 亮度大小 * * @param percent */ public void changeBrightness(float percent) { if (mBrightness < 0) { mBrightness = getWindow().getAttributes().screenBrightness; if (mBrightness <= 0.00f) mBrightness = 0.50f; if (mBrightness < 0.01f) mBrightness = 0.01f; // 显示 mOperationBg.setImageResource(R.drawable.video_brightness_bg); mVolumeBrightnessLayout.setVisibility(View.VISIBLE); } WindowManager.LayoutParams lpa = getWindow().getAttributes(); lpa.screenBrightness = mBrightness + percent; if (lpa.screenBrightness > 1.0f) lpa.screenBrightness = 1.0f; else if (lpa.screenBrightness < 0.01f) lpa.screenBrightness = 0.01f; getWindow().setAttributes(lpa); ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams(); lp.width = (int) (findViewById(R.id.operation_full).getLayoutParams().width * lpa.screenBrightness); mOperationPercent.setLayoutParams(lp); } /** * 播放进度 * * @param percent */ public void changePregress(float percent) { if (mProgress < -1) { mVideoView.pause(); mProgress = mVideoView.getCurrentPosition(); } if (Math.abs(percent) > 0.1) { percent = (float) (percent / Math.abs(percent) * 0.1); } long index = (long) (percent * mVideoView.getDuration()) + mProgress; if (index > mVideoView.getDuration()) { index = mVideoView.getDuration(); } else if (index < 0) { index = 0; } mVideoView.seekTo(index); } /** * 单点触屏 * * @author jin * */ private class SingleGestureListener implements android.view.GestureDetector.OnGestureListener { @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub if (e1.getX() - e2.getX() > 100) { changePregress(-0.2f); } else if (e1.getX() - e2.getX() < -100) { changePregress(0.2f); } return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub if (2 == finNum) { return false; } float moldX = e1.getX(); float moldY = e1.getY(); float y = e2.getY(); float X = e2.getX(); Log.d("onf", distanceX); if (moldX > screenBean.getsWidth() * 9.0 / 10)// 右边滑动 changeVolume((moldY - y) / screenBean.getsHeight()); else if (moldX < screenBean.getsWidth() / 10.0)// 左边滑动 changeBrightness((moldY - y) / screenBean.getsHeight()); else changePregress((X - moldX) / screenBean.getsWidth()); return true; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } } /** * 多点缩放 * * @author jin * */ private class MultiGestureListener implements OnScaleGestureListener { @Override public boolean onScale(ScaleGestureDetector detector) { // TODO Auto-generated method stub return false; } @Override public boolean onScaleBegin(ScaleGestureDetector detector) { // TODO Auto-generated method stub // 返回true ,才能进入onscale()函数 return true; } @Override public void onScaleEnd(ScaleGestureDetector detector) { // TODO Auto-generated method stub float oldDis = detector.getPreviousSpan(); float curDis = detector.getCurrentSpan(); if (oldDis - curDis > 50) { // 缩小 changeLayout(0); Toast.makeText(PlayAcy.this, "缩小", 1000).show(); } else if (oldDis - curDis < -50) { // 放大 changeLayout(1); Toast.makeText(PlayAcy.this, "放大", 1000).show(); } } } } 效果:右侧滑动改变音量:左侧滑动改变亮度:双手缩放屏幕:播放网络视频同样,改变文件路径就是了,这里就不多说了。
Vitamio视频播放器(一)
最新推荐文章于 2018-01-24 18:19:35 发布