WeChat Mini Program SDK
This topic describes how to integrate ApsaraVideo Real-time Communication into a WeChat mini program.
Prerequisites
The minimum required version of the mini program base library is 2.10.0.
The minimum required version of the WeChat app is 7.0.9.
Your mini program has permission to use the live-pusher and live-player components. For information about the requirements and the procedure to enable these permissions, see the WeChat live-pusher documentation.
WeChat DevTools does not support the live-pusher and live-player components. Use a physical device for development and testing. Test accounts for mini programs cannot be used for testing.
Step 1: Create an application
-
Log in to the ApsaraVideo Live console.
-
In the left-side navigation pane, click .
-
Click Create Application.
-
Enter a custom application name, select the service agreement checkbox, and click Buy Now.
-
Once the application is created, refresh the Application Management page to view your new ApsaraVideo for RTC application.
NoteCreating an application does not incur charges by default. Billing is pay-as-you-go and based on actual usage. For more information, see RTC Pricing.
Step 2: Get application ID and AppKey
After you create the application, find it in the application list. In the Actions column, click Manage to open the Basic Information page. On this page, find the Application ID and AppKey.
Step 3: Integrate the SDK
On the WeChat Official Accounts Platform, navigate to Management > Development Management > Server Domain Names. Set the valid domain names for
requestandsocketas follows:Valid domain names for request
https://alivc-aio.cn-hangzhou.log.aliyuncs.com https://apsara-video-client.cn-shanghai.log.aliyuncs.com https://gw.rtn.aliyuncs.com https://slsrole.alicdn.comValid domain names for socket
wss://rs.rtn.aliyuncs.comIn your mini program's JS file, define data for
live-pusherandlive-player.Component({ data: { pusher: {}, playerList: [], }, });In your mini program's WXML file, bind the data to
live-pusherandlive-player.<live-pusher url="{{pusher.url}}" beauty="{{pusher.beauty}}" whiteness="{{pusher.whiteness}}" mode="{{pusher.mode}}" autopush="{{pusher.autopush}}" mirror="{{pusher.mirror}}" local-mirror="{{pusher.localMirror}}" remote-mirror="{{pusher.remoteMirror}}" enable-ans="{{pusher.enableAns}}" enable-agc="{{pusher.enableAgc}}" device-position="{{pusher.devicePosition}}" muted="{{!pusher.enableMic}}" enable-mic="{{pusher.enableMic}}" enable-camera="{{pusher.enableCamera}}" bindstatechange="onPusherStateChange" bindnetstatus="onPusherNetStatus" binderror="onPusherError" <!-- Other properties are omitted. For more information about the properties, see the live-pusher documentation. --> /> <view class="player-container" wx:for="{{playerList}}" wx:key="id"> <live-player id="{{item.id}}" src="{{item.src}}" mode="{{item.mode}}" autoplay="{{item.autoplay}}" muted="{{item.muted}}" mute-audio="{{item.muteAudio}}" mute-video="{{item.muteVideo}}" min-cache="{{item.minCache}}" max-cache="{{item.maxCache}}" bindstatechange="onPlayerStateChange" bindnetstatus="onPlayerNetStatus" <!-- Other properties are omitted. For more information about the properties, see the live-player documentation. --> /> </view>Use npm to integrate the SDK.
npm install aliyun-rtc-wx-sdkInitialize the engine.
import AliRtcEngine from "aliyun-rtc-wx-sdk"; this.artc = AliRtcEngine.getInstance(this)After you create the AliRtcEngine instance, listen for and handle related events.
this.artc.on("remoteUserOnLineNotify", (user, playerList) => { wx.showToast({ title: `${user.displayName || user.userId} joined the channel`, icon: "none", }); this.setData({ playerList, }); }); this.artc.on("remoteUserOffLineNotify", (user, playerList) => { wx.showToast({ title: `${user.displayName || user.userId} left the channel`, icon: "none", }); this.setData({ playerList, }); }); this.artc.on("remoteTrackAvailableNotify", (user, playerList) => { this.setData({ playerList, }); }); this.artc.on("bye", () => { wx.showToast({ title: "You have been kicked out of the channel", icon: "none", }); this.setData({ pusher: {}, playerList: [], }); });Join a channel. You can join a channel with a single parameter or multiple parameters as needed. For information about how to calculate a token, see Token-based authentication.
After you join the channel, update data and start stream ingest.
this.setData( { pusher: pusherAttributes, playerList, }, () => { this.artc.getPusherInstance().start(); }, );In the methods object, define the event handling methods for
live-pusherandlive-player.Component({ methods: { onPusherStateChange(e: any) { // For information about the code for live-pusher, see the live-pusher documentation. console.log("live-pusher code: ", e.detail.code); this.artc.handlePusherStateChange(e); }, onPusherNetStatus(e: any) { console.log("live-pusher netStatus: ", e.detail.info); this.artc.handlePusherNetStatus(e); }, onPusherError(e: any) { this.artc.handlePusherError(e); }, onPlayerStateChange(e: any) { // For information about the code for live-player, see the live-player documentation. console.log("live-player code: ", e.detail.code); this.artc.handlePlayerStateChange(e); }, onPlayerNetStatus(e: any) { console.log("live-player netStatus: ", e.detail.info); this.artc.handlePlayerNetStatus(e); }, }, });Leave the meeting.
await this.artc.leaveChannel(); this.setData({ pusher: {}, playerList: [], });