Android studio使用vitamio

释放双眼,带上耳机,听听看~!

在build.gradle的dependencies 中加入implementation project(‘:vitamio’)


@Override
public void onResume() {
if(!isPrepared || !isVisible)
return;
//检查lib包
Vitamio.initialize(getActivity());
if(!io.vov.vitamio.LibsChecker.checkVitamioLibs(getActivity())){
return;
}
playVideo();
super.onResume();
}
@Override
public void onPause() {
releaseMediaPlayer();
LogUtils.i(TAG,"media play  pause");
super.onPause();
}
@Override
public void onDestroy() {
releaseMediaPlayer();
LogUtils.i(TAG,"media play  onDestroy");
super.onDestroy();
}
public void playVideo(){
try {
s=path.split("/");
file_name =s[s.length-1];
getvideofromsdcard(getActivity(),file_name,flag_ssi);
//判断本地是否有缓存的视频,如果有就读取的,如果没有就从网上边播边下载
if(getvideofromsdcard(getActivity(),file_name,flag_filename)){
mVvv.setVideoURI(Uri.parse("/sdcard/DCIM/"+ file_name));
Log.d("videosuccess","videosuccess");
isexist=false;
mVvv.setVolume(0.1f,0.1f);//设置声音
mVvv.start();
}else {
try {
mVvv.setVideoURI(Uri.parse(path));
mHandler.postDelayed(donwnloadrun,0);
//103对应的操作 将视频边播边存入到本地                                                         //mVvv.setVideoURI(Uri.parse("cache:/sdcard/DCIM/" + file_name + ":" + Uri
mVvv.setVolume(0.1f,0.1f);//设置声音
mVvv.start();
} catch (Exception e) {
e.printStackTrace();
String s = e.getMessage();
Log.d("DCIM", s);
}
}
//设置进度条和操作按钮不可见
MediaController mc=  new MediaController(getActivity());
mc.setVisibility(View.GONE);
mVvv.setMediaController(mc);
mVvv.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH); //清晰度设置高清
​​​​​​   ​//视频播放完成后调用。
mVvv.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
Log.d("vitamioproblem","onCompletion");
//mp.seekTo(0);
mVvv.stopPlayback();
}
});
//在视频预处理完成后被调用,此时视频的宽度、高度、宽高比信息已经获取到,此时可调用seekTo让视频从指定位置开始播放。
mVvv.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp) {
Log.d("vitamioproblem","onPrepared");
mProgressBarLayout.setVisibility(View.GONE);
}
});
//监听 开始缓冲、缓冲结束、下载速度变化
mVvv.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
if (mVvv.isPlaying() && mVvv != null) {
mVvv.pause();
mProgressBarLayout.setVisibility(View.VISIBLE);
//isBufferingStart = true;
}
time_start = System.currentTimeMillis();
Log.d("vitamioproblem", "MediaPlayer.MEDIA_INFO_BUFFERING_START" + " " + time_start);
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
if(mVvv != null )
mVvv.start();
mProgressBarLayout.setVisibility(View.GONE);
time_end = System.currentTimeMillis();
Log.d("vitamioproblem", " " + "时长为"+mVvv.getDuration());
if ((time_end - time_start) > 10000) {
Toast.makeText(getActivity(), "播放太慢,建议下载到本地", Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "正在下载中,请耐心等待", Toast.LENGTH_LONG).show();
mHandler.postDelayed(mRunnable,2000);
}
Log.d("vitamioproblem", "MediaPlayer.MEDIA_INFO_BUFFERING_END" + " " + time_end);
break;
case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:
break;
}
return true;
}
});
//错误监听
mVvv.setOnErrorListener(new MediaPlayer.OnErrorListener(){
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
switch (what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
Toast.makeText(getActivity(),"未知错误",Toast.LENGTH_LONG).show();
break;
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
Toast.makeText(getActivity(),"视频播放比较慢或视频本身有问题",Toast.LENGTH_LONG).show();
break;
case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
Toast.makeText(getActivity(),"视频不能解码",Toast.LENGTH_LONG).show();
break;
}
return true;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 释放MediaPlayer
*/
private void releaseMediaPlayer() {
LogUtils.i(TAG,"media release");
if (mVvv != null) {
if(isPlaying)mVvv.stopPlayback();
}
}
}

为TA充电
共{{data.count}}人
人已赞赏
Android文章

android开发SpannableString 设置文字特殊显示

2021-2-1 13:22:22

Android文章

android开发接入支付宝支付加沙箱环境测试

2021-2-1 14:15:24

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索