IT评测·应用市场-qidao123.com

标题: uniapp H5 对接 声网,截图 [打印本页]

作者: 南飓风    时间: 2025-1-2 19:37
标题: uniapp H5 对接 声网,截图
安装依靠


创建容器

  1. <template>
  2.         <view class="videoValue " id="videoValue">
  3.                 <u-toast ref="uToast"></u-toast>
  4.                 <view @click="screenshot()">
  5.                         截图
  6.                 </view>
  7.         </view>
  8. </template>
复制代码
容器样式

   Hbuilder X 默认支持 less 语法
  1. <style lang="less" scoped>
  2.         .videoValue {
  3.                 width: 100%;
  4.                 height: 100%;
  5.         }
  6. </style>
复制代码
javascript代码


  1. <script>
  2.         import AgoraRTC from 'agora-rtc-sdk-ng';
  3.         import {
  4.                 wuRenJiApi
  5.         } from '@/api/wu-ren-ji.js'
  6.         // 机场直播声网
  7.         const client = AgoraRTC.createClient({
  8.                 codec: 'vp9',// codec 设置支持 "vp8" (VP8)、"h264"(H.264) 具体差别自行研究
  9.                 mode: 'live', // "rtc"(通信场景) 和 "live"(直播场景)
  10.                 mediaType: 'video',
  11.         });
  12.        
  13.         let userClient;
  14.         // 当远端用户成功发布音视频轨道之后,SDK 会触发 user-published 事件。
  15.         // 这个事件携带两个参数:远端用户对象 (user) 和远端发布的媒体类型 (mediaType)。
  16.         // 此时,你可以调用 AgoraRTCClient.subscribe 发起订阅。
  17.         client.on('user-published', async (user, mediaType) => {
  18.                 await client.subscribe(user, mediaType)
  19.                 if (mediaType === 'video') {
  20.                         await user.videoTrack.play('videoValue');
  21.                         userClient = user.videoTrack // 获取当前渲染的视频帧数据
  22.                 }
  23.         });
  24.         export default {
  25.                 data() {
  26.                         return {
  27.                                 snObj: null,
  28.                                 videoUser: null,
  29.                                 shootErrorCount: 0, // 拍摄错误次数
  30.                                 screenShotCount: 30,
  31.                                 screenShotTimer: null
  32.                         }
  33.                 },
  34.                 mounted() {
  35.                         let snObj = uni.getStorageSync('snObj')
  36.                         // snObj 主要包含1个关键参数 deviceSn
  37.                         /**
  38.                                 {
  39.                                     "createName": null,
  40.                                     "createDatetime": "2024-10-30 10:57:36",
  41.                                     "updateName": null,
  42.                                     "deviceSn": "7CTxxxxxx1",
  43.                                 }
  44.                         */
  45.                        
  46.                         this.snObj = snObj
  47.                         this.playVideo()
  48.                 },
  49.                 methods: {
  50.                         playVideo() {
  51.                                 this.openJiChangZhiBo()
  52.                         },
  53.                         // 机场
  54.                         async openJiChangZhiBo() {
  55.                                 let option = uni.getStorageSync('option')
  56.                                 // option 包含3个关键参数 deviceSn
  57.                         /**
  58.                                 {
  59.                                     "token":"++/a1oP903NnBxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxACwS3Nn",
  60.                                     "appId": "fcb7ca994xxxxxxxxxxxxxxx08b",
  61.                                     "channel": "7Cxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-0"
  62.                                 }
  63.                         */
  64.                                 await this.startDockerLive(1) // 连接设备
  65.                                 if (option.appId && option.channel && option.token) {
  66.                                         // 这一步可有可无,(离开频道),一般用于在切换页面的时候,
  67.                                         // 也就是路由改变离开频道不会导致视频一直在播放,从而减少消耗费用
  68.                                         await this.leave();
  69.                                         // // 获得token渲染直播画面 连接声网实例视频
  70.                                         const uid = await client.join(
  71.                                                 option.appId,
  72.                                                 option.channel,
  73.                                                 option.token,
  74.                                                 null // 设置null 或者不设置 自动分配数字 UID
  75.                                         );
  76.                                 } else {
  77.                                         console.error('option', option);
  78.                                 }
  79.                         },
  80.                         // 控制机场直播 0断开 1连接
  81.                         async startDockerLive(op = 0) {
  82.                                 try {
  83.                                         const data = await api.startLive({
  84.                                                 dockSN: this.snObj.deviceSn,
  85.                                                 op: op,
  86.                                         }).catch(err => {
  87.                                                 throw new Error('控制机场直播 断开/连接抛异常' + err)
  88.                                         });
  89.                                         if (op === 1) {
  90.                                                 return data;
  91.                                         }
  92.                                 } catch (error) {
  93.                                         console.error(error);
  94.                                 }
  95.                         },
  96.                         async level() {
  97.                                 await client.leave();
  98.                         },
  99.                         // 截图
  100.                         async screenshot() {
  101.                                 try {
  102.                                         if (this.shootErrorCount >= 5) {
  103.                                         // uview框架
  104.                                                 this.$refs.uToast.show({
  105.                                                         message: `拍摄错误超过5次,请等待${this.screenShotCount}s后重试`
  106.                                                 })
  107.                                                 this.createInteVal()
  108.                                                 return
  109.                                         }
  110.                                         this.$refs.uToast.show({
  111.                                                 loading: true,
  112.                                                 message: '拍摄中...',
  113.                                                 type: "loading",
  114.                                                 duration: 1000 * 10
  115.                                         })
  116.                                         const resImg = await userClient.getCurrentFrameData()
  117.                                         const blobData = await ImageDataToBlob(resImg) // 自行写个js文件吧代码粘过去引入
  118.                                         // 创建一个 FileReader 对象
  119.                                         const reader = new FileReader();
  120.                                         // 定义读取完成后的回调
  121.                                         reader.onloadend = async () => {
  122.                                                 // 获取转换后的 Base64 编码数据
  123.                                                 // 这里已经是base64了,在浏览器可以直接打开看,
  124.                                                 // 但是因为url限制,无法看全,可以直接存到服务器,
  125.                                                 // 然后范围服务器的图片地址,或者是转File文件流传输到服务器
  126.                                                 const base64String = reader.result;
  127.                                                 let fileUrl = await this.uploadFile(base64String) // 这里我是把文件上传到服务器
  128.                                                 const data = await api.screenshot({
  129.                                                         fileName: fileUrl,
  130.                                                         deviceSn: this.snObj.deviceSn,
  131.                                                 }).catch(err => {
  132.                                                         this.shootErrorCount += 1
  133.                                                         this.$refs.uToast.show({
  134.                                                                 message: "拍摄上传抛异常,原因:" + JSON.stringify(err),
  135.                                                                 duration: 1000 * 3
  136.                                                         })
  137.                                                         throw new Error('上传抛异常' + JSON.stringify(err));
  138.                                                 })
  139.                                                 if (data.code === 200) {
  140.                                                         this.$refs.uToast.show({
  141.                                                                 message: "拍摄成功,请前往我的相册查看",
  142.                                                                 position: "center",
  143.                                                                 duration: 1000 * 1.5
  144.                                                         })
  145.                                                 } else {
  146.                                                         this.$refs.uToast.show({
  147.                                                                 message: "拍摄失败,原因:" + JSON.stringify(data.msg),
  148.                                                                 duration: 1000 * 3
  149.                                                         })
  150.                                                         this.shootErrorCount += 1
  151.                                                 }
  152.                                         };
  153.                                         reader.readAsDataURL(blobData)
  154.                                 } catch (e) {
  155.                                         this.shootErrorCount += 1
  156.                                         this.$refs.uToast.show({
  157.                                                 message: "拍摄失败,原因:" + JSON.stringify(data.msg),
  158.                                                 duration: 1000 * 3
  159.                                         })
  160.                                         console.error(e)
  161.                                 }
  162.                         },
  163.                         clearInterval() {
  164.                                 if (this.screenShotTimer) {
  165.                                         return
  166.                                 }
  167.                                 this.screenShotCount -= 1
  168.                                 this.screenShotTimer = setInterval(() => {
  169.                                         if (this.screenShotCount > 0) {
  170.                                                 this.screenShotCount -= 1
  171.                                         } else {
  172.                                                 clearInterval(this.screenShotTimer)
  173.                                                 this.screenShotTimer = null
  174.                                                 this.shootErrorCount = 0
  175.                                         }
  176.                                 }, 1000)
  177.                         }
  178.                 },
  179.         }
  180. </script>
复制代码
ImageDataToBlob 方法

  1. // Uint8ClampedArray 转blob
  2. export function ImageDataToBlob(imageData) {
  3.         let w = imageData.width;
  4.         let h = imageData.height;
  5.         let canvas = document.createElement('canvas');
  6.         canvas.width = w;
  7.         canvas.height = h;
  8.         let ctx = canvas.getContext('2d');
  9.         ctx.putImageData(imageData, 0, 0);
  10.         return new Promise((resolve) => {
  11.                 canvas.toBlob(resolve);
  12.         });
  13. };
复制代码
控制控制台LOG输出

   AgoraRTC.setLogLevel(Number)
SDK 日记输出级别。按照输出日记最全到最少分列:
  
   创建实例之前设置log
  1. <script>
  2.                 AgoraRTC.setLogLevel(2)
  3.                 // 航线直播声网
  4.                 const client = AgoraRTC.createClient({
  5.                         codec: 'vp9',
  6.                         mode: 'live',
  7.                 });
  8. </script>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4