本文将为您介绍如何在微信小程序中集成实时音视频能力。
前置要求
小程序基础库最低版本要求 2.10.0
微信 App 最低版本要求 7.0.9
您的小程序具备 live-pusher 和 live-player 组件的使用权限,申请开通的要求和流程详情,请参见微信 live-pusher 的文档。
微信开发者工具不支持 live-pusher 和 live-player,请使用真机进行体验和测试,并且小程序测试号无法进行测试。
步骤一:开通应用
步骤二:获取应用ID和AppKey
成功开通应用后,在应用管理列表中找到该应用,单击操作栏中的管理按钮,进入基本信息页面,在该页面获取对应的应用ID和AppKey 。
步骤三:集成接入
在微信公众平台-管理-开发管理-服务器域名中设置
socket
合法域名,如下所示:wss://rs.rtn.aliyuncs.com
在您的小程序js中定义data用来存储
live-pusher
和live-player
的数据。Component({ data: { pusher: {}, playerList: [], }, });
在您的小程序wxml中将数据与
live-pusher
和live-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" <!-- 其余属性省略,具体有哪些属性可参考 live-pusher 文档 --> /> <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" <!-- 其余属性省略,具体有哪些属性可参考 live-player 文档 --> /> </view>
使用 npm 集成 SDK。
npm install aliyun-rtc-wx-sdk
初始化引擎。
import AliRtcEngine from "aliyun-rtc-wx-sdk"; this.artc = AliRtcEngine.getInstance(this)
创建好AliRtcEngine实例后,需要监听、处理相关事件。
this.artc.on("remoteUserOnLineNotify", (user, playerList) => { wx.showToast({ title: `${user.displayName || user.userId} 加入了房间`, icon: "none", }); this.setData({ playerList, }); }); this.artc.on("remoteUserOffLineNotify", (user, playerList) => { wx.showToast({ title: `${user.displayName || user.userId} 退出了房间`, icon: "none", }); this.setData({ playerList, }); }); this.artc.on("remoteTrackAvailableNotify", (_userId, playerList) => { this.setData({ playerList, }); }); this.artc.on("bye", () => { wx.showToast({ title: "您已被踢出房间", icon: "none", }); this.setData({ pusher: {}, playerList: [], }); });
加入频道。您可以按需选择单参数入会或者多参数入会的方案。Token计算方式,请参见Token鉴权。
-
入会成功后更新 data 并开始推流。
this.setData( { pusher: pusherAttributes, playerList, }, () => { this.artc.getPusherInstance().start(); }, );
在 methods 中定义
live-pusher
和live-player
的事件处理方法。Component({ methods: { onPusherStateChange(e: any) { // live-pusher 的 code 可查看 live-pusher 的文档 console.log("live-pusher code: ", e.detail.code); this.artc.handlePusherStateChange(e); }, onPlayerStateChange(e: any) { // live-player 的 code 可查看 live-player 的文档 console.log("live-player code: ", e.detail.code); this.artc.handlePlayerStateChange(e); }, }, });
离开会议。
await this.artc.leaveChannel(); this.setData({ pusher: {}, playerList: [], });
该文章对您有帮助吗?
- 本页导读
- 前置要求
- 步骤一:开通应用
- 步骤二:获取应用ID和AppKey
- 步骤三:集成接入