各种视频播放功能的总结
1.直接上效果图
2.使用VideoView加载视频
private void playVideo() { // //2、使用VideoView来播放。在布局文件中使用VideoView结合MediaController来实现对其控制。 // //6.0要手动添加权限 // myVideoView.setMediaController(new MediaController(this));//添加进度条 // myVideoView.setVideoURI(uri); // myVideoView.start(); // myVideoView.requestFocus(); try { MediaMetadataRetriever media = new MediaMetadataRetriever(); if (Build.VERSION.SDK_INT >= 14) { media.setDataSource(videopath, new HashMap<String, String>()); } else { media.setDataSource(videopath); } //获取第一帧 bitmap = media.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC); imageView.setImageBitmap(bitmap); media.release(); // Glide.with(getApplicationContext()) // .load(videopath) // .into(imageView); //视频循环播放 //myVideoView.setVideoURI(uri); myVideoView.setVideoPath(videopath); myVideoView.start();//播放 // myVideoView.pause();//暂停、播放 MediaController mediaController = new MediaController(this); // mediaController.onFinishInflate();//一进来时显示进度条 myVideoView.setMediaController(mediaController);//添加进度条 //注册在媒体文件加载完毕,可以播放时调用的回调函数。可循环播放视频 myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { // mp.start();//离开界面还回后可以重新播放 mp.setLooping(true);//设置是否对播放的东东进行循环播放。 mp.setOnInfoListener(new MediaPlayer.OnInfoListener() { @Override public boolean onInfo(MediaPlayer mediaPlayer, int what, int i1) { //开始播放时,就把显示第一帧的ImageView gone 掉 if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) { // video started; hide the placeholder. imageView.setVisibility(View.GONE); //videoView.seekTo(0); return true; } Log.e("aaa", "00000000000000"); return false; } }); } }); // // //注册在媒体文件播放完毕时调用的回调函数。可设置循环播放视频 // myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { // @Override // public void onCompletion(MediaPlayer mp) { // //myVideoView.setVideoURI(uri); // myVideoView.setVideoPath(videopath); // myVideoView.start(); // //mp.start();//这里设置该值,离开界面还回后不会重新播放 // } // }); } catch (Exception e) { Toast.makeText(getApplicationContext(), "可能视频地址有问题", Toast.LENGTH_SHORT).show(); } }
3.MediaPlayer+SurfaceView播放视频
public void play() { String path = "http://192.168.88.149:999/images/IC/IC0000054/IC0000054_180420163814230_iOS.mp4"; try { mMediaPlayer.reset(); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); //设置需要播放的视频 mMediaPlayer.setDataSource(this, Uri.parse(path)); mMediaPlayer.setLooping(true);//循环播放 //把视频画面输出到SurfaceView mMediaPlayer.setDisplay(mSurfaceView.getHolder()); mMediaPlayer.prepare();//将资源同步缓存到内存中,一般加载本地较小的资源可以用这个 // mMediaPlayer.prepareAsync();//较大的资源或者网络资源建议使用prepareAsync方法,异步加载 mMediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } }
4.MediaPlayer+TextureView播放视频
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_texture_view); setTitle("MediaPlayer+TextureView"); TextureView textureView = findViewById(R.id.textureView); textureView.setSurfaceTextureListener(surfaceTezxtureListener);//设置监听函数 重写4个方法 videoImage = findViewById(R.id.videoImage); seekBar = findViewById(R.id.seekBar); seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);//seekbar改变监听 } private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {//进度改变 } @Override public void onStartTrackingTouch(SeekBar seekBar) {//开始拖动seekbar } @Override public void onStopTrackingTouch(SeekBar seekBar) {//停止拖动seekbar if (mMediaPlayer != null && mMediaPlayer.isPlaying())//播放中 mMediaPlayer.seekTo(seekBar.getProgress()); } }; private TextureView.SurfaceTextureListener surfaceTezxtureListener = new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaces, int width, int height) { surface = new Surface(surfaces); new PlayerVideoThread().start();//开启一个线程去播放视频 } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {//尺寸改变 } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaces) {//销毁 surface = null; mMediaPlayer.stop(); mMediaPlayer.release(); mMediaPlayer = null; return true; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) {//更新 } }; private class PlayerVideoThread extends Thread { @Override public void run() { try { mMediaPlayer = new MediaPlayer(); Uri uri = Uri.parse("http://192.168.88.149:999/images/IC/IC0000054/IC0000054_180420163814230_iOS.mp4"); mMediaPlayer.setDataSource(TextureViewActivity.this, uri);//设置播放资源(可以是应用的资源文件/url/sdcard路径) mMediaPlayer.setSurface(surface);//设置渲染画板 mMediaPlayer.setLooping(true);//循环播放 mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置播放类型 // mMediaPlayer.setOnCompletionListener(onCompletionListener);//播放完成监听,有循环播放不会有此监听 mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {//预加载监听 @Override public void onPrepared(MediaPlayer mp) { videoImage.setVisibility(View.GONE); mMediaPlayer.start();//开始播放 seekBar.setMax(mMediaPlayer.getDuration());//设置总进度 handler.post(mTicker);//更新进度 } }); mMediaPlayer.prepare(); } catch (Exception e) { e.printStackTrace(); } } } private MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) {//播放完成 videoImage.setVisibility(View.VISIBLE); seekBar.setProgress(0); handler.removeCallbacks(mTicker);//删除执行的Runnable 终止计时器 } };
5.使用第三方播放器(饺子播放器)
private String path = "http://192.168.88.149:999/images/IC/IC0000054/IC0000054_180420163814230_iOS.mp4"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jiao_zi_video_player); MyJZVideoPlayerStandard myJZVideoPlayerStandard = findViewById(R.id.jz_video); MediaMetadataRetriever media = new MediaMetadataRetriever(); if (Build.VERSION.SDK_INT >= 14) { media.setDataSource(path, new HashMap<String, String>()); } else { media.setDataSource(path); } //获取第一帧 Bitmap bitmap = media.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC); media.release(); //用于Glide可以加载Bitmap图片 ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] bytes = baos.toByteArray(); myJZVideoPlayerStandard.setUp(path, JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL, "视频播放"); // Glide.with(this).load("http://jzvd-pic.nathen.cn/jzvd-pic/1bb2ebbe-140d-4e2e-abd2-9e7e564f71ac.png").into(myJZVideoPlayerStandard.thumbImageView); Glide.with(this).load(bytes).into(myJZVideoPlayerStandard.thumbImageView); // JZVideoPlayer.setJzUserAction(null); } public void jumpToText(View v) { startActivity(new Intent(this, TextActivity.class)); }
6.视频下载
/** * 检查并下载视频资源 */ public void downLoadVideo(View v) { File file = new File(path); Log.e("TAG", "===>" + path); //文件夹不存在,则创建它 if (!file.exists()) { file.mkdir(); } FileDownloader.setup(this); FileDownloader.getImpl().create(videoUrl) .setPath(path + "/" + videoName) .setForceReDownload(false)//强制重新下载,将会忽略检测文件是否健在 .setListener(new FileDownloadListener() { @Override protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) { //等待,已经进入下载队列 Log.e("TAG", "pending"); } @Override protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) { //下载进度 Log.e("TAG", "--" + i++); } @Override protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) { Log.e("TAG", "retry"); } @Override protected void completed(BaseDownloadTask task) { //完成整个下载过程 i = 0; } @Override protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) { //暂停下载 Log.e("TAG", "paused"); } @Override protected void error(BaseDownloadTask task, Throwable e) { //下载出现错误 Log.e("TAG", "error===>" + e.getMessage()); } @Override protected void warn(BaseDownloadTask task) { //在下载队列中(正在等待/正在下载)已经存在相同下载连接与相同存储路径的任务 Log.e("TAG", "warn"); } }).start(); }