使用SurfaceView进行rtsp格式流的播放

2019-08-15 17:41:21 浏览数 (1)

  • 在网上找的rtsp流的视频链接
代码语言:javascript复制
rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov
  • 设置Activity横屏
代码语言:javascript复制
    if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
  • 开启线程进行视频的播放
代码语言:javascript复制
private void PlayVideo() {

        new Thread() {
            public void run() {
                try {
                    player.setDataSource(MonitorActivity.this, Uri.parse(stream_url));
                    sh = sfv.getHolder();
                    sh.addCallback(new MyCallBack());
                    player.prepare();
                    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {

                            handler.sendEmptyMessage(DOWNLOAD_SUCCESS);
                            player.start();
                            player.setLooping(true);
                        }
                    });
                } catch (Exception e) {

                    handler.sendEmptyMessage(DOWNLOAD_FAILED);

                    Log.e("MonitorActivity", e.getMessage()   "摄像头未开启或网络异常");
                    e.printStackTrace();
                }
            }
        }.start();
    }
  • 最后要注意Activity销毁时进行资源的回收
代码语言:javascript复制
@Override
    protected void onDestroy() {
        if(player.isPlaying()){
            player.stop();//停止音频的播放
        }
        player.release();//释放资源
        super.onDestroy();
    }

0 人点赞