deviceLine.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <view>
  3. <ujp-navbar title="设备详情"></ujp-navbar>
  4. <view style=" padding: 10px 16px;
  5. font-size: 16px;" >{{detail.deviceName}}<span style="float: right;"><span class="boot" :class="{
  6. boot2:detail.status
  7. }"></span>{{detail.status?'在线':'离线'}}</span></view>
  8. <img src="@/assets/img/gkke02.png" v-if="!detail.status" style="width: 100%;height: 234px;" />
  9. <view class="video-wrap" style="width: 100%;height: 234px;"
  10. v-html="html"
  11. v-show="detail.status" ref="videoWrapHls">
  12. </view>
  13. <!-- -->
  14. <view style=" text-align: center;" v-if="endTime">
  15. <u-button
  16. type="primary" size="medium"
  17. @click="getInfo" >继续播放</u-button>
  18. </view>
  19. <view style="padding: 10px ;" v-if="endTime">
  20. 单次播放时间为{{expireTime/60}}分钟;<br/>
  21. 过期后可点击“继续播放”
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import * as API from '@/apis/finance.js'
  27. //import Hls from '@/apis/hls.js';
  28. // import videojs from '@/apis/video.js'
  29. import Hls from 'hls.js'
  30. export default {
  31. data() {
  32. return {
  33. html:'<video id="myVideo" autoplay controls style="width: 100%; height: 234px;"><source id="myVideoSource" type="application/x-mpegURL" /></video>',
  34. subForm: {},
  35. liveAddressUrl: "",
  36. detail: {},
  37. hls: null,
  38. player: null,
  39. expireTime:10*60,
  40. endTime:0,
  41. }
  42. },
  43. onLoad(op) {
  44. this.subForm.id=op.id;
  45. },
  46. onReady() {
  47. //uniapp 渲染有问题, 自主渲染
  48. if(!this.player){
  49. // var video = document.createElement('video');
  50. // video.id = 'myVideo';
  51. // video.style="width: 100%;height: 234px;"
  52. // this.$refs.videoWrapHls.$el.appendChild(video);
  53. // this.player = video;
  54. var video=document.getElementById("myVideo")
  55. this.player = video;
  56. }
  57. this.getInfo();
  58. },
  59. methods: {
  60. getInfo(){
  61. uni.showLoading({
  62. title: "加载中",
  63. mask: true,
  64. })
  65. //秒
  66. this.subForm.expireTime=this.expireTime
  67. API.cameraDetail(this.subForm).then((res) => {
  68. this.detail=res.data.cameraInfo;
  69. this.endTime=0;
  70. if(this.detail.status&&res.data.liveJson){
  71. var myVideoSource =document.getElementById("myVideoSource")
  72. myVideoSource.src=this.liveAddressUrl;
  73. this.liveAddressUrl = res.data.liveJson.url
  74. var endTime=new Date(res.data.liveJson.expireTime).getTime()
  75. var nowTime=new Date().getTime()
  76. setTimeout(()=>{
  77. this.endTime=1;
  78. },endTime-nowTime)
  79. this.$nextTick(()=>{
  80. this.play();
  81. })
  82. }
  83. uni.hideLoading()
  84. }).catch(error => {
  85. uni.showToast({
  86. title: error
  87. })
  88. })
  89. },
  90. handlePlay() {
  91. },
  92. play() {
  93. var video= this.player;
  94. this.liveAddressUrl = this.liveAddressUrl.replace("http:", "https:");
  95. var videoSrc = this.liveAddressUrl;
  96. if (Hls.isSupported()) {
  97. var hls = new Hls();
  98. hls.loadSource(videoSrc);
  99. hls.attachMedia(video);
  100. hls.on(Hls.Events.MANIFEST_PARSED, () => {
  101. video.play();
  102. });
  103. this.hls = hls;
  104. } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
  105. video.src = videoSrc;
  106. video.addEventListener('loadedmetadata', () => {
  107. video.play();
  108. });
  109. }
  110. //监听开始播放事件
  111. // this.player.addEventListener('play', this.handlePlay);
  112. }
  113. }
  114. }
  115. </script>
  116. <style scoped>
  117. .boot{
  118. display: inline-block;
  119. width: 12px;
  120. height: 12px;
  121. margin-left: 2px;
  122. background-color: rgba(255, 115, 0, 100);
  123. border-radius: 999px;
  124. }
  125. .boot2{
  126. background-color:rgba(0, 185, 98, 100)
  127. }
  128. </style>