使用vitamio做直播demo

来源:互联网 发布:计算机绘图软件 编辑:程序博客网 时间:2024/06/05 22:36

项目引入vitamio后做一个简单的直播demo。

直播获取网站:http://www.hdpfans.com/

参考:http://www.cnblogs.com/over140/archive/2012/07/15/2591427.html

1.xml文件中引入布局

<io.vov.vitamio.widget.VideoView        android:id="@+id/surface_view"        android:layout_width="match_parent"        android:layout_height="match_parent"        />
2.java代码
@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_live);        ButterKnife.bind(this);        if (!LibsChecker.checkVitamioLibs(this))            return;        url = "";        initPlayer();    }    private void initPlayer() {        Vitamio.isInitialized(getApplication());        videoView.setVideoURI(Uri.parse(url));        videoView.requestFocus();        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {            @Override            public void onPrepared(MediaPlayer mp) {                videoView.start();            }        });        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {            @Override            public boolean onError(MediaPlayer mp, int what, int extra) {                if (count > RETRY_TIMES) {                    new AlertDialog.Builder(LiveActivity.this)                            .setMessage(R.string.error_message)                            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {                                @Override                                public void onClick(DialogInterface dialog, int which) {                                    LiveActivity.this.finish();                                }                            }).setCancelable(false).show();                } else {                    videoView.stopPlayback();                    videoView.setVideoURI(Uri.parse(url));                    videoView.start();                }                count++;                return false;            }        });        videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {            @Override            public boolean onInfo(MediaPlayer mp, int what, int extra) {                switch (what) {                    case MediaPlayer.MEDIA_INFO_BUFFERING_START:                        llLoading.setVisibility(View.VISIBLE);                        break;                    case MediaPlayer.MEDIA_INFO_BUFFERING_END:                    case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:                    case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:                        llLoading.setVisibility(View.GONE);                        break;                }                return false;            }        });    }
运行代码即可。

效果如图: