当前位置:   article > 正文

uniapp 安卓UVC摄像头推流原生插件_uniapp 网络摄像头插件

uniapp 网络摄像头插件

插件介绍

安卓UVC摄像头推流原生插件,支持RTMP推流服务,支持拍照,录像,停止预览,开启预览,RTMP推流

插件地址

安卓UVC摄像头推流原生插件 - DCloud 插件市场

使用文档

uniapp 安卓UVC摄像头推流插件

超级福利

uniapp leven系列插件购买超级福利

用法

在需要使用插件的页面加载以下代码

  1. <leven-uvcCameraPusher ref="refLevenUsbCamera" style="flex:1; height: 300px;" @onDestroy="onDestroy" @onAttach="onAttach" @onDettach="onDettach"
  2. @onConnect="onConnect" @onDisconnect="onDisconnect" @onCancel="onCancel" @onError="onError" @onPusherError="onPusherError"
  3. @onPusherConnect="onPusherConnect" @onPusherStop="onPusherStop" @onRecording="onRecording">
  4. </leven-uvcCameraPusher>

页面内容

  1. <template>
  2. <view>
  3. <uni-card title="安卓UVC摄像头推流原生插件">
  4. <leven-uvcCameraPusher ref="refLevenUsbCamera" style="flex:1; height: 300px;" @onDestroy="onDestroy" @onAttach="onAttach" @onDettach="onDettach"
  5. @onConnect="onConnect" @onDisconnect="onDisconnect" @onCancel="onCancel" @onError="onError" @onPusherError="onPusherError"
  6. @onPusherConnect="onPusherConnect" @onPusherStop="onPusherStop" @onRecording="onRecording">
  7. </leven-uvcCameraPusher>
  8. <!-- <button type="primary" @click="changeCamera">切换摄像头</button> -->
  9. <button type="primary" @click="capture">开始拍照</button>
  10. <button type="primary" @click="stopPreview">关闭预览</button>
  11. <button type="primary" @click="openPreview">开启预览</button>
  12. <button type="primary" @click="startRecord">开始录制</button>
  13. <button type="primary" @click="stopRecord">结束录制</button>
  14. <button type="primary" @click="setSize">设置分辨率</button>
  15. <button type="primary" @click="getSupportedSize">获取支持的分辨率</button>
  16. <button type="primary" @click="close">关闭摄像头</button>
  17. <button type="primary" @click="open">开启摄像头</button>
  18. <button type="primary" @click="logStr = ''">清空日志</button>
  19. </uni-card>
  20. <uni-card title="推流功能">
  21. <uni-section title="推流地址,RTMP协议">
  22. <uni-easyinput v-model="pusherForm.url" placeholder="请输入推流地址"></uni-easyinput>
  23. </uni-section>
  24. <uni-section title="直播地址">
  25. <uni-easyinput v-model="pusherForm.playUrl" placeholder="请输入直播地址"></uni-easyinput>
  26. </uni-section>
  27. <uni-data-checkbox v-model="pusherForm.type" :localdata="pusherType"></uni-data-checkbox>
  28. <button type="primary" @click="startPusher">开启推流</button>
  29. <button type="primary" @click="stopPusher">关闭推流</button>
  30. </uni-card>
  31. <uni-card title="播放流" v-if="isPusher">
  32. <video style="flex:1; height: 300px;" :src="pusherForm.playUrl" :is-live="true" :autoplay="true"></video>
  33. </uni-card>
  34. <uni-card class="uni-card-box" title="日志">
  35. <view><text style="font-size: 14px; flex-wrap: wrap;">{{logStr}}</text></view>
  36. </uni-card>
  37. <!-- 弹窗 -->
  38. <uni-popup ref="refSelectSizePop">
  39. <view style="width: 300px;">
  40. <uni-card title="请选择分辨率">
  41. <button v-for="(item, index) in sizeList" :key="index" type="primary" @click="selectSizeItem(item)">{{item.width}}×{{item.height}}</button>
  42. </uni-card>
  43. </view>
  44. </uni-popup>
  45. </view>
  46. </template>
  47. <script>
  48. export default {
  49. data() {
  50. return {
  51. // 摄像头列表
  52. deviceList: [],
  53. // 当前预览的摄像头索引
  54. previewIndex: 0,
  55. // 当前分辨率的索引
  56. sizeIndex: 0,
  57. logStr: "",
  58. // 推流表单
  59. pusherForm: {
  60. //推流类型,1.数据推流,2.文件推流
  61. type: 1,
  62. // 推流地址
  63. url: "rtmp://195964.push.tlivecloud.com/live/leven?txSecret=e387f3e5f5226247727ba0c2e7cde355&txTime=65E2D090",
  64. // 直播地址
  65. playUrl: "rtmp://txplayer.yeyuboke.com/live/leven"
  66. },
  67. //推流类型
  68. pusherType: [{
  69. text: "数据推流",
  70. value: 1
  71. }, {
  72. text: "文件推流",
  73. value: 2
  74. }],
  75. // 分辨率列表
  76. sizeList: [],
  77. // 是否开始推流
  78. isPusher: false
  79. }
  80. },
  81. methods: {
  82. // 切换摄像头
  83. changeCamera() {
  84. if (this.deviceList.length == 0) {
  85. this.showToast("设备列表为空");
  86. return false;
  87. }
  88. this.previewIndex++;
  89. let index = this.previewIndex % this.deviceList.length;
  90. if (this.$refs.refLevenUsbCamera) {
  91. this.$refs.refLevenUsbCamera.changeCamera({
  92. deviceName: this.deviceList[index].deviceName
  93. }, res => {
  94. // console.log(res)
  95. this.writeLog(JSON.stringify(res))
  96. })
  97. }
  98. },
  99. // 拍照
  100. capture() {
  101. if (this.$refs.refLevenUsbCamera) {
  102. this.$refs.refLevenUsbCamera.capture(res => {
  103. // console.log(res)
  104. this.writeLog(JSON.stringify(res))
  105. })
  106. }
  107. },
  108. // 关闭预览
  109. stopPreview() {
  110. if (this.$refs.refLevenUsbCamera) {
  111. this.$refs.refLevenUsbCamera.stopPreview(res => {
  112. // console.log(res)
  113. this.writeLog(JSON.stringify(res))
  114. })
  115. }
  116. },
  117. // 开启预览
  118. openPreview() {
  119. if (this.$refs.refLevenUsbCamera) {
  120. this.$refs.refLevenUsbCamera.openPreview(res => {
  121. // console.log(res)
  122. this.writeLog(JSON.stringify(res))
  123. })
  124. }
  125. },
  126. // 开始录制
  127. startRecord() {
  128. if (this.$refs.refLevenUsbCamera) {
  129. this.$refs.refLevenUsbCamera.startRecord(res => {
  130. // console.log(res)
  131. this.writeLog(JSON.stringify(res))
  132. })
  133. }
  134. },
  135. // 结束录制
  136. stopRecord() {
  137. if (this.$refs.refLevenUsbCamera) {
  138. this.$refs.refLevenUsbCamera.stopRecord(res => {
  139. // console.log(res)
  140. this.writeLog(JSON.stringify(res))
  141. })
  142. }
  143. },
  144. // 设置分辨率
  145. setSize() {
  146. if (this.$refs.refLevenUsbCamera) {
  147. this.$refs.refLevenUsbCamera.getSupportedSize(res => {
  148. this.sizeList = res.data.list;
  149. if (this.$refs.refSelectSizePop) {
  150. this.$refs.refSelectSizePop.open();
  151. }
  152. })
  153. }
  154. },
  155. // 选择结果
  156. selectSizeItem(item) {
  157. this.$refs.refLevenUsbCamera.setSize({
  158. width: item.width,
  159. height: item.height
  160. }, res => {
  161. // console.log(res)
  162. this.writeLog(JSON.stringify(res))
  163. if (this.$refs.refSelectSizePop) {
  164. this.$refs.refSelectSizePop.close();
  165. }
  166. })
  167. },
  168. // 获取支持的分辨率
  169. getSupportedSize() {
  170. if (this.$refs.refLevenUsbCamera) {
  171. this.$refs.refLevenUsbCamera.getSupportedSize(res => {
  172. // console.log(res)
  173. this.writeLog(JSON.stringify(res))
  174. })
  175. }
  176. },
  177. // 关闭摄像头
  178. close() {
  179. if (this.$refs.refLevenUsbCamera) {
  180. this.$refs.refLevenUsbCamera.close(res => {
  181. // console.log(res)
  182. this.writeLog(JSON.stringify(res))
  183. })
  184. }
  185. },
  186. // 开启摄像头
  187. open() {
  188. if (this.$refs.refLevenUsbCamera) {
  189. this.$refs.refLevenUsbCamera.open(res => {
  190. // console.log(res)
  191. this.writeLog(JSON.stringify(res))
  192. })
  193. }
  194. },
  195. // 开启推流
  196. startPusher() {
  197. try {
  198. if (uni.$lv.validate.empty(this.pusherForm.url)) {
  199. throw new Error("请输入推流地址");
  200. }
  201. if (this.$refs.refLevenUsbCamera) {
  202. this.$refs.refLevenUsbCamera.startPusher(this.pusherForm)
  203. }
  204. } catch (e) {
  205. uni.$lv.func.toast(e.message);
  206. }
  207. },
  208. // 关闭推流
  209. stopPusher() {
  210. if (this.$refs.refLevenUsbCamera) {
  211. this.$refs.refLevenUsbCamera.stopPusher()
  212. }
  213. },
  214. // 组件卸载
  215. onDestroy(e) {
  216. this.writeLog("onDestroy:" + JSON.stringify(e))
  217. },
  218. // 组件加载完成
  219. onAttach(e) {
  220. let detail = e.detail;
  221. this.deviceList = detail.deviceList || [];
  222. this.writeLog("onAttach:" + JSON.stringify(e))
  223. },
  224. // 设备移除
  225. onDettach(e) {
  226. let detail = e.detail;
  227. this.deviceList = detail.deviceList || [];
  228. this.writeLog("onDettach:" + JSON.stringify(e))
  229. },
  230. // 摄像机连接成功
  231. onConnect(e) {
  232. let detail = e.detail;
  233. this.deviceList = detail.deviceList || [];
  234. this.writeLog("onConnect:" + JSON.stringify(e))
  235. },
  236. // 断开连接
  237. onDisconnect(e) {
  238. let detail = e.detail;
  239. this.deviceList = detail.deviceList || [];
  240. this.writeLog("onDisconnect:" + JSON.stringify(e))
  241. },
  242. // 取消连接
  243. onCancel(e) {
  244. let detail = e.detail;
  245. this.deviceList = detail.deviceList || [];
  246. this.writeLog("onCancel:" + JSON.stringify(e))
  247. },
  248. // 系统错误
  249. onError(e) {
  250. this.writeLog("onError:" + JSON.stringify(e))
  251. },
  252. // 推流错误
  253. onPusherError(e) {
  254. this.isPusher = false;
  255. this.writeLog("onPusherError:" + JSON.stringify(e))
  256. },
  257. // 推流连接成功
  258. onPusherConnect(e) {
  259. let detail = e.detail;
  260. if (detail.status == 3) {
  261. //推流中
  262. this.isPusher = true;
  263. }
  264. this.writeLog("onPusherConnect:" + JSON.stringify(e))
  265. },
  266. // 停止推流
  267. onPusherStop(e) {
  268. this.isPusher = false;
  269. this.writeLog("onPusherStop:" + JSON.stringify(e))
  270. },
  271. //录像结果
  272. onRecording(e) {
  273. this.writeLog("onRecording:" + JSON.stringify(e))
  274. },
  275. // 提示信息
  276. showToast(content) {
  277. uni.showToast({
  278. icon: "none",
  279. title: content
  280. })
  281. },
  282. // 写日志
  283. writeLog(str) {
  284. console.log(str);
  285. let logStr = uni.$lv.date.format(null, "yyyy-mm-dd hh:MM:ss") + " " + str + "\n";
  286. this.logStr = logStr + this.logStr;
  287. }
  288. }
  289. }
  290. </script>
  291. <style>
  292. </style>

插件方法

  1. 拍照
  2. 关闭预览
  3. 开启预览
  4. 开始录制
  5. 结束录制
  6. 设置分辨率
  7. 获取支持的分辨率
  8. 关闭摄像头
  9. 开启摄像头
  10. 开启推流
  11. 关闭推流

插件事件

  • 组件卸载
  • 插入事件
  • 拔出事件
  • 连接事件
  • 断开连接
  • 取消连接
  • 系统错误
  • 推流错误
  • 推流状态
  • 停止推流
  • 录像结果

联系作者

 购买插件前请先试用,试用通过再购买。在试用中如果遇到任何问题,可与作者联系,QQ:334106817,将全力协助你使用本插件

图片预览

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/1017184
推荐阅读
相关标签
  

闽ICP备14008679号