video.nvue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <template>
  2. <view class="video_box" :style="'height:' + phoneHeight + 'px'">
  3. <!-- 本地预览视图 -->
  4. <view class="video_me" v-if="allUserViewObjectList.length > 0" v-for="(item, index) in allUserViewObjectList" :key="item.userID" :style="'height:' + phoneHeight + 'px'">
  5. <!-- <zego-local-view v-if="videoShow" :viewMode="publisherViewModeIndex"
  6. style="width: 100%; flex: 1; border: 1px solid #0000; z-index: 20">
  7. </zego-local-view> -->
  8. <!-- 服务器拉流的视图 -->
  9. <zego-remote-view v-if="item.streamID" :streamID="item.streamID" :viewMode="item.viewMode"
  10. style="width: 100%; flex: 1; border: 1px solid #0000; z-index: 20">
  11. </zego-remote-view>
  12. </view>
  13. <!-- 服务器传来的预览图 -->
  14. <view class="video_you" v-if="allUserViewObjectList.length > 0">
  15. <!-- <zego-remote-view v-if="item.streamID" :streamID="item.streamID" :viewMode="item.viewMode"
  16. style="height: 403.84rpx; flex: 1;">
  17. </zego-remote-view> -->
  18. <!-- 本地视图 -->
  19. <zego-local-view v-if="videoShow" :viewMode="publisherViewModeIndex"
  20. style="height: 403.84rpx; flex: 1; z-index: 200">
  21. </zego-local-view>
  22. </view>
  23. <view class="video_btn">
  24. <view class="video_imgList">
  25. <view class="video_imgList1">
  26. <view class="video_view" @click="turn">
  27. <image class="video_view_img" src="../../static/image/9.png" mode=""></image>
  28. </view>
  29. <text class="video_view_txt">翻转镜头</text>
  30. </view>
  31. <view class="video_imgList2" v-if="CameraShow">
  32. <view class="video_view" @click="closeCamera">
  33. <image class="video_view_img" src="../../static/image/11.png" mode=""></image>
  34. </view>
  35. <text class="video_view_txt" style="margin: 0 0 0 30rpx;">静音</text>
  36. </view>
  37. <view class="video_imgList2" v-else>
  38. <view class="video_view" @click="openCamera">
  39. <image class="video_view_img" src="../../static/image/12.png" mode=""></image>
  40. </view>
  41. <text class="video_view_txt" style="margin: 0 0 0 30rpx;">开启</text>
  42. </view>
  43. </view>
  44. <image @click="offVideo()" style="width: 50px; height: 50px; margin: 0 0 0 330rpx;"
  45. src="../../static/image/10.png" mode=""></image>
  46. </view>
  47. <!-- 消息提醒 -->
  48. <u-toast ref="uToast"></u-toast>
  49. </view>
  50. </template>
  51. <script>
  52. import permision from "@/zego-express-video-uniapp/permission.js";
  53. import ZegoExpressEngine from '@/zego-express-video-uniapp/lib/ZegoExpressEngine';
  54. import {
  55. ZegoScenario,
  56. ZegoRoomState,
  57. ZegoUpdateType,
  58. // ZegoViewMode
  59. } from '@/zego-express-video-uniapp/lib/ZegoExpressDefines'
  60. import {
  61. AppID,
  62. AppSign
  63. } from '@/zego-express-video-uniapp/KeyCenter.js'
  64. import ZegoLocalView from '@/zego-express-video-uniapp/zego-view/ZegoLocalView';
  65. import ZegoRemoteView from '@/zego-express-video-uniapp/zego-view/ZegoRemoteView';
  66. export default {
  67. data() {
  68. return {
  69. phoneHeight: '', // 获取当前的屏幕高度
  70. // 即构
  71. publisherViewModeIndex: 0, // 本地预览图
  72. viewModeA: 0, // 服务器预览图
  73. serveViewModeIndex: "", // 服务器拉流预览图
  74. engine: undefined,
  75. videoObj: null,
  76. userid: "Uni" + Math.floor(Math.random() * 1000000).toString(),
  77. isPublishingStream: false,
  78. shotShow: false, // 翻转镜头
  79. videoShow: false, // 切换视频显示
  80. allStreamList: [],
  81. allUserViewObjectList: [],
  82. CameraShow: true, // 镜头开启关闭
  83. }
  84. },
  85. components: {
  86. ZegoLocalView: ZegoLocalView,
  87. ZegoRemoteView: ZegoRemoteView
  88. },
  89. watch: {
  90. allUserViewObjectList(val) {
  91. if (val.length > 0) {
  92. val.forEach((item) => {
  93. if (item.streamID) {
  94. this.tensile(item)
  95. return true
  96. }
  97. })
  98. }
  99. }
  100. },
  101. methods: {
  102. // 即构 - 视频通话 初始化
  103. async setup() {
  104. var that = this
  105. // 创建引擎
  106. let profile = {
  107. appID: AppID,
  108. appSign: AppSign,
  109. scenario: ZegoScenario.General
  110. }
  111. this.engine = await ZegoExpressEngine.createEngineWithProfile(profile);
  112. this.engine.useFrontCamera(this.shotShow); // 设置前后摄像头
  113. // console.log(this.engine)
  114. this.addListeners();
  115. // console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' + that.videoObj.room_id)
  116. this.engine.loginRoom(that.videoObj.room_id, {
  117. userID: that.videoObj.room_id,
  118. userName: that.videoObj.room_id
  119. }, {
  120. isUserStatusNotify: true
  121. });
  122. },
  123. // 即构 - 翻转镜头
  124. turn() {
  125. this.shotShow = !this.shotShow
  126. this.engine.useFrontCamera(this.shotShow)
  127. },
  128. // 即构 - 关闭镜头 - 停止拉流 、推流
  129. closeCamera() {
  130. console.log('关闭摄像头')
  131. this.allStreamList.forEach((item) => {
  132. // this.notTensile(item.streamID); // 停止拉流
  133. item.muteAudio = !item.muteAudio;
  134. this.engine.mutePlayStreamAudio(item.streamID, item.muteAudio);
  135. })
  136. // // 停止推流
  137. // this.engine.stopPreview();
  138. // this.engine.stopPublishingStream();
  139. this.CameraShow = false
  140. },
  141. // 即构 - 开启镜头 - 开启拉流、推流
  142. openCamera() {
  143. console.log('开启摄像头')
  144. this.allStreamList.forEach((item) => {
  145. // this.engine.startPlayingStream(item.streamID); // 开始拉流
  146. item.muteAudio = !item.muteAudio;
  147. this.engine.mutePlayStreamAudio(item.streamID, item.muteAudio);
  148. })
  149. // // 开始推流
  150. // this.engine.startPreview();
  151. // this.engine.startPublishingStream(this.videoObj.room_id);
  152. this.CameraShow = true
  153. },
  154. // 切换视频显示
  155. changeShow() {
  156. this.videoShow = !this.videoShow
  157. // this.onPublish()
  158. this.engine.startPreview();
  159. this.engine.startPublishingStream(this.videoObj.room_id);
  160. this.publishBtnName = "Stop Publishing";
  161. console.log(this.videoShow)
  162. },
  163. // 即构 - 视频通话 监听房间
  164. addListeners() {
  165. // 房间状态变化通知
  166. this.engine.on("roomStateUpdate", (roomID, state, errorCode, extendedData) => {
  167. // console.log('房间状态变化')
  168. if (state == "CONNECTED") {
  169. // 与房间连接成功,只有当房间状态是连接成功时,才能进行推流、拉流等操作。
  170. // 接下来的“预览并推流”的代码写在这里
  171. console.log("房间连接成功");
  172. }
  173. if (state == "DISCONNECTED") {
  174. // 与房间断开了连接
  175. console.log("与房间断开连接");
  176. }
  177. if (state == "CONNECTING") {
  178. // 与房间尝试连接中
  179. console.log("与房间尝试连接中");
  180. }
  181. });
  182. // 房间用户变化通知
  183. this.engine.on("roomUserUpdate", (roomID, updateType, userList) => {
  184. console.log(updateType)
  185. console.log(userList)
  186. console.log("有其他用户进出房间");
  187. if (updateType == ZegoUpdateType.Add) {
  188. for (let user of userList) {
  189. for (let stream of this.allStreamList) {
  190. if (user.userID == stream.user.userID) {
  191. user.streamID = stream.streamID;
  192. }
  193. }
  194. this.allUserViewObjectList.push(user);
  195. }
  196. } else if (updateType == ZegoUpdateType.Delete) {
  197. this.allUserViewObjectList = this.allUserViewObjectList.filter((object) => {
  198. for (let user of userList) {
  199. if (user.userID == object.userID) {
  200. return false;
  201. }
  202. }
  203. return true;
  204. });
  205. }
  206. if (updateType == "1") {
  207. // 有其他用户退出房间
  208. this.offVideo()
  209. }
  210. });
  211. // 房间内其他用户推的流变化通知
  212. this.engine.on("roomStreamUpdate", (roomID, updateType, streamList) => {
  213. // if (updateType == "0") {
  214. // // 流新增,开始拉流
  215. // this.tensile(streamList[0].streamID)
  216. // } else if (updateType == "1") {
  217. // // 流删除,停止拉流
  218. // this.notTensile(streamList[0].streamID);
  219. // }
  220. console.log('---------updateType-----------', updateType)
  221. console.log('---------streamList-----------', streamList)
  222. console.log('---------ZegoUpdateType-----------', ZegoUpdateType)
  223. console.log('---------ZegoUpdateType.Add-----------', ZegoUpdateType.Add)
  224. console.log('---------ZegoUpdateType.Delete-----------', ZegoUpdateType.Delete)
  225. console.log('---------allUserViewObjectList-----------', this.allUserViewObjectList)
  226. if (updateType == ZegoUpdateType.Add) {
  227. for (let object of this.allUserViewObjectList) {
  228. for (let stream of streamList) {
  229. if (object.userID == stream.user.userID) {
  230. object.streamID = stream.streamID;
  231. }
  232. }
  233. }
  234. this.allStreamList = this.allStreamList.concat(streamList);
  235. } else if (updateType == ZegoUpdateType.Delete) {
  236. for (let object of this.allUserViewObjectList) {
  237. for (let stream of streamList) {
  238. if (object.streamID == stream.streamID) {
  239. object.streamID = undefined;
  240. }
  241. }
  242. }
  243. this.allStreamList = this.allStreamList.filter((object) => {
  244. for (let stream of streamList) {
  245. if (object.streamID == stream.streamID) {
  246. return false;
  247. }
  248. }
  249. return true;
  250. });
  251. }
  252. });
  253. // 房间内在线用户数量回调
  254. // this.engine.on("roomOnlineUserCountUpdate", (roomID, count) => {
  255. // // console.log(roomID)
  256. // console.log(count)
  257. // console.log('>?>>>>>>>>>>>>>>>>>>>>>>>在线用户有变化')
  258. // });
  259. // 拉流质量回调
  260. this.engine.on("playerQualityUpdate", (streamID, quality) => {
  261. console.log('拉流质量回调')
  262. });
  263. // 拉流后的事件回调
  264. this.engine.on("playerStateUpdate", (streamID, state, errorCode, extendedData) => {
  265. console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' + '拉流后的事件触发了')
  266. })
  267. },
  268. // 拉流
  269. // tensile(roomId) {
  270. // console.log('拉流进入了啊')
  271. // console.log(roomId)
  272. // this.serveViewModeIndex = roomId
  273. // console.log(this.serveViewModeIndex)
  274. // this.engine.startPlayingStream(roomId);
  275. // },
  276. tensile(item) {
  277. console.log(this.allStreamList)
  278. console.log(item)
  279. console.log(this.allUserViewObjectList)
  280. this.engine.startPlayingStream(item.streamID)
  281. },
  282. // 停止拉流
  283. notTensile(roomId) {
  284. // console.log(roomId)
  285. this.serveViewModeIndex = roomId
  286. // console.log(this.serveViewModeIndex)
  287. this.engine.stopPlayingStream(roomId)
  288. },
  289. // 推流 - 展示视图 - 本地视频流
  290. onPublish() {
  291. // 设置视频配置
  292. // ZegoExpressEngine.instance().setVideoConfig(this.videoObj.room_id);
  293. if (this.isPublishingStream) {
  294. // 停止推流
  295. this.engine.stopPreview();
  296. this.engine.stopPublishingStream();
  297. } else {
  298. console.log('开始推流了啊', this.isPublishingStream)
  299. // 开始推流
  300. this.engine.startPreview();
  301. this.engine.startPublishingStream(this.videoObj.room_id);
  302. this.videoShow = true
  303. }
  304. this.isPublishingStream = !this.isPublishingStream;
  305. },
  306. // 挂断退出
  307. offVideo() {
  308. console.log('进入了啊')
  309. this.engine.logoutRoom(this.videoObj.room_id)
  310. this.$refs.uToast.show({
  311. type: 'default',
  312. message: '结束视频通话!',
  313. })
  314. setTimeout(() => {
  315. uni.navigateBack({
  316. delta: 1
  317. })
  318. }, 1500)
  319. // uni.redirectTo({
  320. // url: "/pages/response/index"
  321. // })
  322. },
  323. },
  324. async onLoad(optinos) {
  325. var that = this
  326. that.videoObj = JSON.parse(optinos.videoObj)
  327. // console.log(that.videoObj)
  328. // 即构 - 视频通话
  329. // 获取系统信息同步接口
  330. if (uni.getSystemInfoSync().platform === 'android') {
  331. await permision.requestAndroidPermission('android.permission.RECORD_AUDIO');
  332. await permision.requestAndroidPermission('android.permission.CAMERA');
  333. }
  334. await this.setup()
  335. this.onPublish(); // 推流
  336. uni.getSystemInfo({ //异步获取。
  337. success(res) {
  338. // that.phoneHeight = res.windowHeight - 13; //窗口高度
  339. that.phoneHeight = res.windowHeight; //窗口高度
  340. }
  341. });
  342. },
  343. // 监听页面返回
  344. onBackPress() {
  345. console.log('返回了!')
  346. },
  347. }
  348. </script>
  349. <style lang="scss">
  350. .video_box {
  351. background: #000;
  352. .video_you {
  353. border: 1px solid;
  354. background: #fff;
  355. width: 150px;
  356. height: 200px;
  357. position: absolute;
  358. right: 10px;
  359. top: 10px;
  360. overflow: hidden;
  361. }
  362. .video_me {
  363. z-index: 100;
  364. background: #000;
  365. position: relative;
  366. right: 0;
  367. top: 0;
  368. }
  369. .video_btn {
  370. position: absolute;
  371. bottom: 35rpx;
  372. left: 0;
  373. width: 750rpx;
  374. .video_imgList {
  375. display: flex;
  376. flex-direction: row;
  377. justify-content: space-around;
  378. // 翻转
  379. .video_imgList1 {
  380. .video_view {
  381. background: rgba(0, 0, 0, .5);
  382. border-radius: 50px;
  383. width: 50px;
  384. height: 50px;
  385. margin: 0 auto;
  386. .video_view_img {
  387. height: 25px;
  388. width: 25px;
  389. margin: 24rpx 0 0 25rpx;
  390. }
  391. }
  392. .video_view_txt {
  393. color: #fff;
  394. font-size: 14px;
  395. line-height: 30px;
  396. text-align: center;
  397. }
  398. }
  399. // 语音通话
  400. .video_imgList2 {
  401. .video_view {
  402. background: rgba(0, 0, 0, .5);
  403. border-radius: 50px;
  404. width: 50px;
  405. height: 50px;
  406. margin: 0 0 0 30rpx;
  407. .video_view_img {
  408. height: 25px;
  409. width: 25px;
  410. margin: 24rpx 0 0 25rpx;
  411. }
  412. }
  413. .video_view_txt {
  414. color: #fff;
  415. font-size: 14px;
  416. line-height: 30px;
  417. text-align: center;
  418. }
  419. }
  420. }
  421. }
  422. }
  423. </style>