123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- import {getDataSet, getEventParam, toast} from "../../utils/utils";
- import {liveSources, LOG_TAGS} from "../../model/enum";
- import {timStore} from "../../store/tim";
- import {createStoreBindings} from "mobx-miniprogram-bindings";
- import Tim from "../../model/tim";
- import User from "../../model/user";
- import EduTRTC from "../../model/edu-trct";
- import Route from "../../model/route";
- // var log = require('../../utils/log.js') // 引用上面的 log.js 文件
- import MyLogManager from "../../utils/log";
- var log = new MyLogManager(LOG_TAGS.LIVE);
- // var testLog = new MyLogManager('test');
- Page({
- data: {
- liveSourcesEnum: liveSources,
- user: {},
- groupId: '',
- pic: '',
- userSig: '',
- scheduleId: '',
- isFull: false,
- isRotate:false,
- bigIndex: 0,
- isLive: false,
- hasRotate:false,
- // pageOptions: {}
- },
- async onLoad(options) {
- // wx.setKeepScreenOn({
- // keepScreenOn: true,
- // })
- // 退出时 退房处理 使用 ↓
- // this.storeBindings = createStoreBindings(this, {
- // store: timStore,
- // fields: ['isCanShare', 'isAudVideo', 'isAudText', 'isStuVideo', 'pusher', 'playerList', 'handReply'],
- // actions: ['setScroll', 'setHandReply', 'pushMessage', 'resetMessage', 'logout', 'initLive', "exitRoom",
- // "setPusherAttributesHandler",
- // "setPlayerAttributesHandler"]
- // })
- // this.setData({
- // pageOptions: options
- // });
-
- // const title = options.title;
- // wx.setNavigationBarTitle({title})
- //原来的 ↓
- wx.setKeepScreenOn({
- keepScreenOn: true,
- })
- this.storeBindings = createStoreBindings(this, {
- store: timStore,
- fields: ['isCanShare', 'isAudVideo', 'isAudText', 'isStuVideo', 'pusher', 'playerList', 'handReply'],
- actions: ['setScroll', 'setHandReply', 'pushMessage', 'resetMessage', 'logout', 'initLive', "exitRoom",
- "setPusherAttributesHandler",
- "setPlayerAttributesHandler"]
- })
- const {title, type, eId, scheduleId, eduStuName,pic} = options;
- let userID = "";
- let groupId = "";
- if (type == liveSources.DEFAULT) {
- userID = "student_" + eId;
- } else if (type == liveSources.GUEST) {
- userID = "guest_" + eId;
- }
- groupId = "schedule_" + scheduleId;
- const res = await User.getUserSign(userID);
- const userSig = res.userSig
- Tim.SDKAppID = res.appID;
- this.setData({
- type,
- user: {nickname: eduStuName, avatar: '', gender: 1, userID},
- groupId,
- scheduleId,
- userSig,
- pic
- }, () => {
- console.log('群组ID: ', groupId);
- log.info('onload 设置的群组ID: ', groupId);
- this.initLive(this);
- wx.nextTick(() => {
- // 为了防止直播画面不显示 ↓
- setTimeout(() => EduTRTC.getInstance().getSDK().getPusherInstance().start(), 800)
- });
- });
-
- wx.setNavigationBarTitle({title})
- },
- // async onShow(){
- // console.log('onShow -> live');
- // wx.setKeepScreenOn({
- // keepScreenOn: true,
- // });
- // const {type, eId, scheduleId, eduStuName,pic} = this.data.pageOptions;
- // let userID = "";
- // let groupId = "";
- // if (type == liveSources.DEFAULT) {
- // userID = "student_" + eId;
- // } else if (type == liveSources.GUEST) {
- // userID = "guest_" + eId;
- // }
- // groupId = "schedule_" + scheduleId;
- // const res = await User.getUserSign(userID);
- // const userSig = res.userSig
- // Tim.SDKAppID = res.appID;
- // this.setData({
- // type,
- // user: {nickname: eduStuName, avatar: '', gender: 1, userID},
- // groupId,
- // scheduleId,
- // userSig,
- // pic
- // }, () => {
- // console.log('群组ID: ', groupId);
- // log.info('onload 设置的群组ID: ', groupId);
- // this.initLive(this);
- // // 为了防止直播画面不显示 ↓
- // setTimeout(() => EduTRTC.getInstance().getSDK().getPusherInstance().start(), 800)
- // });
- // },
- async reply(e) {
- let msg = getEventParam(e, "msg")
- if (!msg.trim()) {
- toast('请输入内容')
- return;
- }
- const message = Tim.getInstance().createGroupTextMsg(msg, this.data.groupId);
- this.setScroll(true)
- try {
- await Tim.getInstance().sendMessage(message);
- this.pushMessage(message)
- this.selectComponent('.bottom-reply').clear();
- } catch (e) {
- toast(e.message)
- }
- },
- async hand(e) {
- const message = Tim.getInstance().createHandMsg(this.data.groupId);
- const res = await Tim.getInstance().sendCMD(message, ({code}) => {
- if (code == 10017) {
- toast('举手失败:禁言情况不能举手');
- this.setHandReply(!this.data.handReply);
- }
- });
- },
- // async handleLeaveRoom(){
- // wx.setKeepScreenOn({
- // keepScreenOn: false,
- // })
- // this.exitRoom()
- // await this.logout()
- // // this.storeBindings.destroyStoreBindings()
- // },
- // onSaveExitState(){
- // testLog.info('exit room');
- // this.handleLeaveRoom();
- // console.log('exitRoom -> live');
- // },
- // onHide(){
- // console.log('exit');
- // testLog.info('exit room');
- // this.handleLeaveRoom();
- // },
- async onUnload() {
- wx.setKeepScreenOn({
- keepScreenOn: false,
- })
- this.exitRoom()
- await this.logout()
- this.storeBindings.destroyStoreBindings()
- // this.handleLeaveRoom();
- // this.storeBindings.destroyStoreBindings();
- },
- changeFull(e) {
- let verticalFullScreen = this.data.bigIndex === -1;
- if(!verticalFullScreen) {
- let currentBigerPlayer = this.data.playerList[this.data.bigIndex];
- // let livePlayerContext = EduTRTC.getInstance().getPlayerInstance(playerList.id);
- const {videoWidth, videoHeight} = currentBigerPlayer.netStatus;
- if(videoWidth && videoHeight && videoHeight > videoWidth) {
- verticalFullScreen = true;
- }
- }
- if(verticalFullScreen) {
- this.setData({
- isFull: !this.data.isFull
- });
- } else {
- this.rotate();
- }
- // let playerList = this.data.playerList[this.data.bigIndex];
- // this.rotate();
- // if (this.data.bigIndex==-1 || playerList.userID.indexOf("student") !=-1
- // || playerList.userID.indexOf("guest_") !=-1 ){
- // this.setData({
- // isFull: !this.data.isFull
- // })
- // }
- // else{
- // this.rotate();
- // }
- },
- handlePusher(e) {
- console.log("handlePusher", e)
- },
- handlePlayer(e) {
- if(this.data.isRotate){
- console.log("旋转状态下不允许切换了");
- return;
- }
- console.log("handlePlayer", e)
- let index = getDataSet(e, 'index');
- if (this.data.bigIndex == index || (index == -1 && !this.data.pusher.enableCamera)) {
- return;
- }
- this.setData({
- bigIndex: index
- })
- },
- switchCamera() {
- wx.createLivePusherContext().switchCamera({
- success() {
- },
- fail() {
- toast("切换失败")
- },
- }
- )
- },
- quitLink() {
- this.setPusherAttributesHandler({enableCamera: false, enableMic: false})
- },
- // 请保持跟 wxml 中绑定的事件名称一致
- _pusherStateChangeHandler(event) {
- console.log("_pusherStateChangeHandler************", event);
- const code = getEventParam(event, "code")
- if (code == 1018) {
- this.setData({
- isLive: true
- })
- }
- EduTRTC.getInstance().getSDK().pusherEventHandler(event)
- },
- _pusherNetStatusHandler(event) {
- EduTRTC.getInstance().getSDK().pusherNetStatusHandler(event)
- // try{
- // EduTRTC.getInstance().getSDK().pusherNetStatusHandler(event)
- // } catch(err) {
- // console.log('_pusherNetStatusHandler error:', err);
- // }
- },
- _pusherErrorHandler(event) {
- console.log("_pusherErrorHandler", event);
- EduTRTC.getInstance().getSDK().pusherErrorHandler(event)
- },
- _pusherBGMStartHandler(event) {
- EduTRTC.getInstance().getSDK().pusherBGMStartHandler(event)
- },
- _pusherBGMProgressHandler(event) {
- EduTRTC.getInstance().getSDK().pusherBGMProgressHandler(event)
- },
- _pusherBGMCompleteHandler(event) {
- EduTRTC.getInstance().getSDK().pusherBGMCompleteHandler(event)
- },
- _pusherAudioVolumeNotify(event) {
- EduTRTC.getInstance().getSDK().pusherAudioVolumeNotify(event)
- },
- _playerStateChange(event) {
- console.log("_playerStateChange", event);
- EduTRTC.getInstance().getSDK().playerEventHandler(event)
- },
- _playerFullscreenChange(event) {
- EduTRTC.getInstance().getSDK().playerFullscreenChange(event)
- let playerList = this.data.playerList[this.data.bigIndex];
- console.log("event",event)
- if (event.detail.fullScreen){
- this.setPlayerAttributesHandler(playerList,{objectFit:'contain'})
- }else{
- this.setPlayerAttributesHandler(playerList,{objectFit:'fillCrop'})
- }
- },
- _playerNetStatus(event) {
- EduTRTC.getInstance().getSDK().playerNetStatus(event)
- },
- _playerAudioVolumeNotify(event) {
- EduTRTC.getInstance().getSDK().playerAudioVolumeNotify(event)
- },
- onShareAppMessage(options) {
- let obj = {};
- if (this.data.pic){
- obj.imageUrl = this.data.pic
- }
- return {
- path: Route.getIndexUrl(this.data.scheduleId),
- ...obj
- }
- },
- rotate(){
- let that = this;
- let playerList = this.data.playerList[this.data.bigIndex];
- let livePlayerContext = wx.createLivePlayerContext(playerList.id);
- // let livePlayerContext = EduTRTC.getInstance().getPlayerInstance(playerList.id);
- const {videoWidth, videoHeight} = playerList.netStatus;
- // if(!videoWidth || !videoHeight) {
- // toast("网络状态不好,请稍后再试!");
- // return;
- // }
- const direction = videoWidth && videoHeight > videoWidth? 0 : 90;
- livePlayerContext.requestFullScreen({
- // direction: 90,
- direction,
- success(res) {
- that.setData({isRotate:true})
- },
- fail(e){
- toast("屏幕旋转失败,请稍后重试")
- console.log(e)
- }
- })
- },
- rotate2(){
- this.setPusherAttributesHandler({enableCamera: true, enableMic: true})
- },
- exitRotate(){
- let that = this;
- let playerList = this.data.playerList[this.data.bigIndex];
- let livePlayerContext = wx.createLivePlayerContext(playerList.id);
- // 解决退出全屏后 屏幕还是在旋转状态的问题 --- start
- livePlayerContext.requestFullScreen({
- direction: 0
- });
- // 解决退出全屏后 屏幕还是在旋转状态的问题 --- end
- wx.nextTick(() => {
- livePlayerContext.exitFullScreen({
- success(res) {
- that.setData({isRotate:false})
- },
- fail(e){
- toast("屏幕旋转失败,请稍后重试")
- console.log(e)
- }
- })
- });
- },
- //放大pusher的方法(就是改变pusher控件的大小)
- //放大player的方法
- testPlayerFullScreen(id) {
- let livePlayerContext = wx.createLivePlayerContext(id);
- //进入全屏
- let direction = 0;
- let item = this.data.playerList[0];//item是流集合里的一个
- // // 已知问题:视频的尺寸需要等待player触发NetStatus事件才能获取到,如果进房就双击全屏,全屏后的方向有可能不对。
- if (item.netStatus && item.netStatus.videoWidth && item.netStatus.videoHeight) {
- // 如果是横视频,全屏时进行横屏处理。如果是竖视频,则为0
- direction = item.netStatus.videoWidth > item.netStatus.videoHeight ? 90 : 0
- }
- livePlayerContext.requestFullScreen({
- direction,
- success(res) {
- }
- })
- //退出全屏
- livePlayerContext.exitFullScreen({
- success(res) {
- }
- })
- }
- });
|