Android视频播放框架Vitamio集成及使用MediaPlayer+SurfaceView、VideoView+MediaController实现播放网络视频

工作内容:

准备工作【集成Vitamio】

1.使用Vitamio框架中的VideoView,MediaController播放本地视频和网络视频

2.使用Vitamio的MediaPlayer+SurfaceView(用于得到SurfaceHolder)来播放视频

3.自定义MediaController,让其播放控制栏更好用【可以放其他的控件来控制VideoView的播放状态】

【注意:使用前需先执行vitamio的初始化操作【Vitamio.initialize(this)】】

学习分享:

使用Vitamio的原因是:Vitamio能够解析很多格式的视频(MKV,FLV,MP4,MOV,TS,RMVB,MMS, RTSP, RTMP, HLS(m3u8) 等常见的多种视频流媒体协议,包括点播与直播),而我们系统自带的VideoView仅可以播放MP4,,3GP格式的视频。

【集成Vitamio】

1.在github上下载/clone“vitamio”地址:https://github.com/yixia/VitamioBundle

2.将VitamioBundle解压后,通过new —— import Module导入vitamio文件夹【出错,修改build.gradle中的compileSdkVersion,buildToolsVersion与你常用的一致即可】导入成功。

3.将vitamio添加为你原来的app的Module依赖,完成即可使用。


一、使用Vitamio框架中的VideoView,MediaController播放本地视频和网络视频

1.java代码:

import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.io.File;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.Vitamio;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;

public class Demo_5_Activity extends AppCompatActivity {
    private VideoView videoView;
    private TextView tvProgress;
    private TextView tvDownloadSpeed;
    private String urlString = "http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/hc.yinyuetai.com/D046015255134077DDB3ACA0D7E68D45.flv";
    private MediaController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_5_);
        File file = Environment.getExternalStorageDirectory();
        File file1 = new File(file,"a11.mp4");
        //判断Vitamio是否已经初始化【并且初始化】
        if (Vitamio.initialize(this)) {
            videoView = (VideoView) findViewById(R.id.vitamio);
//            videoView.setVideoURI(Uri.parse(urlString));//播放网络视频
            videoView.setVideoPath(file1.getPath());//播放本地视频
            controller = new MediaController(this);//视频控制器
            videoView.setMediaController(controller);//关联控制器
            videoView.start();
        }
        initView();
        setListener();
    }
    private void setListener() {
        //设置缓冲进度的监听
        videoView.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                tvProgress.setText(percent + "%");
            }
        });
        //设置缓冲下载监听
        videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                switch (what) {
                    //开始缓冲
                    case MediaPlayer.MEDIA_INFO_BUFFERING_START://开始缓冲时的视图变化
                        tvProgress.setVisibility(View.VISIBLE);
                        tvDownloadSpeed.setVisibility(View.VISIBLE);
                        mp.pause();
                        break;
                    //缓冲结束
                    case MediaPlayer.MEDIA_INFO_BUFFERING_END://缓冲好后的视图变化(可播放)
                        tvProgress.setVisibility(View.GONE);
                        tvDownloadSpeed.setVisibility(View.GONE);
                        mp.start();
                        break;
                    //正在缓冲
                    case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:
                        tvDownloadSpeed.setText("当前网速:" + extra + "kb/s");//下载是速度
                        break;
                }
                return true;
            }
        });
        //设置准备监听(判断一准备好播放)
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                videoView.setBufferSize(512*1025);//设置缓冲区大小
            }
        });
    }

    private void initView() {
        tvProgress = (TextView) findViewById(R.id.tv_progress_home);
        tvDownloadSpeed = (TextView) findViewById(R.id.tv_download_home);
    }
}
2.对应xml代码:


    
        
            
        
        
        
    
演示效果:

Android视频播放框架Vitamio集成及使用MediaPlayer+SurfaceView、VideoView+MediaController实现播放网络视频_第1张图片Android视频播放框架Vitamio集成及使用MediaPlayer+SurfaceView、VideoView+MediaController实现播放网络视频_第2张图片

