本文主要是介绍Android studio 使用vitamio,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在build.gradle的dependencies 中加入implementation project(':vitamio')
@Overridepublic void onResume() {if(!isPrepared || !isVisible)return;//检查lib包Vitamio.initialize(getActivity());if(!io.vov.vitamio.LibsChecker.checkVitamioLibs(getActivity())){return;}playVideo();super.onResume();}@Overridepublic void onPause() {releaseMediaPlayer();LogUtils.i(TAG,"media play pause");super.onPause();}@Overridepublic 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 + ":" + UrimVvv.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(){@Overridepublic void onCompletion(MediaPlayer mp) {Log.d("vitamioproblem","onCompletion");//mp.seekTo(0);mVvv.stopPlayback();}});//在视频预处理完成后被调用,此时视频的宽度、高度、宽高比信息已经获取到,此时可调用seekTo让视频从指定位置开始播放。mVvv.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){@Overridepublic void onPrepared(MediaPlayer mp) {Log.d("vitamioproblem","onPrepared");mProgressBarLayout.setVisibility(View.GONE);}});//监听 开始缓冲、缓冲结束、下载速度变化mVvv.setOnInfoListener(new MediaPlayer.OnInfoListener() {@Overridepublic 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(){@Overridepublic 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();}}}
这篇关于Android studio 使用vitamio的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!