视频播放库Vitamio的使用以及功能扩展

news/2024/10/31 5:24:19/

概览

简介

Vitamio 能够流畅播放720P甚至1080P高清MKV,FLV,MP4,MOV,TS,RMVB等常见格式的视频,还可以在 Android 与 iOS 上跨平台支持 MMS, RTSP, RTMP, HLS(m3u8) 等常见的多种视频流媒体协议,包括点播与直播。
Vitamio 凭借其简洁易用的 API 接口赢得了全球众多开发者的青睐,到目前,全球已经有超过 10,000 种应用在使用 Vitamio,覆盖用户超过5亿。

最近更新较少,上一次更新(2017-04-01)已经是两年前了,但是这并不妨碍它是一个优秀的框架。

Vitamio 官网:http://www.vitamio.org
github: https://github.com/yixia/VitamioBundle
使用博客:农民伯伯:http://www.cnblogs.com/over140/category/409230.html(开发者之一,有大量的文档)

本文内容

本文对其使用进行了一些扩展和功能的丰富,将其整理后汇总。除集成外,扩展功能如下:

  1. 双击暂停,开始播放
  2. 全屏播放
  3. 倍速播放
  4. 左边上下滑动调节亮度
  5. 右边上下滑动调节声音
  6. 左右滑动快进快退
  7. 退出时保留播放进度
  8. 全屏播放时根据手机方向变化,屏幕自动旋转
  9. 进度条控件大小和位置的设置

注意事项

个人使用免费,商用需要授权
大家如果商用的话,别忘了去要授权……

集成

导入

github地址: https://github.com/yixia/VitamioBundle
首先从github上下载源文件,然后以 module的方式导入工程中,即可开始使用。

  1. 导入module,在Android Studio 中依次点击 File -->New --> Import Module…,然后在弹出框中选择文件的路径。
  2. 添加依赖,打开Project Structure选项卡,点击左上角的’+'按钮,然后按照提示添加即可。

目前为止,github上的代码已经很久没有维护了,上次维护应该还是 [2014-04-18] 发布的 4.2.2 版本。所以并不推荐用在公司项目中,一是很久没有维护了,二是需要授权。

  1. 打开app/src/main目录下的AndroidManifest.xml,注册io.vov.vitamio.activity.InitActivity

    注意:这个InitActivity存在于vitamio/src/对应的目录下,不需要用户编写.
<activityandroid: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" />

至此,vitamio导入完毕.

扩展功能

需求。左侧上下滑动调节亮度,右侧上下滑动调节声音,中间添加提示:
界面文件,因为点击之后控制器会布满屏幕,所及监听和显示组件都要添加到 MediaController中
在这里我们采用手势去处理

1. 双击暂停,开始播放

//播放与暂停
private void playOrPause(){if (videoView != null)if (videoView.isPlaying()) {videoView.pause();} else {videoView.start();}
}

2. 全屏播放

全屏播放,点击按钮后隐藏其他控件,重设尺寸即可
半屏播放同理

3. 倍速播放

Vitamio 支持倍速播放,可以根据项目需求进行设置

4. 左边上下滑动调节亮度