二、使用Vitamio的MediaPlayer+SurfaceView(用于得到SurfaceHolder)来播放视频

java代码:

import java.io.IOException;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.Vitamio;
import io.vov.vitamio.utils.Log;
import read.liu.com.videoplayer.R;

public class MediaActivity extends AppCompatActivity {
    private long playTime = 0;
    private MediaPlayer mediaPlayer;
    private SurfaceHolder surfaceHolder;
    private String urlString = "http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/hc.yinyuetai.com/D046015255134077DDB3ACA0D7E68D45.flv";
    private boolean isPlay = false; //用于记录是否在播放
    @BindView(R.id.sf_media)
    SurfaceView sfMedia;
    @BindView(R.id.ib_play_media)
    ImageButton ibPlayMedia;
    @BindView(R.id.pb_media)
    SeekBar pbMedia;
    @BindView(R.id.ib_fullscreen_media)
    ImageButton ibFullscreenMedia;
    private int progress1 = 1;
    private Uri uri;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            pbMedia.setProgress(progress1);
            if((playTime-progress1*1000)<=1000){
                Log.e("-----------","播放结束");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media);
        ButterKnife.bind(this);
        Vitamio.initialize(this);//初始化Vitamio
        init();
    }

    private void init() {
        //关联mediaplayer和surfaceView
        surfaceHolder = sfMedia.getHolder();
        surfaceHolder.setFormat(PixelFormat.RGBA_8888);//设置播放的清晰度(像素格式)
        mediaPlayer = new MediaPlayer(MediaActivity.this);
        mediaPlayer.setLooping(false);
        uri = Uri.parse(urlString);
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    mediaPlayer.setDisplay(surfaceHolder);//关联mediaplayer和surfaceview
                    mediaPlayer.setDataSource(MediaActivity.this, uri);//设置播放数据源
                    mediaPlayer.prepareAsync();//异步准备
                    //异步准备监听
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            playTime = mediaPlayer.getDuration();
                            pbMedia.setMax((int)playTime/1000);//设置seekbar的最大值
                            isPlay = true;
                            mediaPlayer.start();
                            startTimeTast();
                            ibPlayMedia.setImageResource(read.liu.com.wait.R.drawable.ic_adjust_black_24dp);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
        mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                switch (what) {
                    //开始缓冲
                    case MediaPlayer.MEDIA_INFO_BUFFERING_START://开始缓冲时的视图变化
                        mp.pause();
                        break;
                    //缓冲结束
                    case MediaPlayer.MEDIA_INFO_BUFFERING_END://缓冲好后的视图变化(可播放)
                        mp.start();
                        break;
                    //正在缓冲
                    case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:
                        break;
                }
                return true;
            }
        });
        //添加拖动条监听事件
        pbMedia.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //判断是用户拖动进度条产生的改变
                if(fromUser){
                    mediaPlayer.seekTo(seekBar.getProgress()*1000);
                    progress1 = progress;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {


            }
        });

    }

    private void startTimeTast() {
        new Thread(){
            @Override
            public void run() {
                while (isPlay){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.sendEmptyMessage(0);
                    progress1++;
                }
            }
        }.start();
    }

    @OnClick({R.id.ib_play_media, R.id.pb_media})
    public void onClick(View view) {
        switch (view.getId()) {
            //点击了播放/暂停按钮
            case R.id.ib_play_media:
                if (isPlay) {
                    ibPlayMedia.setImageResource(read.liu.com.wait.R.drawable.ic_adjust_black_24dp);
                    mediaPlayer.pause();
                    isPlay = false;
                } else {
                    ibPlayMedia.setImageResource(read.liu.com.wait.R.drawable.ic_play_arrow);
                    mediaPlayer.start();
                    isPlay = true;
                }
                startTimeTast();
                break;
            case R.id.pb_media:

                break;
        }
    }
    //退出后注销mediapalyer
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    @OnClick(R.id.ib_fullscreen_media)
    public void onClick() {
        //点击全屏按钮——将播放参数传入下一个全屏的界面
    }
}

你可能感兴趣的:(Android基础)