编程知识 cdmana.com

Android uni app for audio and video calls

Preface

The last one explained how to do it Android uni-app Encapsulating native plug-ins , This article explains , hold anyRTC Of RTC( Audio and video communication ) encapsulation uni-app Realize audio and video call .

Don't understand anyRTC Little buddy , You can click on the link below :

Developer website

1. design sketch

First map , Post explanation !

1.1 home page

1.2 Visitor interface

1.3 Anchor interface

2.GitHub Address

uni-app demo: Click to download

Scan and download :

4. Code

4.1 Integrated plug-ins

const RtcModule = uni.requireNativePlugin('AR-RtcModule');
  • AR-RtcModule: The plug-in name , Home integration plug-in

4.2 Initial event callback

//callback  receive 
callbackFn() {
	RtcModule.setCallBack((res) => {
		switch (res.engineEvent) {
			case "onWarning":
				this.promptFn("warn", res.warningCode);
				break;
			case "onError":
				res.errorCode != 18 ? this.promptFn("error", res.errorCode) : '';
				break;
			case "onJoinChannelSuccess": // User joined successfully 
				uni.hideLoading();
				this.role == 1 ? this.PeerVideoUser.push(res.uid) : "";
				this.videoShow = true;
				setTimeout(() => {
					// this.videoShowBg = false;
					this.promptText = ""
					// The speaker 
					RtcModule.setEnableSpeakerphone({
						"enabled": true
					}, (res) => {})
					setTimeout(() => {
						//  Enable video module .
						this.role == 1 ? this.setupLocalVideoFn() : RtcModule.enableVideo((res) => {});
					}, 200)
				}, 2000)
				break;
			case "onLeaveChannel": // Off channel callback 
				setTimeout(() => {
					this.closeAll()
				}, 500)
				break;
			case "onUserJoined": // The remote user joins the current channel callback .
				// this.promptFn("info", " The remote user joins the current channel callback ");
				this.PeerVideoUser.push(res.uid);
				break;
			case "onUserOffline": // The remote user leaves the current channel to call back .
				this.PeerVideoUser = this.PeerVideoUser.filter((x) => x !== res.uid);
				break;

			case "onFirstLocalAudioFrame": // A callback for the first frame of local audio has been sent .( Add audio to the page )
				break;
			case "onFirstLocalVideoFrame": // The callback for the first frame of the local video has been displayed .( Add local video to the page )
				// this.promptFn("error", " The callback for the first frame of the local video has been displayed ");
				break;
			case "onFirstRemoteVideoDecoded": // Remote video first frame decoding completed .( Add remote video to the page )
				// this.promptFn("info", " Remote video first frame decoding completed ");
				this.promptText = " One moment please ..."
				let uid = []
				uid.push(res.uid)
				setTimeout(() => {
					this.promptText = "";
					// this.videoShowBg = false; // Set the background switch 
					setTimeout(() => {
						uid.map(item => {
							this.$refs[`popup${item}`][0].setupRemoteVideo({
								"renderMode": 1,
								"channelId": this.chanel,
								"uid": item,
								"mirrorMode": 0
							}, (res) => {})
							// preview 
							RtcModule.startPreview((res) => {});
						})
					}, 500)

				}, 2000)
				break;
		}

	})
},

  • res.engineEvent: Receive various callbacks , Join channel successfully , Leave channel , Error code, etc .

4.3 Create examples

await RtcModule.create({
	"appId": this.appid  //anyRTC  by  App  Signed by the developer  App ID. Each project should have a unique  App ID. If you don't have it in your development kit  App ID, Please from anyRTC Official website (https://www.anyrtc.io) Apply for a new  App ID
}, (res) => {});

4.4 Set up characters

setClientRole(num) {
	this.role = num;
	// Set the user role in the live scene 
	RtcModule.setClientRole({
		"role": Number(num) //1: I'm the anchor ,2: tourists 
	}, (ret) => {});
},

4.5 Join the channel

await RtcModule.joinChannel({
	"token": "",
	"channelId": this.channel,
	"uid": this.uid
}, (res) => {})

  • token: Registration does not open token verification , It can be empty .
  • channelId: The channel you need to join ID.
  • uid: Each user is assigned a unique UID, Can't repeat .

4.6 Leave and destroy

RtcModule.leaveChannel((res) => {}); // Leave channel 
RtcModule.destroyRtc((res) => {});    // Destroy the channel 

5. summary

Basically important interfaces , This is basically the end of the introduction , If you have any questions , Leave a comment in the comments section !

author :anyRTC

Click to see the original

版权声明
本文为[anyRTC]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224110619997w.html

Scroll to Top