第一步:配置Vitamio
app目录下的build.gradle,在dependencies 中添加依赖 compile project(‘:vitamio’)
第二步:AndroidManifest配置
android:name="android.permission.WAKE_LOCK" />
android:name="android.permission.INTERNET" />
android:name="android.permission.ACCESS_NETWORK_STATE" />
android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:name="io.vov.vitamio.activity.InitActivity" android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation" android:launchMode="singleTop" android:theme="@android:style/Theme.NoTitleBar" android:windowSoftInputMode="stateAlwaysHidden" />
第三步:播放视频
public class MainActivity extends Activity { private VideoView mVideoView; private TextView downloadRateView, loadRateView; private ProgressBar pb; private CustomMediaController mCustomMediaController; private String url1 = "http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/hc.yinyuetai.com/D046015255134077DDB3ACA0D7E68D45.flv"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //定义全屏参数 int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN; //获得当前窗体对象 Window window = MainActivity.this.getWindow(); //设置当前窗体为全屏显示 window.setFlags(flag, flag); // //检查vitamio框架是否可用 // if (!LibsChecker.checkVitamioLibs(this)) { // return; // } setContentView(R.layout.activity_main); pb = (ProgressBar) findViewById(R.id.probar); downloadRateView = (TextView) findViewById(R.id.download_rate); loadRateView = (TextView) findViewById(R.id.load_rate); mVideoView = (VideoView) findViewById(R.id.buffer); mCustomMediaController = new CustomMediaController(this,mVideoView,this); mCustomMediaController.setVideoName("创利丰"); mCustomMediaController.show(5000); //5s隐藏 //初始化加载库文件 if (Vitamio.isInitialized(this)) { mVideoView.setVideoURI(Uri.parse(url1)); mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH); mVideoView.setMediaController(mCustomMediaController); mVideoView.setBufferSize(10240); //设置视频缓冲大小 mVideoView.requestFocus(); mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { // optional need Vitamio 4.0 mediaPlayer.setPlaybackSpeed(1.0f); } }); mVideoView.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() { @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { loadRateView.setText(percent + "%"); } }); mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() { @Override public boolean onInfo(MediaPlayer mp, int what, int extra) { switch (what) { //开始缓冲 case MediaPlayer.MEDIA_INFO_BUFFERING_START: if (mVideoView.isPlaying()) { mVideoView.pause(); pb.setVisibility(View.VISIBLE); downloadRateView.setText(""); loadRateView.setText(""); downloadRateView.setVisibility(View.VISIBLE); loadRateView.setVisibility(View.VISIBLE); } break; //缓冲结束 case MediaPlayer.MEDIA_INFO_BUFFERING_END: mVideoView.start(); pb.setVisibility(View.GONE); downloadRateView.setVisibility(View.GONE); loadRateView.setVisibility(View.GONE); break; //正在缓冲 case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED: downloadRateView.setText("" + extra + "kb/s" + " "); break; } return true; } }); } } @Override public void onConfigurationChanged(Configuration newConfig) { //屏幕切换时,设置全屏 if (mVideoView != null){ mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_SCALE, 0); } super.onConfigurationChanged(newConfig); } }
视频界面:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> android:id="@+id/buffer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="horizontal"> android:id="@+id/probar" style="?android:attr/progressBarStyleLarge" android:layout_width="50dp" android:layout_height="50dp" /> android:id="@+id/download_rate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="" android:textColor="#FFFFFF" /> android:id="@+id/load_rate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="" android:textColor="#FFFFFF" />
视频控制器:
/** * 自定义视频控制器 */ public class CustomMediaController extends MediaController implements View.OnClickListener{ //控制提示窗口的显示 private static final int HIDEFRAM = 0; private GestureDetector mGestureDetector; //返回按钮 private ImageButton img_back; //文件名 private TextView mFileName; private VideoView videoView; private Activity activity; private Context context; //视频名称 private String videoname; //设置mediaController高度为了使横屏时top显示在屏幕顶端 private int controllerWidth = 0; private View mVolumeBrightnessLayout; //提示窗口 private ImageView mOperationBg; //提示图片 private TextView mOperationTv; //提示文字 private AudioManager mAudioManager; //最大声音 private int mMaxVolume; // 当前声音 private int mVolume = -1; //当前亮度 private float mBrightness = -1f; private Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case HIDEFRAM: //隐藏提示窗口 mVolumeBrightnessLayout.setVisibility(View.GONE); break; } } }; //videoview 用于对视频进行控制的等,activity为了退出 public CustomMediaController(Context context, VideoView videoView, Activity activity) { super(context); this.context = context; this.videoView = videoView; this.activity = activity; WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); controllerWidth = wm.getDefaultDisplay().getWidth(); mGestureDetector = new GestureDetector(context, new MyGestureListener()); } @Override protected View makeControllerView() { //此处的mymediacontroller为我们自定义控制器的布局文件名称 View v = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.mymediacontroller, this); v.setMinimumHeight(controllerWidth); //获取控件 img_back = (ImageButton) v.findViewById(R.id.mediacontroller_top_back); mFileName = (TextView) v.findViewById(R.id.mediacontroller_filename); if (mFileName != null) { mFileName.setText(videoname); } mVolumeBrightnessLayout = (RelativeLayout) v.findViewById(R.id.operation_volume_brightness); mOperationBg = (ImageView) v.findViewById(R.id.operation_bg); mOperationTv = (TextView) v.findViewById(R.id.operation_tv); //声音控制 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //注册事件监听 img_back.setOnClickListener(this); return v; } //返回监听 @Override public void onClick(View v) { switch (v.getId()){ case R.id.mediacontroller_top_back: if (activity != null) { activity.finish(); } break; } } @Override public boolean dispatchKeyEvent(KeyEvent event) { System.out.println("MYApp-MyMediaController-dispatchKeyEvent"); return true; } @Override public boolean onTouchEvent(MotionEvent event) { if (mGestureDetector.onTouchEvent(event)) return true; // 处理手势结束 switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_UP: endGesture(); break; } return super.onTouchEvent(event); } /** * 手势结束 */ private void endGesture() { mVolume = -1; mBrightness = -1f; //隐藏 myHandler.removeMessages(HIDEFRAM); myHandler.sendEmptyMessageDelayed(HIDEFRAM, 500); } private class MyGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onSingleTapUp(MotionEvent e) { return false; } /** * 因为使用的是自定义的mediaController 当显示后,mediaController会铺满屏幕, * 所以VideoView的点击事件会被拦截,所以重写控制器的手势事件, * 将全部的操作全部写在控制器中, * 因为点击事件被控制器拦截,无法传递到下层的VideoView, * 所以原来的单击隐藏会失效,作为代替, * 在手势监听中onSingleTapConfirmed()添加自定义的隐藏/显示, * * @param e * @return */ @Override public boolean onSingleTapConfirmed(MotionEvent e) { //当手势结束,并且是单击结束时,控制器隐藏/显示 toggleMediaControlsVisiblity(); return super.onSingleTapConfirmed(e); } @Override public boolean onDown(MotionEvent e) { return true; } //滑动事件监听 @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { float mOldX = e1.getX(), mOldY = e1.getY(); int y = (int) e2.getRawY(); int x = (int) e2.getRawX(); Display disp = activity.getWindowManager().getDefaultDisplay(); int windowWidth = disp.getWidth(); int windowHeight = disp.getHeight(); if (mOldX > windowWidth * 3.0 / 4.0) { //右边滑动 超过屏幕右侧3/4 onVolumeSlide((mOldY - y) / windowHeight); } else if (mOldX < windowWidth * 1.0 / 4.0) { //左边滑动 不超过屏幕左侧1/4 onBrightnessSlide((mOldY - y) / windowHeight); } return super.onScroll(e1, e2, distanceX, distanceY); } @Override public boolean onDoubleTap(MotionEvent e) { playOrPause(); return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return super.onFling(e1, e2, velocityX, velocityY); } } /** * 滑动改变声音大小 * * @param percent */ private void onVolumeSlide(float percent) { if (mVolume == -1) { mVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); if (mVolume < 0) mVolume = 0; // 显示 mVolumeBrightnessLayout.setVisibility(View.VISIBLE); } int index = (int) (percent * mMaxVolume) + mVolume; if (index > mMaxVolume) { index = mMaxVolume; } else if (index < 0) { index = 0; } //变更声音 mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0); mOperationTv.setText((int) (((double) index / mMaxVolume) * 100) + "%"); if (index >= 10) { mOperationBg.setImageResource(R.mipmap.video_volumn_bg); } else if (index >= 5 && index < 10) { mOperationBg.setImageResource(R.mipmap.video_volumn_bg); } else if (index > 0 && index < 5) { mOperationBg.setImageResource(R.mipmap.video_volumn_bg); } else { mOperationBg.setImageResource(R.mipmap.video_volumn_bg); } } /** * 滑动改变亮度 * * @param percent */ private void onBrightnessSlide(float percent) { if (mBrightness < 0) { mBrightness = activity.getWindow().getAttributes().screenBrightness; if (mBrightness <= 0.00f) mBrightness = 0.50f; if (mBrightness < 0.01f) mBrightness = 0.01f; mVolumeBrightnessLayout.setVisibility(View.VISIBLE); } WindowManager.LayoutParams lpa = activity.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; } //变更亮度 activity.getWindow().setAttributes(lpa); mOperationTv.setText((int) (lpa.screenBrightness * 100) + "%"); if (lpa.screenBrightness * 100 >= 90) { mOperationBg.setImageResource(R.mipmap.light_100); } else if (lpa.screenBrightness * 100 >= 80 && lpa.screenBrightness * 100 < 90) { mOperationBg.setImageResource(R.mipmap.light_100); } else if (lpa.screenBrightness * 100 >= 70 && lpa.screenBrightness * 100 < 80) { mOperationBg.setImageResource(R.mipmap.light_100); } else if (lpa.screenBrightness * 100 >= 60 && lpa.screenBrightness * 100 < 70) { mOperationBg.setImageResource(R.mipmap.light_100); } else if (lpa.screenBrightness * 100 >= 50 && lpa.screenBrightness * 100 < 60) { mOperationBg.setImageResource(R.mipmap.light_100); } else if (lpa.screenBrightness * 100 >= 40 && lpa.screenBrightness * 100 < 50) { mOperationBg.setImageResource(R.mipmap.light_100); } else if (lpa.screenBrightness * 100 >= 30 && lpa.screenBrightness * 100 < 40) { mOperationBg.setImageResource(R.mipmap.light_100); } else if (lpa.screenBrightness * 100 >= 20 && lpa.screenBrightness * 100 < 20) { mOperationBg.setImageResource(R.mipmap.light_100); } else if (lpa.screenBrightness * 100 >= 10 && lpa.screenBrightness * 100 < 20) { mOperationBg.setImageResource(R.mipmap.light_100); } } /** * 设置视频文件名 * * @param name */ public void setVideoName(String name) { videoname = name; if (mFileName != null) { mFileName.setText(name); } } /** * 隐藏或显示 */ private void toggleMediaControlsVisiblity() { if (isShowing()) { hide(); } else { show(); } } /** * 播放/暂停 */ private void playOrPause() { if (videoView != null) if (videoView.isPlaying()) { videoView.pause(); } else { videoView.start(); } } }
视频控制器界面:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="34dp" android:background="#77000000"> android:id="@+id/mediacontroller_top_back" android:layout_width="50dp" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:background="@null" android:src="@mipmap/ic_player_close_white" /> android:id="@+id/mediacontroller_filename" style="@style/MediaController_Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/mediacontroller_top_back" android:ellipsize="marquee" android:singleLine="true" android:text="file name" /> android:id="@+id/mediacontroller_share" android:layout_width="50dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:background="@null" android:src="@drawable/ic_launcher" /> android:id="@+id/mediacontroller_favorite" android:layout_width="50dp" android:layout_height="match_parent" android:layout_toLeftOf="@id/mediacontroller_share" android:background="@null" android:src="@drawable/ic_launcher" /> RelativeLayout> android:id="@+id/play_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@null" android:src="@drawable/mediacontroller_play" android:visibility="gone"/> <RelativeLayout android:id="@+id/operation_volume_brightness" android:layout_width="150dp" android:layout_height="75dp" android:layout_centerInParent="true" android:background="#77000000" android:orientation="horizontal" android:padding="0dip" android:visibility="gone"> android:id="@+id/operation_bg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/video_volumn_bg" /> android:id="@+id/operation_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/operation_bg" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:textColor="#ffffff" android:textSize="12sp" /> RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="#77000000"> android:id="@+id/mediacontroller_play_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" /> android:id="@+id/mediacontroller_time_current" style="@style/MediaController_Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:text="33:33:33" android:layout_alignBaseline="@+id/mediacontroller_time_total" android:layout_alignBottom="@+id/mediacontroller_time_total" android:layout_toRightOf="@id/mediacontroller_play_pause" /> android:id="@+id/mediacontroller_time_total" style="@style/MediaController_Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:text="33:33:33" /> android:id="@+id/mediacontroller_seekbar" style="@style/MediaController_SeekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@id/mediacontroller_time_total" android:layout_toRightOf="@id/mediacontroller_time_current" android:focusable="true" android:max="1000" /> RelativeLayout> RelativeLayout>