Android--vitamio实现播放器

43 篇文章 0 订阅

 

Step1 到官网或者github下载vitamio资源

官网地址:https://www.vitamio.org/ (最新版本5.0.0,但是官网很难打开...)
github地址: https://github.com/yixia/VitamioBundleStudio

Step2 解压文件,将其中的vitamio导入到as中

其中的vitamio-sample是官方提供的demo,而我们要导入as的是vitamio.

打开AS,File -> New -> Import Moudle,选择刚才解压文件夹下的 vitamio 文件.
导入后的文件目录中会多出vitamin文件夹,如下图

导入后一般会出现这个问题:

解决方案单独写在step3中.

Step3 配置build.gradle

app目录下的build.gradle

在 dependencies 中添加 compile project(':vitamio') 如果你导入module中更改过名字的话 要改成修改后的名字 如图:

按照app目录下的build.gradle配置vitamio目录下的build.gradle(注意不是vitamio文件夹下app下的)

再次等待gradle编译完成,应该就没有问题了.

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

 
  1. <activity

  2. android:name="io.vov.vitamio.activity.InitActivity"

  3. android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"

  4. android:launchMode="singleTop"

  5. android:theme="@android:style/Theme.NoTitleBar"

  6. android:windowSoftInputMode="stateAlwaysHidden" />

注意:这个InitActivity存在于vitamio/src/对应的目录下,不需要用户编写.

至此,vitamio导入完毕.

