diff --git a/README.md b/README.md index 187c5b6..45e59cf 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,62 @@ # cordova-plugin-trtc -腾讯实时音视频(TRTC)封装cordova插件 (示例) +腾讯实时音视频(TRTC)封装cordova插件 -具体原生项目参考tencent的demo, 源码大部分从这里搬运: +## 使用方法 -[https://github.com/zhaoyang21cn/Android_TRTC](https://github.com/zhaoyang21cn/Android_TRTC) +```bash +cordova plugin add git+http://m.shuto.cn:8680/public/cordova-plugin-trtc.git +``` +## 内置方法 -[https://github.com/zhaoyang21cn/iOS_TRTC](https://github.com/zhaoyang21cn/iOS_TRTC) +### 加入视频会议 -iOS的sdk需要自己添加到src/ios/libs下, 可以从官网获取 +```javascript +cordova.plugin.trtc.joinChannel( + { + ROOM_ID: 123123, // 房间号 + USER_ID: '', // 用户id + SDK_APP_ID: '', // TRTC app id + USER_SIG: '' // 用户签名 + }, + res => { console.log(res); }, + err => { console.log(err); }, + ); +``` + +### 用户信息变更 + +```javascript +cordova.plugin.trtc.joinChannel( + { + room: 123123, // 房间号 + userId: '', // 用户id + displayName: '', // 用户名 + }, + res => { console.log(res); }, + err => { console.log(err); }, + ); +``` + +### 主播窗口变更事件 + +```typescript +document.addEventListener( + cordova.plugin.trtc.Constant.EVENT_ON_LAYOUT_CHANGE, + (event: {room:string,userId:string}) => { + }, + false + ); +``` + +### 用户加入房间事件 + +```typescript +ocument.addEventListener( + cordova.plugin.trtc.Constant.EVENT_ON_USER_VIDEO_AVAILABLE, + (event: {room:string,userId:string,available:boolean}) => { + }, + false + ); +``` diff --git a/src/android/java/com/tencent/trtc/CustomVideoView.java b/src/android/java/com/tencent/trtc/CustomVideoView.java index f9ac607..e683d15 100644 --- a/src/android/java/com/tencent/trtc/CustomVideoView.java +++ b/src/android/java/com/tencent/trtc/CustomVideoView.java @@ -61,14 +61,6 @@ public class CustomVideoView extends RelativeLayout { }else { setVisibility(alwaysHide?INVISIBLE:VISIBLE); } -// if(this.userInfo != null){ -// if(this.userInfo.isLocal()){ -// mTRTCCloud.stopLocalPreview(); -// mTRTCCloud.stopLocalAudio(); -//// }else{ -//// mTRTCCloud.stopRemoteView(this.userInfo.getPersonid()); -// } -// } if(isChanged(userInfo)){ this.userInfo = userInfo; if(this.userInfo != null){ @@ -119,7 +111,7 @@ public class CustomVideoView extends RelativeLayout { Events.addListener("userinfo.update",(extra -> { if(this.userInfo !=null && this.userInfo.getDisplayName() == null && this.userInfo.getDisplayName().length() == 0 - && extra.getString("personid").equals(this.userInfo.getPersonid())){ + && extra.getString("userId").equals(this.userInfo.getPersonid())){ this.titleView.setText(extra.getString("displayName",this.userInfo.getDisplayName())); } })); diff --git a/src/android/java/com/tencent/trtc/TrtcPlugin.java b/src/android/java/com/tencent/trtc/TrtcPlugin.java index 27147ef..99f3de2 100644 --- a/src/android/java/com/tencent/trtc/TrtcPlugin.java +++ b/src/android/java/com/tencent/trtc/TrtcPlugin.java @@ -50,7 +50,7 @@ public class TrtcPlugin extends CordovaPlugin { if(object != null){ Events.fireEvent("userinfo.update",extra -> { try { - extra.putString("personid",object.getString("personid")); + extra.putString("userId",object.getString("userId")); extra.putString("displayName",object.getString("displayName")); } catch (JSONException e) { e.printStackTrace(); diff --git a/src/android/java/com/tencent/trtc/event/Events.java b/src/android/java/com/tencent/trtc/event/Events.java index 2a3b51d..c71f8d9 100644 --- a/src/android/java/com/tencent/trtc/event/Events.java +++ b/src/android/java/com/tencent/trtc/event/Events.java @@ -2,8 +2,6 @@ package com.tencent.trtc.event; import android.os.Bundle; -import org.json.JSONException; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -11,7 +9,7 @@ import java.util.Map; public class Events { private static final String PREFIX = "com.tencent.trtc.event"; - private static Map> events = new HashMap(); + private static final Map> events = new HashMap(); public static void fireEvent(String event){ fireEvent(event,null); } diff --git a/src/android/java/com/tencent/trtc/videocall/VideoCallingActivity.java b/src/android/java/com/tencent/trtc/videocall/VideoCallingActivity.java index 992b34a..ea6b26e 100644 --- a/src/android/java/com/tencent/trtc/videocall/VideoCallingActivity.java +++ b/src/android/java/com/tencent/trtc/videocall/VideoCallingActivity.java @@ -167,8 +167,8 @@ public class VideoCallingActivity extends TRTCBaseActivity implements View.OnCli try { JSONObject object = new JSONObject(); object.put("room",mRoomId); - object.put("personid",viewUser.getPersonid()); - CordovaEventKit.kit.fireEvent("LayoutChangeMessage",object); + object.put("userId",viewUser.getPersonid()); + CordovaEventKit.kit.fireEvent("onLayoutChangeMessage",object); } catch (JSONException e) { e.printStackTrace(); } @@ -180,7 +180,7 @@ public class VideoCallingActivity extends TRTCBaseActivity implements View.OnCli Events.fireEvent("subview.always.hide",(extra)->extra.putBoolean("alwaysHide",false)); Events.addListener("userinfo.update",(extra -> { - String user = extra.getString("personid"); + String user = extra.getString("userId"); int index = this.mUserList.indexOf(user); if(index > -1){ this.mUserList.get(index).setDisplayName(extra.getString("displayName")); @@ -204,8 +204,8 @@ public class VideoCallingActivity extends TRTCBaseActivity implements View.OnCli JSONObject object = new JSONObject(); try { object.put("room",mRoomId); - object.put("personid",mUserList.get(0).getPersonid()); - CordovaEventKit.kit.fireEvent("LayoutChangeMessage",object); + object.put("userId",mUserId); + CordovaEventKit.kit.fireEvent("onLayoutChangeMessage",object); } catch (JSONException e) { e.printStackTrace(); } @@ -302,16 +302,6 @@ public class VideoCallingActivity extends TRTCBaseActivity implements View.OnCli } } - private void refreshRemoteVideoViews() { - for (int i = 0; i < mRemoteViewList.size(); i++) { - if (i < mUserList.size()) { - UserInfo user = mUserList.get(i); - mRemoteViewList.get(i).changeUser(mTRTCCloud,user); - } else { - mRemoteViewList.get(i).changeUser(mTRTCCloud,null); - } - } - } private class TRTCCloudImplListener extends TRTCCloudListener { @@ -342,6 +332,16 @@ public class VideoCallingActivity extends TRTCBaseActivity implements View.OnCli refreshRemoteVideoViews(); } + JSONObject object = new JSONObject(); + try { + object.put("room",mRoomId); + object.put("userId",userId); + object.put("available",available); + CordovaEventKit.kit.fireEvent("onUserVideoAvailable",object); + } catch (JSONException e) { + e.printStackTrace(); + } + } private void refreshRemoteVideoViews() { diff --git a/www/trtc.js b/www/trtc.js index 809c9ed..f40f152 100644 --- a/www/trtc.js +++ b/www/trtc.js @@ -8,10 +8,16 @@ Trtc.prototype.joinChannel = function(arg, success, error) { Trtc.prototype.userInfoChange = function(userInfo,success, error) { exec(success, error, 'Trtc', 'userInfoChange', [userInfo]); -} +}; Trtc.prototype.fireEvent = function(event,extra) { cordova.fireDocumentEvent("trtc."+event, extra); -} +}; + +Trtc.prototype.Constant = { + EVENT_ON_LAYOUT_CHANGE :'trtc.onLayoutChangeMessage', + EVENT_ON_USER_VIDEO_AVAILABLE :'trtc.onUserVideoAvailable', +}; + module.exports = new Trtc();