close

延續上一篇

Android Agora 介紹、入門教學、實作範例 sample --- 螢幕分享 Screen Sharing ( 一 ) 』Willy's Fish教學筆記

Android Agora 介紹、入門教學、實作範例 Real-Time 直播、視訊、通話、互動遊戲 ( 二 ) 』Willy's Fish教學筆記

 

我們知道了如何送出畫面給接收端了

那接收端這邊又該如何設定呢?

今天就讓我們來看接收的方法吧

 

 

一、Create Engine

老樣子,第一步是建立 Engine

mRtcEngine = RtcEngine.create(getApplicationContext(),[Your App ID] ,
 new IRtcEngineEventHandler() {

    @Override
    public void onJoinChannelSuccess(String channel
                                           , int uid, int elapsed) {
        Log.d(TAG, "onJoinChannelSuccess: " + (uid));
    }

    @Override
    public void onUserJoined(final int uid, int elapsed) {
        Log.d(TAG, "onUserJoined: " + (uid));
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                setupRemoteView(uid);
            }
        });
    }

});

需要注意的是,在 onUserJoined 的時候

要把 UID 給 setupRemoteVideo 設定

畫面才能被設定並正確顯示

 

 

二、基本設定

由於前幾篇都講過了

所以設定部份就在這邊一起解說

首先是 channel profile

mRtcEngine.setChannelProfile(Constants.CHANNEL_PROFILE_COMMUNICATION)

由於我們這次範例都是用 communication 

所以這邊也要設定好接收模試也是 communication

mRtcEngine.enableVideo();

開啟影像畫面

mRtcEngine.setClientRole(Constants.CLIENT_ROLE_AUDIENCE)

設定端點的角色為觀眾

mRtcEngine.joinChannel(null
, getResources().getString(R.string.label_channel_name)
, ""
, Constant.AUDIENCE_UID
)

加入與發送相同的 channel 即可接收

 

 

三、setupRemoteView

接下來是遠端畫面的設定

private void setupRemoteView(int uid) {
    // 取得 surface view of Agora
    SurfaceView surfaceV = 
            RtcEngine.CreateRendererView(getApplicationContext());
    // 置前顯示
    surfaceV.setZOrderOnTop(true);
    surfaceV.setZOrderMediaOverlay(true);

    // 以不同 uid 去判斷這次接收的是哪一個 channel
    if (uid == Constant.CAMERA_UID) {
        mFlCam.addView(surfaceV, new FrameLayout.LayoutParams(
                              ViewGroup.LayoutParams.MATCH_PARENT, 
                              ViewGroup.LayoutParams.MATCH_PARENT)
                      );
    } else if (uid == Constant.SCREEN_SHARE_UID){
        mFlSS.addView(surfaceV, new FrameLayout.LayoutParams(
                             ViewGroup.LayoutParams.MATCH_PARENT, 
                             ViewGroup.LayoutParams.MATCH_PARENT)
                     );
    } else {
        Log.e(TAG, "unknown uid");
    }

    // 遠端畫面設定
    mRtcEngine.setupRemoteVideo(new VideoCanvas(surfaceV
                             , VideoCanvas.RENDER_MODE_FIT, uid)
                               );
}

我用註解的方式讓大家了解程式內容

這裡補充一點

由於我們這這個專案是同時有 直播畫面 & 分享螢幕畫面

所以才有 mFlCam & mFlSS 的區別

 

 

三、資源釋放

@Override
protected void onDestroy() {
    super.onDestroy();

    mRtcEngine.leaveChannel();

    RtcEngine.destroy();
    mRtcEngine = null;
}

 

接收端的 code 相對比較簡單,設定較少

到這裡 Agora 的教學算是告一段落了

之後若是有打算做到別的功能,如互動遊戲…等

再來寫一篇相關文章

 

 

資料來源:

https://docs.agora.io/en/Video/screensharing_android?platform=Android

https://developer.android.com/reference/android/media/projection/MediaProjection

https://github.com/AgoraIO/Advanced-Video/tree/master/Screensharing/Agora-Screen-Sharing-Android#agora-screen-sharing-android


arrow
arrow

    顏澤偉 發表在 痞客邦 留言(0) 人氣()