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
-
<activity
-
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" />
注意:这个InitActivity存在于vitamio/src/对应的目录下,不需要用户编写.
至此,vitamio导入完毕.
DEMO
-
import android.app.Activity;
-
import android.content.pm.ActivityInfo;
-
import android.content.res.Configuration;
-
import android.graphics.Rect;
-
import android.net.Uri;
-
import android.os.Bundle;
-
import android.os.PersistableBundle;
-
import android.util.DisplayMetrics;
-
import android.view.KeyEvent;
-
import android.view.View;
-
import android.view.Window;
-
import android.view.WindowManager;
-
import android.widget.Button;
-
import android.widget.FrameLayout;
-
import android.widget.LinearLayout;
-
import android.widget.ProgressBar;
-
import android.widget.RelativeLayout;
-
import android.widget.TextView;
-
import com.sengke.vehicleviewer.R;
-
import io.vov.vitamio.MediaPlayer;
-
import io.vov.vitamio.Vitamio;
-
import io.vov.vitamio.utils.Log;
-
import io.vov.vitamio.widget.MediaController;
-
import io.vov.vitamio.widget.VideoView;
-
/**
-
* Created by Administrator on 2019/3/26
-
* <p>
-
* desc:
-
*/
-
public class videoPlayActivity extends Activity implements View.OnClickListener,MediaPlayer.OnInfoListener,
-
MediaPlayer.OnBufferingUpdateListener{
-
private VideoView videoView = null;
-
private Button btn_play1,btn_play2,btn_play3;
-
private Uri uri;
-
private VideoView mVideoView;
-
private ProgressBar pb;
-
private TextView downloadRateView, loadRateView;
-
private FrameLayout fl_controller;
-
boolean isPortrait=true;
-
private long mPosition = 0;
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_videoplay);
-
Vitamio.initialize(videoPlayActivity.this);
-
btn_play1 = findViewById(R.id.btn_play1);
-
btn_play2 = findViewById(R.id.btn_play2);
-
btn_play3 = findViewById(R.id.btn_play3);
-
btn_play1.setOnClickListener(this);
-
btn_play2.setOnClickListener(this);
-
btn_play3.setOnClickListener(this);
-
initVideo("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov");
-
}
-
private void initVideo(String path){
-
mVideoView = (VideoView) findViewById(R.id.buffer);
-
fl_controller= (FrameLayout) findViewById(R.id.fl_controller);
-
pb = (ProgressBar) findViewById(R.id.probar);
-
downloadRateView = (TextView) findViewById(R.id.download_rate);
-
loadRateView = (TextView) findViewById(R.id.load_rate);
-
if (path == "") {
-
// Tell the user to provide a media file URL/path.
-
return;
-
} else {
-
/*
-
* Alternatively,for streaming media you can use
-
* mVideoView.setVideoURI(Uri.parse(URLstring));
-
*/
-
uri = Uri.parse(path);
-
mVideoView.setVideoURI(uri);
-
MediaController mc = new MediaController(this, true, fl_controller);
-
mc.setOnControllerClick(new MediaController.OnControllerClick() {
-
@Override
-
public void OnClick(int type) {
-
//type 0 全屏。type1 分享
-
if (type == 0) {
-
if (isPortrait) {
-
LinearLayout.LayoutParams fl_lp = new LinearLayout.LayoutParams(
-
getHeightPixel(videoPlayActivity.this),
-
getWidthPixel(videoPlayActivity.this) - getStatusBarHeight(videoPlayActivity.this)
-
);
-
fl_controller.setLayoutParams(fl_lp);
-
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
-
mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_SCALE, 0);
-
isPortrait = false;
-
} else {
-
LinearLayout.LayoutParams fl_lp = new LinearLayout.LayoutParams(
-
LinearLayout.LayoutParams.MATCH_PARENT,
-
DensityUtil.dip2px(260, videoPlayActivity.this)
-
);
-
fl_controller.setLayoutParams(fl_lp);
-
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
-
isPortrait = true;
-
}
-
}
-
}
-
});
-
mVideoView.setMediaController(mc);
-
mc.setVisibility(View.GONE);
-
// mVideoView.setMediaController(new MediaController(this));
-
mVideoView.requestFocus();
-
mVideoView.setOnInfoListener(this);
-
mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);//设置播放画质 高画质
-
mVideoView.setOnBufferingUpdateListener(this);
-
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
-
@Override
-
public void onPrepared(MediaPlayer mediaPlayer) {
-
// optional need Vitamio 4.0
-
mediaPlayer.setPlaybackSpeed(1.0f);
-
// mVideoView.start();
-
}
-
});
-
}
-
}
-
@Override
-
public void onClick(View v) {
-
switch (v.getId()) {
-
case R.id.btn_play1:
-
initVideo("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov");
-
break;
-
case R.id.btn_play2:
-
initVideo("rtsp://218.204.223.237:554/live/1/6D1E43167B3A7BDA/oby9efo80duh9bjf.sdp");
-
break;
-
case R.id.btn_play3:
-
initVideo("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov");
-
break;
-
}
-
}
-
@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 boolean onKeyDown(int keyCode, KeyEvent event) {
-
if(keyCode==KeyEvent.KEYCODE_BACK){
-
if(!isPortrait){
-
LinearLayout.LayoutParams fl_lp=new LinearLayout.LayoutParams(
-
LinearLayout.LayoutParams.MATCH_PARENT,
-
DensityUtil.dip2px(200,videoPlayActivity.this)
-
);
-
fl_controller.setLayoutParams(fl_lp);
-
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
-
isPortrait=true;
-
return true;
-
}
-
}
-
return super.onKeyDown(keyCode, event);
-
}
-
@Override
-
public void onBufferingUpdate(MediaPlayer mp, int percent) {
-
loadRateView.setText(percent + "%");
-
}
-
@Override
-
protected void onPause() {
-
mPosition = mVideoView.getCurrentPosition();
-
mVideoView.stopPlayback();
-
super.onPause();
-
}
-
@Override
-
protected void onResume() {
-
if (mPosition > 0) {
-
mVideoView.seekTo(mPosition);
-
mPosition = 0;
-
}
-
super.onResume();
-
// mVideoView.start();
-
}
-
public int getHeightPixel(Activity activity)
-
{
-
DisplayMetrics localDisplayMetrics = new DisplayMetrics();
-
activity.getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
-
return localDisplayMetrics.heightPixels;
-
}
-
public int getWidthPixel(Activity activity)
-
{
-
DisplayMetrics localDisplayMetrics = new DisplayMetrics();
-
activity.getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
-
return localDisplayMetrics.widthPixels;
-
}
-
public int getStatusBarHeight(Activity activity){
-
Rect frame = new Rect();
-
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
-
int statusBarHeight = frame.top;
-
return statusBarHeight;
-
}
-
}
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout
-
xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:background="#fff"
-
android:orientation="vertical">
-
<FrameLayout
-
android:id="@+id/fl_controller"
-
android:layout_width="match_parent"
-
android:layout_height="260dp">
-
<io.vov.vitamio.widget.CenterLayout
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical"
-
>
-
<io.vov.vitamio.widget.VideoView
-
android:id="@+id/buffer"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:layout_centerHorizontal="true"
-
android:layout_centerVertical="true"
-
/>
-
</io.vov.vitamio.widget.CenterLayout>
-
<LinearLayout
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_gravity="center"
-
android:orientation="horizontal">
-
<ProgressBar
-
android:id="@+id/probar"
-
style="?android:attr/progressBarStyleLarge"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content" />
-
<TextView
-
android:id="@+id/download_rate"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_gravity="center"
-
android:textColor="#52C1BD"
-
android:text="" />
-
<TextView
-
android:id="@+id/load_rate"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_gravity="center"
-
android:textColor="#52C1BD"
-
android:text="" />
-
</LinearLayout>
-
</FrameLayout>
-
<LinearLayout
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:orientation="horizontal"
-
android:layout_marginTop="10dp"
-
>
-
<Button
-
android:id="@+id/btn_play1"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_marginTop="10dp"
-
android:background="@drawable/button"
-
android:paddingBottom="8dp"
-
android:paddingLeft="15dp"
-
android:paddingRight="15dp"
-
android:paddingTop="8dp"
-
android:text="摄像头1"
-
android:textColor="#fff"
-
android:textSize="18sp"
-
android:layout_weight="1"/>
-
<Button
-
android:id="@+id/btn_play2"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_marginTop="10dp"
-
android:background="@drawable/button"
-
android:paddingBottom="8dp"
-
android:paddingLeft="15dp"
-
android:paddingRight="15dp"
-
android:paddingTop="8dp"
-
android:text="摄像头2"
-
android:textColor="#fff"
-
android:textSize="18sp"
-
android:layout_marginLeft="10dp"
-
android:layout_weight="1"/>
-
<Button
-
android:id="@+id/btn_play3"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_marginTop="10dp"
-
android:layout_marginLeft="10dp"
-
android:background="@drawable/button"
-
android:paddingBottom="8dp"
-
android:paddingLeft="15dp"
-
android:paddingRight="15dp"
-
android:paddingTop="8dp"
-
android:text="摄像头3"
-
android:textColor="#fff"
-
android:textSize="18sp"
-
android:layout_weight="1"/>
-
</LinearLayout>
-
</LinearLayout>
-
<?xml version="1.0" encoding="utf-8"?>
-
<selector xmlns:android="http://schemas.android.com/apk/res/android">
-
<!--点击背景-->
-
<item android:state_pressed="true">
-
<shape>
-
<!--色值-->
-
<solid android:color="#e79429" />
-
<!--圆角-->
-
<corners android:radius="10dp" />
-
</shape>
-
</item>
-
<!--默认背景-->
-
<item>
-
<shape>
-
<solid android:color="#f6aa3e" />
-
<corners android:radius="10dp" />
-
</shape>
-
</item>
-
</selector>