Vitamio是一款开源的视频播放库,我们可以在Android或者ios中很轻松的调用。本文主要介绍了如何在AndroidStudio中导入Vitamio库,以实现播放网络视频。
一,导入Vitamio工程
1,去GitHub下载这个项目: https://github.com/yixia/VitamioBundle
2,在AndroidStudio中将其导入
如图,我们选择Import Module(在AndroidStudio中一个Project就相当于Eclipse中的WorkSpace,而Module则相当于Eclipse中的Project)。
如图在这里将项目导入我们的项目中。
3,将导入的Vitamio项目作为一个库引用
如图,我们选择Project Structure,然后在弹出的页面的左边的Modules一栏,选择我们的主要的Android项目,然后点击右边Dependencies一栏,点击底部的“加号”如下图,并选择Module dependency。
在弹出的界面中,我们选择刚刚导入的Vitamio工程,然后点击ok,即可导入成功。
二,使用Vitamio创建自己的播放器Activity
public class VideoViewBuffer extends Activity implements MediaPlayer.OnInfoListener, MediaPlayer.OnBufferingUpdateListener {
/**
* TODO: Set the path variable to a streaming video URL or a local media file
* path.
*/
private String path = "测试视频地址";
private Uri uri;
private io.vov.vitamio.widget.VideoView mVideoView;
private ProgressBar pb;
private TextView downloadRateView, loadRateView;
private String subTitleUrlPath = "字幕地址";
private String subTitlePath = Environment.getExternalStorageDirectory() + "/Test/";
private String subTitleName = "test.srt";
private TextView tvSrt;
private List<HashMap<String, String>> SrtList;
private DownLoadSubTitle task = new DownLoadSubTitle();
private SrtTask srtTask = new SrtTask();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (!LibsChecker.checkVitamioLibs(this))
return;
setContentView(R.layout.coursera_videobuffer);
mVideoView = (VideoView) findViewById(R.id.buffera);
pb = (ProgressBar) findViewById(R.id.probar);
downloadRateView = (TextView) findViewById(R.id.download_rate);
loadRateView = (TextView) findViewById(R.id.load_rate);
tvSrt = (TextView) findViewById(R.id.coursera_videobuffer_srt);//项目中显示字幕的控件
if (path == "") {
// Tell the user to provide a media file URL/path.
Toast.makeText(
VideoViewBuffer.this,
"Please edit VideoBuffer Activity, and set path"
+ " variable to your media file URL/path", Toast.LENGTH_LONG).show();
return;
} else {
/*
* Alternatively,for streaming media you can use
* mVideoView.setVideoURI(Uri.parse(URLstring));
*/
uri = Uri.parse(path);
mVideoView.setVideoURI(uri);
mVideoView.setMediaController(new MediaController(this, mVideoView));
mVideoView.requestFocus();
mVideoView.setOnInfoListener(this);
mVideoView.setOnBufferingUpdateListener(this);
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// optional need Vitamio 4.0
task.execute();
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
}
// Toast.makeText(getApplicationContext(), "测试", Toast.LENGTH_SHORT).show();
}
@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 onBufferingUpdate(MediaPlayer mp, int percent) {
loadRateView.setText(percent + "%");
}
public void onConfigurationChanged(Configuration newConfig) {
// System.out.println("方向变化了");
mVideoView.setVideoLayout(mVideoView.mVideoLayout, mVideoView.mAspectRatio);
super.onConfigurationChanged(newConfig);
// System.out.println("videoBuffer中的方向" + newConfig.orientation);
}
public void getSubtitle(String path, String filename) {
HttpDownLoad hd = new HttpDownLoad();
hd.getConnection(path);
if ((hd.getInputStream() != null) && (hd.getLength() != 0)) {
hd.writeIntoSD(filename);
}
}
private class DownLoadSubTitle extends AsyncTask<Integer, Integer, Integer> {
@Override
protected Integer doInBackground(Integer... params) {
System.out.println("执行了下载字幕的方法");
int state = 0;
try {
getSubtitle(subTitleUrlPath, subTitleName);
} catch (Exception e) {
state = 1;
e.printStackTrace();
}
return state;
}
@Override
protected void onPostExecute(Integer result) {
if (result == 0) {
showSrt(subTitlePath + subTitleName);
System.out.println("执行了下载字幕的方法,成功");
} else {
Toast.makeText(getApplicationContext(), "字幕获取失败", Toast.LENGTH_SHORT).show();
}
}
}
private void showSrt(String filePath) {
tvSrt.setVisibility(View.VISIBLE);
SrtUtils srt = new SrtUtils();
srt.loadLyricFile(filePath);
SrtList = srt.getSrtList();
srtTask.execute();
}
private class SrtTask extends AsyncTask<Integer, Integer, Integer> {
private int position;
private String currentTex = "";
@Override
protected Integer doInBackground(Integer... params) {
while (mVideoView.getCurrentPosition() < mVideoView.getDuration()) {
if (isCancelled()) {
return null;
}
for (int i = 0; i < SrtList.size(); i++) {
HashMap map = SrtList.get(i);
long start = Long.parseLong(map.get("start").toString());
long end = Long.parseLong(map.get("end").toString());
long current = mVideoView.getCurrentPosition();
if ((current > start && current < end)) {
position = i;
publishProgress(i);
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
if (isCancelled()) {
return;
}
String tex = SrtList.get(position).get("text").toString();
if (currentTex.equals("")) {
tvSrt.setText(tex);
} else {
if (!currentTex.equals(tex)) {
tvSrt.setText(tex);
}
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("重写了onDestory方法");
task.cancel(true);
srtTask.cancel(true);
}
@Override
public void onBackPressed() {
System.out.println("重写了back方法");
task.cancel(true);
srtTask.cancel(true);
this.finish();
return;
}
}