live.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {getDataSet, getEventParam, toast} from "../../utils/utils";
  2. import {liveSources} from "../../model/enum";
  3. import {timStore} from "../../store/tim";
  4. import {createStoreBindings} from "mobx-miniprogram-bindings";
  5. import Tim from "../../model/tim";
  6. import User from "../../model/user";
  7. import EduTRTC from "../../model/edu-trct";
  8. import Route from "../../model/route";
  9. Page({
  10. data: {
  11. liveSourcesEnum: liveSources,
  12. user: {},
  13. groupId: '',
  14. userSig: '',
  15. scheduleId: '',
  16. isFull:false,
  17. bigIndex:0,
  18. isLive:false,
  19. },
  20. async onLoad(options) {
  21. wx.setKeepScreenOn({
  22. keepScreenOn: true,
  23. })
  24. this.storeBindings = createStoreBindings(this, {
  25. store: timStore,
  26. fields: ['isCanShare', 'isAudVideo', 'isAudText', 'isStuVideo', 'pusher', 'playerList'],
  27. actions: ['setScroll', 'pushMessage', 'resetMessage', 'logout', 'initLive', "exitRoom","setPusherAttributesHandler"]
  28. })
  29. let type = options.type;
  30. let title = options.title;
  31. let eId = options.eId;
  32. let scheduleId = options.scheduleId;
  33. let eduStuName = options.eduStuName;
  34. let userID = "";
  35. let groupId = "";
  36. if (type == liveSources.DEFAULT) {
  37. userID = "student_" + eId;
  38. } else if (type == liveSources.GUEST) {
  39. userID = "guest_" + eId;
  40. }
  41. groupId = "schedule_" + scheduleId;
  42. const res = await User.getUserSign(userID);
  43. const userSig = res.userSig
  44. Tim.SDKAppID = res.appID;
  45. this.setData({
  46. type,
  47. user: {nickname: eduStuName, avatar: '', gender: 1, userID},
  48. groupId,
  49. scheduleId,
  50. userSig
  51. })
  52. wx.setNavigationBarTitle({title})
  53. this.initLive(this);
  54. setTimeout(()=>EduTRTC.getInstance().getSDK().getPusherInstance().start(), 500)
  55. },
  56. async reply(e) {
  57. let msg = getEventParam(e, "msg")
  58. if (!msg.trim()) {
  59. toast('请输入内容')
  60. return;
  61. }
  62. const message = Tim.getInstance().createGroupTextMsg(msg, this.data.groupId);
  63. this.setScroll(true)
  64. this.pushMessage(message)
  65. const res = await Tim.getInstance().sendMessage(message);
  66. this.selectComponent('.bottom-reply').clear();
  67. },
  68. async hand(e) {
  69. const message = Tim.getInstance().createHandMsg(this.data.groupId);
  70. const res = await Tim.getInstance().sendCMD(message);
  71. },
  72. async onUnload() {
  73. wx.setKeepScreenOn({
  74. keepScreenOn: false,
  75. })
  76. this.exitRoom()
  77. await this.logout()
  78. this.storeBindings.destroyStoreBindings()
  79. },
  80. changeFull(e){
  81. this.setData({
  82. isFull:!this.data.isFull
  83. })
  84. },
  85. handlePusher(e) {
  86. console.log("handlePusher", e)
  87. },
  88. handlePlayer(e) {
  89. console.log("handlePlayer", e)
  90. let index = getDataSet(e,'index');
  91. if (this.data.bigIndex == index || (index == -1 && !this.data.pusher.enableCamera)){
  92. return;
  93. }
  94. this.setData({
  95. bigIndex:index
  96. })
  97. },
  98. switchCamera() {
  99. wx.createLivePusherContext().switchCamera({
  100. success() {
  101. },
  102. fail() {
  103. toast("切换失败")
  104. },
  105. }
  106. )
  107. },
  108. quitLink() {
  109. this.setPusherAttributesHandler({enableCamera: false, enableMic: false})
  110. },
  111. // 请保持跟 wxml 中绑定的事件名称一致
  112. _pusherStateChangeHandler(event) {
  113. console.log("_pusherStateChangeHandler",event);
  114. const code = getEventParam(event,"code")
  115. if (code == 1018){
  116. this.setData({
  117. isLive:true
  118. })
  119. }
  120. EduTRTC.getInstance().getSDK().pusherEventHandler(event)
  121. },
  122. _pusherNetStatusHandler(event) {
  123. EduTRTC.getInstance().getSDK().pusherNetStatusHandler(event)
  124. },
  125. _pusherErrorHandler(event) {
  126. console.log("_pusherErrorHandler",event);
  127. EduTRTC.getInstance().getSDK().pusherErrorHandler(event)
  128. },
  129. _pusherBGMStartHandler(event) {
  130. EduTRTC.getInstance().getSDK().pusherBGMStartHandler(event)
  131. },
  132. _pusherBGMProgressHandler(event) {
  133. EduTRTC.getInstance().getSDK().pusherBGMProgressHandler(event)
  134. },
  135. _pusherBGMCompleteHandler(event) {
  136. EduTRTC.getInstance().getSDK().pusherBGMCompleteHandler(event)
  137. },
  138. _pusherAudioVolumeNotify(event) {
  139. EduTRTC.getInstance().getSDK().pusherAudioVolumeNotify(event)
  140. },
  141. _playerStateChange(event) {
  142. console.log("_playerStateChange",event);
  143. EduTRTC.getInstance().getSDK().playerEventHandler(event)
  144. },
  145. _playerFullscreenChange(event) {
  146. EduTRTC.getInstance().getSDK().playerFullscreenChange(event)
  147. },
  148. _playerNetStatus(event) {
  149. EduTRTC.getInstance().getSDK().playerNetStatus(event)
  150. },
  151. _playerAudioVolumeNotify(event) {
  152. EduTRTC.getInstance().getSDK().playerAudioVolumeNotify(event)
  153. },
  154. onShareAppMessage(options) {
  155. return {
  156. path: Route.getIndexUrl(this.data.scheduleId)
  157. }
  158. },
  159. //放大pusher的方法(就是改变pusher控件的大小)
  160. //放大player的方法
  161. testPlayerFullScreen(id){
  162. let livePlayerContext = wx.createLivePlayerContext(id);
  163. //进入全屏
  164. let direction = 0;
  165. let item = this.data.playerList[0];//item是流集合里的一个
  166. // // 已知问题:视频的尺寸需要等待player触发NetStatus事件才能获取到,如果进房就双击全屏,全屏后的方向有可能不对。
  167. if (item.netStatus && item.netStatus.videoWidth && item.netStatus.videoHeight) {
  168. // 如果是横视频,全屏时进行横屏处理。如果是竖视频,则为0
  169. direction = item.netStatus.videoWidth > item.netStatus.videoHeight ? 90 : 0
  170. }
  171. livePlayerContext.requestFullScreen({
  172. direction,
  173. success(res) {
  174. }
  175. })
  176. //退出全屏
  177. livePlayerContext.exitFullScreen({
  178. success(res) {
  179. }
  180. })
  181. }
  182. });