/*** 滑动改变亮度** @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;// 显示//mOperationBg.setImageResource(R.drawable.video_brightness_bg);mVolumeBrightnessLayout.setVisibility(View.VISIBLE);mOperationTv.setVisibility(VISIBLE);}

5. 右边上下滑动调节声音

/*** 滑动改变声音大小** @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);mOperationTv.setVisibility(VISIBLE);}int index = (int) (percent * mMaxVolume) + mVolume;if (index > mMaxVolume)index = mMaxVolume;else if (index < 0)index = 0;if (index >= 10) {mOperationBg.setImageResource(R.drawable.volmn_100);} else if (index >= 5 && index < 10) {mOperationBg.setImageResource(R.drawable.volmn_60);} else if (index > 0 && index < 5) {mOperationBg.setImageResource(R.drawable.volmn_30);} else {mOperationBg.setImageResource(R.drawable.volmn_no);}//DecimalFormat    df   = new DecimalFormat("######0.00");mOperationTv.setText((int) (((double) index / mMaxVolume)*100)+"%");// 变更声音mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0);}

6. 左右滑动快进快退

//滑动监听@Overridepublic 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/4onVolumeSlide((mOldY - y) / windowHeight);} else if (mOldX < windowWidth * 1.0 / 4.0) {// 左边滑动 屏幕 1/4onBrightnessSlide((mOldY - y) / windowHeight);}return super.onScroll(e1, e2, distanceX, distanceY);}

7. 退出时保留播放进度

当页面失去焦点时,保存当前播放进度数值,然后当恢复播放的时候,重新设置进度

8. 全屏播放时根据手机方向变化,屏幕自动旋转

首先确定开始收集转屏,然后页面设置中设置为响应屏幕方向的变化,即可自动切换

9. 进度条控件大小和位置的设置

@Override
protected View makeControllerView() {View v = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(getResources().getIdentifier("mymediacontroller", "layout", getContext().getPackageName()), this);v.setMinimumHeight(controllerWidth);... // 省略其他代码mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);return v;
}

完整代码

public class MyMediaController extends MediaController {private static final int HIDEFRAM = 0;//控制提示窗口的显示
private static final int SHOW_PROGRESS = 2;private GestureDetector mGestureDetector;//手势监听类
private ImageButton img_back;//返回键
private ImageView img_Battery;//电池电量显示
private TextView textViewTime;//时间提示
private TextView textViewBattery;//文字显示电池
private VideoView videoView;
private Activity activity;
private Context context;
private int controllerWidth = 0;//设置mediaController高度为了使横屏时top显示在屏幕顶端private View mVolumeBrightnessLayout;//提示窗口
private ImageView mOperationBg;//提示图片
private TextView mOperationTv;//提示文字
private AudioManager mAudioManager;private SeekBar progress;
private boolean mDragging;
private MediaPlayerControl player;
//最大声音
private int mMaxVolume;
// 当前声音
private int mVolume = -1;
//当前亮度
private float mBrightness = -1f;//返回监听
private View.OnClickListener backListener = new View.OnClickListener() {public void onClick(View v) {if(activity != null){activity.finish();}}
};private Handler myHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {long pos;switch (msg.what) {case HIDEFRAM://隐藏提示窗口mVolumeBrightnessLayout.setVisibility(View.GONE);mOperationTv.setVisibility(View.GONE);break;}}
};//videoview 用于对视频进行控制的等,activity为了退出
public MyMediaController(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() {View v = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(getResources().getIdentifier("mymediacontroller", "layout", getContext().getPackageName()), this);v.setMinimumHeight(controllerWidth);//TOP
//        img_back = (ImageButton) v.findViewById(getResources().getIdentifier("mediacontroller_top_back", "id", context.getPackageName()));
//        img_Battery = (ImageView) v.findViewById(getResources().getIdentifier("mediacontroller_imgBattery", "id", context.getPackageName()));
//        img_back.setOnClickListener(backListener);
//        textViewBattery = (TextView)v.findViewById(getResources().getIdentifier("mediacontroller_Battery", "id", context.getPackageName()));
//        textViewTime = (TextView)v.findViewById(getResources().getIdentifier("mediacontroller_time", "id", context.getPackageName()));img_back = (ImageButton) v.findViewById(R.id.mediacontroller_top_back);img_Battery = (ImageView) v.findViewById(R.id.mediacontroller_imgBattery);img_back.setOnClickListener(backListener);textViewBattery = (TextView)v.findViewById(R.id.mediacontroller_Battery);textViewTime = (TextView)v.findViewById(R.id.mediacontroller_time);progress = (SeekBar)v.findViewById(R.id.mediacontroller_seekbar);//midmVolumeBrightnessLayout = (RelativeLayout)v.findViewById(R.id.operation_volume_brightness);mOperationBg = (ImageView)v.findViewById(R.id.operation_bg);mOperationTv = (TextView) v.findViewById(R.id.operation_tv);mOperationTv.setVisibility(View.GONE);mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);return v;}@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, 1);
}private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {@Overridepublic boolean onSingleTapUp(MotionEvent e) {return false;}@Overridepublic boolean onSingleTapConfirmed(MotionEvent e) {//当收拾结束,并且是单击结束时,控制器隐藏/显示toggleMediaControlsVisiblity();return super.onSingleTapConfirmed(e);}@Overridepublic boolean onDown(MotionEvent e) {return true;}
//滑动监听@Overridepublic 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/4onVolumeSlide((mOldY - y) / windowHeight);} else if (mOldX < windowWidth * 1.0 / 4.0) {// 左边滑动 屏幕 1/4onBrightnessSlide((mOldY - y) / windowHeight);}return super.onScroll(e1, e2, distanceX, distanceY);}//双击暂停或开始@Overridepublic boolean onDoubleTap(MotionEvent e) {playOrPause();return true;}@Overridepublic 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);mOperationTv.setVisibility(VISIBLE);}int index = (int) (percent * mMaxVolume) + mVolume;if (index > mMaxVolume)index = mMaxVolume;else if (index < 0)index = 0;if (index >= 10) {mOperationBg.setImageResource(R.drawable.volmn_100);} else if (index >= 5 && index < 10) {mOperationBg.setImageResource(R.drawable.volmn_60);} else if (index > 0 && index < 5) {mOperationBg.setImageResource(R.drawable.volmn_30);} else {mOperationBg.setImageResource(R.drawable.volmn_no);}//DecimalFormat    df   = new DecimalFormat("######0.00");mOperationTv.setText((int) (((double) index / mMaxVolume)*100)+"%");// 变更声音mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0);}/*** 滑动改变亮度** @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;// 显示//mOperationBg.setImageResource(R.drawable.video_brightness_bg);mVolumeBrightnessLayout.setVisibility(View.VISIBLE);mOperationTv.setVisibility(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.drawable.light_100);} else if (lpa.screenBrightness * 100 >= 80 && lpa.screenBrightness * 100 < 90) {mOperationBg.setImageResource(R.drawable.light_90);} else if (lpa.screenBrightness * 100 >= 70 && lpa.screenBrightness * 100 < 80) {mOperationBg.setImageResource(R.drawable.light_80);} else if (lpa.screenBrightness * 100 >= 60 && lpa.screenBrightness * 100 < 70) {mOperationBg.setImageResource(R.drawable.light_70);} else if (lpa.screenBrightness * 100 >= 50 && lpa.screenBrightness * 100 < 60) {mOperationBg.setImageResource(R.drawable.light_60);} else if (lpa.screenBrightness * 100 >= 40 && lpa.screenBrightness * 100 < 50) {mOperationBg.setImageResource(R.drawable.light_50);} else if (lpa.screenBrightness * 100 >= 30 && lpa.screenBrightness * 100 < 40) {mOperationBg.setImageResource(R.drawable.light_40);} else if (lpa.screenBrightness * 100 >= 20 && lpa.screenBrightness * 100 < 20) {mOperationBg.setImageResource(R.drawable.light_30);} else if (lpa.screenBrightness * 100 >= 10 && lpa.screenBrightness * 100 < 20) {mOperationBg.setImageResource(R.drawable.light_20);}}public void setTime(String time){if (textViewTime != null)textViewTime.setText(time);
}
//显示电量,
public void setBattery(String stringBattery){if(textViewTime != null && img_Battery != null){textViewBattery.setText(stringBattery + "%");int battery = Integer.valueOf(stringBattery);if(battery < 15)img_Battery.setImageDrawable(getResources().getDrawable(R.drawable.battery_15));if(battery < 30 && battery >= 15)img_Battery.setImageDrawable(getResources().getDrawable(R.drawable.battery_15));if(battery < 45 && battery >= 30)img_Battery.setImageDrawable(getResources().getDrawable(R.drawable.battery_30));if(battery < 60 && battery >= 45)img_Battery.setImageDrawable(getResources().getDrawable(R.drawable.battery_45));if(battery < 75 && battery >= 60)img_Battery.setImageDrawable(getResources().getDrawable(R.drawable.battery_60));if(battery < 90 && battery >= 75)img_Battery.setImageDrawable(getResources().getDrawable(R.drawable.battery_75));if(battery > 90 )img_Battery.setImageDrawable(getResources().getDrawable(R.drawable.battery_90));}
}
//隐藏/显示
private void toggleMediaControlsVisiblity(){if (isShowing()) {hide();} else {show();}
}
//播放与暂停
private void playOrPause(){if (videoView != null)if (videoView.isPlaying()) {videoView.pause();} else {videoView.start();}
}
}

http://www.ppmy.cn/news/549774.html

相关文章

兔子NB播放器 (不用配置网上邻居,手机播放电脑视频)

不知道怎么运营,已经不维护了. 急需合伙人.请加我. 功能&#xff1a;使用手机或者电视盒子播放PC上的视频文件 &#xff08;我厌倦了配置电脑上组件家庭网络&#xff0c;配置IP什么的&#xff0c;故自己写了这个软件&#xff09; &#xff08;无需配置电脑环境&#xf…

ubuntu下用mplayer播放rmvb

转载时请注明出处和作者联系方式&#xff1a;http://blog.csdn.net/mimepp 作者联系方式&#xff1a;YU TAO <yut616 at sohu dot com> 环境&#xff1a;ubuntu 1、首先安装mplayer&#xff1a; sudo apt-get install mplayer 2、下载支持rmvb的codec&#xff1a;all-2…

安卓dts音频解码_安卓手机有什么音乐播放软件可以播放DTS音乐的呢?

展开全部 可以直接回答XBMC播放器可以播放DTS音乐。 XBMC播放器支持的视频格式&#xff1a; ◆62616964757a686964616fe58685e5aeb931333431333961MPEG-1◆MPEG-2◆MPEG-4◆ wmv◆ VC-1◆ RealVideo◆ QuickTime◆ SMC◆ VP3 ◆ Intel Indeo◆ NUV◆ VIVO ◆ ITU◆ CYUV◆ CVID…

[教程]手机通过网络(网上邻居)播放PC共享视频的方法

优点&#xff1a;与PC共享视频&#xff0c;无需转码&#xff0c;无需使用手机存储空间。 缺点&#xff1a;需要无线网络。可能需要缓冲时间。 需要的软件 1、MX_Player 2、ES文件浏览器&#xff0c;旧版或新版都可&#xff0c;本人喜欢用旧版的 设置网络共享&#xff1a; 对于…

顶峰Android手机视频转换器,凡人Android手机视频转换器

凡人Android手机视频转换器是一款专门针对谷歌Android安卓操作系统的手机开发的Android视频转换器工具&#xff0c;可以帮助您将各种流行的视频格式转换为Android手机所支持的视频格式和音频格式。本软件是Android手机用户看视频听音乐的必备工具。可转换RM、RMVB、AVI、MPG、V…

[教程]WMV6.1操作系统智能手机实现rmvb视频播放

写在前面&#xff1a; 很久没有写过技术文章了&#xff0c;今天忽然想用手机看看视频&#xff0c;于是说干就干&#xff0c;从电脑里找了相关的文件开始操作&#xff0c;又是上网查资料又是安装调试软件&#xff0c;还算顺利的用了完成了整个过程&#xff0c;达到了预期效果。心…

怎样用苹果手机播放html文件夹,无需转格式 如何用iPhone轻松爽看各种片

iPhone 5问世后,瞬间就成为了大家追随的最热门产品之一。无论是最具创新还是最热门,每一款产品推出后总是会存在遗憾的,iPhone 5同样不例外。在大家眼中它可能有这样或那样的问题,但是在我看来,自带视频播放器仅支持指定苹果标准视频,不支持大众化视频,这是让我最不满意…

android x5获取播放地址,Android应用内实现视频播放--腾讯浏览服务(TBS)

TBS视频播放 TBS视频播放器可以支持市面上几乎所有的视频格式&#xff0c;包括mp4, flv, avi, 3gp, webm, ts, ogv, m3u8, asf, wmv, rm, rmvb, mov, mkv等18种视频格式。同时支持硬件解码&#xff0c;使得内存占用&#xff0c;稳定性相较于系统播放器都更优。 接入TBS 1.导入j…