1.注册声网账号 2.添加小程序开发所需服务端域名 3.下载SDK引入 初始化 4.引入项目
const AgoraMiniappSDK = require('../../utils/mini-app-sdk-production.js');
const client = new AgoraMiniappSDK.Client();
client.init("Sdkappid", (ev)=>{
console.log('声网SDK初始化成功:',ev);
let token = '',
channel = '1',
uid = `${parseInt(Math.random() * 1000000)}`;
self.JoinStartlive(token,channel,uid);
console.log(uid)
},(err)=>{
console.log("声网SDK初始化失败:", err);
})
JoinStartlive(token,channel,uid){
let self = this;
client.join(token, channel, uid, (evs)=>{
console.log("加入频道成功:", evs);
client.setRole('audience',(user)=>{
console.log('用户角色设置成功:',user)
self.liveEventChange();
});
},(err)=>{
console.log("加入频道失败:", err);
});
},
client.subscribe(1659110820, (res)=>{
console.log("订阅视频流成功playUrl:" + res);
self.setData({
playUrl:res
});
},(err)=>{
console.log("订阅视频流错误:", err);
});
liveEventChange(){
let self = this;
client.subscribe(1659110820, (res)=>{
console.log("订阅视频流成功playUrl:" + res);
self.setData({
playUrl:res
});
},(err)=>{
console.log("订阅视频流错误:", err);
});
client.on('stream-added',(evt)=>{
console.log('远端有人加入频道',evt)
});
client.on('token-privilege-will-expire',(evt)=>{
console.log('Token服务即将过期:',evt)
});
},
|