agora声网接入微信小程序准备工作

一、下载agora的微信小程序sdk,配置和注册相关信息

agora微信小程序文档地址:https://docs.agora.io/cn/Interactive%20Broadcast/landing-page?platform=%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F
根据文档注册agora账号,以及小程序配置,文档写的很清楚并且有截图

二、通过文档快速的实现视频直播,开始写自己的第一个demo

image.png

2.1 html代码

直播播放

  <view >
    <live-player
        id="player"
        src="{{rtmpUrl}}"
        mode="RTC"
        bindstatechange="playerStateChange"
        binderror="playerror"
        autoplay="{{true}}"
        object-fit="fillCrop" />
  </view>

直播录制

<view style="margin-top:100rpx">
    <live-pusher
        url="{{rtmpUrl}}"
        mode="RTC"
        bindstatechange="recorderStateChange"
        style="height:600rpx;width:600rpx"
        autopush="{{true}}"
        />

2.2 js代码

先理一遍逻辑,如何实现直播

1.agora的sdk初始化,传入房间号(channel)、用户ID uid(自定义)、用户角色role(audience观众 ,broadcaster主播,token(服务端生成)
2.初始化后会发布返回本地流的rtmp 的url,至此就完成了发布本地流和推流的过程,非常简单,实现其他观众加入和多名主播需要进步一步改造

/**声网client初始化 */
agoraClintInit(userInfo){
  return new Promise((resolve,reject)=>{
    let that=this
    let client={}
    client = new AgoraMiniappSDK.Client()
    this.subscribeEvents(client)
    this.client = client
    let channel=userInfo.channel
    let uid = userInfo.uid
    console.log("==userinfo",userInfo)
    client.setRole(userInfo.role);   //如果作为观众加入则需要设置该角色 audience观众 
    // 初始化客户端
    client.init(APPID, () => {
      Utils.log(`client init success`);
      // 加入频道
      client.join(token, channel, uid, () => {
        Utils.log(`client join channel success`);
        // 发布本地流
        if (this.isBroadcaster()) {
          client.publish(url => {
            that.setData({
              playUrl:url
            })
            Utils.log(`client publish success:${url}`);
            resolve(url);
          }, e => {
            Utils.log(`client publish failed: ${e.code} ${e.reason}`);
            reject(e)
          });
        } else {
          resolve();
        }
      }, e => {
        Utils.log(`client join channel failed: ${e.code} ${e.reason}`);
        reject(e)
      })
    }, e => {
      Utils.log(`client init failed: ${e} ${e.code} ${e.reason}`);
      reject(e)
    });
  })
      
},
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容