DEMO

 
  1. import android.app.Activity;

  2. import android.content.pm.ActivityInfo;

  3. import android.content.res.Configuration;

  4. import android.graphics.Rect;

  5. import android.net.Uri;

  6. import android.os.Bundle;

  7. import android.os.PersistableBundle;

  8. import android.util.DisplayMetrics;

  9. import android.view.KeyEvent;

  10. import android.view.View;

  11. import android.view.Window;

  12. import android.view.WindowManager;

  13. import android.widget.Button;

  14. import android.widget.FrameLayout;

  15. import android.widget.LinearLayout;

  16. import android.widget.ProgressBar;

  17. import android.widget.RelativeLayout;

  18. import android.widget.TextView;

  19.  
  20. import com.sengke.vehicleviewer.R;

  21.  
  22. import io.vov.vitamio.MediaPlayer;

  23. import io.vov.vitamio.Vitamio;

  24. import io.vov.vitamio.utils.Log;

  25. import io.vov.vitamio.widget.MediaController;

  26. import io.vov.vitamio.widget.VideoView;

  27.  
  28. /**

  29. * Created by Administrator on 2019/3/26

  30. * <p>

  31. * desc:

  32. */

  33. public class videoPlayActivity extends Activity implements View.OnClickListener,MediaPlayer.OnInfoListener,

  34. MediaPlayer.OnBufferingUpdateListener{

  35. private VideoView videoView = null;

  36. private Button btn_play1,btn_play2,btn_play3;

  37. private Uri uri;

  38. private VideoView mVideoView;

  39. private ProgressBar pb;

  40. private TextView downloadRateView, loadRateView;

  41. private FrameLayout fl_controller;

  42. boolean isPortrait=true;

  43. private long mPosition = 0;

  44.  
  45. @Override

  46. public void onCreate(Bundle savedInstanceState) {

  47. super.onCreate(savedInstanceState);

  48. setContentView(R.layout.activity_videoplay);

  49.  
  50. Vitamio.initialize(videoPlayActivity.this);

  51. btn_play1 = findViewById(R.id.btn_play1);

  52. btn_play2 = findViewById(R.id.btn_play2);

  53. btn_play3 = findViewById(R.id.btn_play3);

  54. btn_play1.setOnClickListener(this);

  55. btn_play2.setOnClickListener(this);

  56. btn_play3.setOnClickListener(this);

  57.  
  58. initVideo("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov");

  59.  
  60. }

  61. private void initVideo(String path){

  62. mVideoView = (VideoView) findViewById(R.id.buffer);

  63. fl_controller= (FrameLayout) findViewById(R.id.fl_controller);

  64. pb = (ProgressBar) findViewById(R.id.probar);

  65.  
  66. downloadRateView = (TextView) findViewById(R.id.download_rate);

  67. loadRateView = (TextView) findViewById(R.id.load_rate);

  68. if (path == "") {

  69. // Tell the user to provide a media file URL/path.

  70. return;

  71. } else {

  72. /*

  73. * Alternatively,for streaming media you can use

  74. * mVideoView.setVideoURI(Uri.parse(URLstring));

  75. */

  76. uri = Uri.parse(path);

  77. mVideoView.setVideoURI(uri);

  78. MediaController mc = new MediaController(this, true, fl_controller);

  79. mc.setOnControllerClick(new MediaController.OnControllerClick() {

  80. @Override

  81. public void OnClick(int type) {

  82. //type 0 全屏。type1 分享

  83. if (type == 0) {

  84. if (isPortrait) {

  85. LinearLayout.LayoutParams fl_lp = new LinearLayout.LayoutParams(

  86. getHeightPixel(videoPlayActivity.this),

  87. getWidthPixel(videoPlayActivity.this) - getStatusBarHeight(videoPlayActivity.this)

  88. );

  89.  
  90. fl_controller.setLayoutParams(fl_lp);

  91. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

  92.  
  93. mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_SCALE, 0);

  94. isPortrait = false;

  95. } else {

  96. LinearLayout.LayoutParams fl_lp = new LinearLayout.LayoutParams(

  97. LinearLayout.LayoutParams.MATCH_PARENT,

  98. DensityUtil.dip2px(260, videoPlayActivity.this)

  99. );

  100.  
  101. fl_controller.setLayoutParams(fl_lp);

  102.  
  103. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

  104. isPortrait = true;

  105. }

  106.  
  107. }

  108.  
  109. }

  110. });

  111. mVideoView.setMediaController(mc);

  112. mc.setVisibility(View.GONE);

  113. // mVideoView.setMediaController(new MediaController(this));

  114. mVideoView.requestFocus();

  115. mVideoView.setOnInfoListener(this);

  116. mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);//设置播放画质 高画质

  117. mVideoView.setOnBufferingUpdateListener(this);

  118. mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

  119. @Override

  120. public void onPrepared(MediaPlayer mediaPlayer) {

  121. // optional need Vitamio 4.0

  122. mediaPlayer.setPlaybackSpeed(1.0f);

  123. // mVideoView.start();

  124. }

  125. });

  126. }

  127.  
  128. }

  129.  
  130. @Override

  131. public void onClick(View v) {

  132. switch (v.getId()) {

  133. case R.id.btn_play1:

  134. initVideo("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov");

  135. break;

  136. case R.id.btn_play2:

  137. initVideo("rtsp://218.204.223.237:554/live/1/6D1E43167B3A7BDA/oby9efo80duh9bjf.sdp");

  138. break;

  139. case R.id.btn_play3:

  140. initVideo("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov");

  141. break;

  142. }

  143. }

  144. @Override

  145. public boolean onInfo(MediaPlayer mp, int what, int extra) {

  146.  
  147. switch (what) {

  148. case MediaPlayer.MEDIA_INFO_BUFFERING_START:

  149. if (mVideoView.isPlaying()) {

  150. mVideoView.pause();

  151. pb.setVisibility(View.VISIBLE);

  152. downloadRateView.setText("");

  153. loadRateView.setText("");

  154. downloadRateView.setVisibility(View.VISIBLE);

  155. loadRateView.setVisibility(View.VISIBLE);

  156.  
  157. }

  158. break;

  159. case MediaPlayer.MEDIA_INFO_BUFFERING_END:

  160. mVideoView.start();

  161. pb.setVisibility(View.GONE);

  162. downloadRateView.setVisibility(View.GONE);

  163. loadRateView.setVisibility(View.GONE);

  164. break;

  165. case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:

  166. downloadRateView.setText("" + extra + "kb/s" + " ");

  167. break;

  168. }

  169. return true;

  170. }

  171.  
  172. @Override

  173. public boolean onKeyDown(int keyCode, KeyEvent event) {

  174. if(keyCode==KeyEvent.KEYCODE_BACK){

  175. if(!isPortrait){

  176. LinearLayout.LayoutParams fl_lp=new LinearLayout.LayoutParams(

  177. LinearLayout.LayoutParams.MATCH_PARENT,

  178. DensityUtil.dip2px(200,videoPlayActivity.this)

  179. );

  180. fl_controller.setLayoutParams(fl_lp);

  181.  
  182. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

  183. isPortrait=true;

  184. return true;

  185. }

  186. }

  187. return super.onKeyDown(keyCode, event);

  188. }

  189.  
  190. @Override

  191. public void onBufferingUpdate(MediaPlayer mp, int percent) {

  192. loadRateView.setText(percent + "%");

  193. }

  194.  
  195. @Override

  196. protected void onPause() {

  197. mPosition = mVideoView.getCurrentPosition();

  198. mVideoView.stopPlayback();

  199. super.onPause();

  200. }

  201.  
  202. @Override

  203. protected void onResume() {

  204. if (mPosition > 0) {

  205. mVideoView.seekTo(mPosition);

  206. mPosition = 0;

  207. }

  208. super.onResume();

  209. // mVideoView.start();

  210.  
  211.  
  212. }

  213.  
  214.  
  215. public int getHeightPixel(Activity activity)

  216. {

  217. DisplayMetrics localDisplayMetrics = new DisplayMetrics();

  218. activity.getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);

  219. return localDisplayMetrics.heightPixels;

  220. }

  221. public int getWidthPixel(Activity activity)

  222. {

  223. DisplayMetrics localDisplayMetrics = new DisplayMetrics();

  224. activity.getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);

  225. return localDisplayMetrics.widthPixels;

  226. }

  227. public int getStatusBarHeight(Activity activity){

  228. Rect frame = new Rect();

  229. activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

  230.  
  231. int statusBarHeight = frame.top;

  232. return statusBarHeight;

  233. }

  234.  
  235. }

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout

  3. xmlns:android="http://schemas.android.com/apk/res/android"

  4. android:layout_width="match_parent"

  5. android:layout_height="match_parent"

  6. android:background="#fff"

  7. android:orientation="vertical">

  8.  
  9. <FrameLayout

  10. android:id="@+id/fl_controller"

  11. android:layout_width="match_parent"

  12. android:layout_height="260dp">

  13. <io.vov.vitamio.widget.CenterLayout

  14. android:layout_width="match_parent"

  15. android:layout_height="match_parent"

  16. android:orientation="vertical"

  17. >

  18. <io.vov.vitamio.widget.VideoView

  19. android:id="@+id/buffer"

  20. android:layout_width="match_parent"

  21. android:layout_height="match_parent"

  22. android:layout_centerHorizontal="true"

  23. android:layout_centerVertical="true"

  24. />

  25. </io.vov.vitamio.widget.CenterLayout>

  26. <LinearLayout

  27. android:layout_width="wrap_content"

  28. android:layout_height="wrap_content"

  29. android:layout_gravity="center"

  30. android:orientation="horizontal">

  31. <ProgressBar

  32. android:id="@+id/probar"

  33. style="?android:attr/progressBarStyleLarge"

  34. android:layout_width="wrap_content"

  35. android:layout_height="wrap_content" />

  36. <TextView

  37. android:id="@+id/download_rate"

  38. android:layout_width="wrap_content"

  39. android:layout_height="wrap_content"

  40. android:layout_gravity="center"

  41. android:textColor="#52C1BD"

  42. android:text="" />

  43. <TextView

  44. android:id="@+id/load_rate"

  45. android:layout_width="wrap_content"

  46. android:layout_height="wrap_content"

  47. android:layout_gravity="center"

  48. android:textColor="#52C1BD"

  49. android:text="" />

  50. </LinearLayout>

  51. </FrameLayout>

  52.  
  53. <LinearLayout

  54. android:layout_width="match_parent"

  55. android:layout_height="wrap_content"

  56. android:orientation="horizontal"

  57. android:layout_marginTop="10dp"

  58. >

  59. <Button

  60. android:id="@+id/btn_play1"

  61. android:layout_width="wrap_content"

  62. android:layout_height="wrap_content"

  63. android:layout_marginTop="10dp"

  64. android:background="@drawable/button"

  65. android:paddingBottom="8dp"

  66. android:paddingLeft="15dp"

  67. android:paddingRight="15dp"

  68. android:paddingTop="8dp"

  69. android:text="摄像头1"

  70. android:textColor="#fff"

  71. android:textSize="18sp"

  72. android:layout_weight="1"/>

  73. <Button

  74. android:id="@+id/btn_play2"

  75. android:layout_width="wrap_content"

  76. android:layout_height="wrap_content"

  77. android:layout_marginTop="10dp"

  78. android:background="@drawable/button"

  79. android:paddingBottom="8dp"

  80. android:paddingLeft="15dp"

  81. android:paddingRight="15dp"

  82. android:paddingTop="8dp"

  83. android:text="摄像头2"

  84. android:textColor="#fff"

  85. android:textSize="18sp"

  86. android:layout_marginLeft="10dp"

  87. android:layout_weight="1"/>

  88. <Button

  89. android:id="@+id/btn_play3"

  90. android:layout_width="wrap_content"

  91. android:layout_height="wrap_content"

  92. android:layout_marginTop="10dp"

  93. android:layout_marginLeft="10dp"

  94. android:background="@drawable/button"

  95. android:paddingBottom="8dp"

  96. android:paddingLeft="15dp"

  97. android:paddingRight="15dp"

  98. android:paddingTop="8dp"

  99. android:text="摄像头3"

  100. android:textColor="#fff"

  101. android:textSize="18sp"

  102. android:layout_weight="1"/>

  103. </LinearLayout>

  104. </LinearLayout>

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">

  3. <!--点击背景-->

  4. <item android:state_pressed="true">

  5. <shape>

  6. <!--色值-->

  7. <solid android:color="#e79429" />

  8. <!--圆角-->

  9. <corners android:radius="10dp" />

  10. </shape>

  11. </item>

  12. <!--默认背景-->

  13. <item>

  14. <shape>

  15. <solid android:color="#f6aa3e" />

  16. <corners android:radius="10dp" />

  17. </shape>

  18. </item>

  19. </selector>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值