局域網無紙化會議封裝,只需三步實現推流和播放

又名 基於rstp的錄屏直播android

結構圖

vlc播放效果

上篇文章git

步驟:

1.下載lib到本地,而後依賴

RtspServerAndVlcPlaygithub

git clone https://github.com/wobiancao/RtspServerAndVlcPlay.gitbash

  1. 若是隻是推流端,只須要依賴 rtsplibraryide

  2. 若是隻是播放端,只須要依賴 libvlc-android佈局

重點聲明: rtsplibrary是基於libstreaming開源封裝 libvlc-android是別人開源vlc項目編譯好的,非本人編譯ui

libvlc-android 原項目地址: vlc-android-sdk 多點幾個star給做者鼓勵吧this

2.推流端

  1. 初始化
public static final int REQUEST_CODE = 1001;
    private RtspServer mRtspServer;
    private MediaProjectionManager mMediaProjectionManager;
    private ScreenRecordThread mScreenRecord;

    mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
    private ServiceConnection mRtspServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mRtspServer = ((RtspServer.LocalBinder)service).getService();
            mRtspServer.addCallbackListener(mRtspCallbackListener);
            mRtspServer.start();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {}
    };
複製代碼
  1. 受權成功後開啓錄屏線程服務
//請求受權
            Intent captureIntent = mMediaProjectionManager.createScreenCaptureIntent();
            startActivityForResult(captureIntent, REQUEST_CODE);
            bindService(new Intent(this,RtspServer.class), mRtspServiceConnection, Context.BIND_AUTO_CREATE);
            //受權回調
             public void onActivityResult(int requestCode, int resultCode, Intent data) {
              try {
                  MediaProjection mediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
                  if(mediaProjection == null){
                      T.showShort(ServerActivity.this, "程序發生錯誤:MediaProjection@1");
                      RunState.getInstance().setRun(false);
                    return;
            }
           //開啓錄屏線程
            mScreenRecord = new ScreenRecordThread(this,mediaProjection, this);
            mScreenRecord.start();
        }
        catch (Exception e){
        }
    }
複製代碼
  1. 實現H264DataCollecter接口回調,設置推流數據
@Override
    public void collect(H264Data data) {
        DataUtil.getInstance().putData(data);
    }
複製代碼

3.播放端

  1. 佈局
<SurfaceView
        android:id="@+id/player_surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
複製代碼
  1. 實例化
SurfaceView mPlayView;
    private String playUrl = "";
    private LibVLC libVLC = null;
    private MediaPlayer mediaPlayer;
    private SurfaceHolder mSurfaceHolder;

        ArrayList<String> options = new ArrayList<>();
        libVLC = new LibVLC(getApplication(), options);
        mediaPlayer = new MediaPlayer(libVLC);
        mSurfaceHolder = mPlayView.getHolder();//用於設置視圖
        mSurfaceHolder.setFixedSize(DisplayUtils.getDisplayW(this), DisplayUtils.getDisplayH(this));
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mediaPlayer.getVLCVout().setVideoSurface(mPlayView.getHolder().getSurface(), mSurfaceHolder);
        mediaPlayer.getVLCVout().attachViews();
        Media media = new Media(libVLC, Uri.parse(playUrl));
        mediaPlayer.setMedia(media);
複製代碼
  1. 播放 調用mediaPlayer.play();方法便可開始播放
//設置全屏去掉狀態欄虛擬鍵 屏幕常亮
private void onPreCreate() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        //去除title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去掉Activity上面的狀態欄
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //去掉虛擬按鍵全屏顯示
//        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav
                        // bar
                        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//屏幕常亮
    }

public void onExit(View view) {
        finish();
    }

    //物理按鍵 虛擬按鍵 所有屏蔽
    @Override
    public void onBackPressed() {
        return;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HOME || keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mediaPlayer != null) {
            mediaPlayer.pause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mediaPlayer != null) {
            mediaPlayer.play();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

複製代碼

體驗

qr.png

apk下載地址spa

本項目github地址線程

歡迎star

Thanks

libstreaming

AndroidShow

ScreenRecorder

vlc-android-sdk

相關文章
相關標籤/搜索