经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » TypeScript » 查看文章
vue + typescript + video.js实现 流媒体播放 视频监控功能
来源:jb51  时间:2019/7/8 8:30:34  对本文有异议

视频才用流媒体,有后台实时返回数据, 要支持flash播放, 所以需安装对应的flash插件。当视频播放时,每间隔3秒向后台发送请求供检测心跳,表明在线收看状态,需要后台持续发送视频数据。

1. yarn add video.js videojs-flash

2. 创建videp.js声明文件 

 

3. 创建video_player.vue组件,供外部调用。源码如下

  1. <script lang="ts">
  2. import { Component, Emit, Prop, Vue } from 'vue-property-decorator';
  3.  
  4. import 'video.js/dist/video-js.css';
  5.  
  6. import _videojs from 'video.js';
  7. const videojs = (window as any).videojs || _videojs;
  8. import 'videojs-flash';
  9.  
  10.  
  11. @Component({
  12. name: 'video-player',
  13. })
  14. export default class VideoPlayer extends Vue {
  15. /* ------------------------ INPUT & OUTPUT ------------------------ */
  16. @Prop({ type: Object, default: () => {}}) private options!: object;
  17.  
  18. /* ------------------------ VUEX (vuex getter & vuex action) ------------------------ */
  19.  
  20. /* ------------------------ LIFECYCLE HOOKS (created & mounted & ...) ------------------------ */
  21. private mounted() {
  22. this.player = videojs(this.$refs.videoPlayer, this.options, function onPlayerReady() {
  23. // console.log('onPlayerReady');
  24. });
  25. }
  26.  
  27. private beforeDestroy() {
  28. if (this.player) {
  29. this.player.dispose();
  30. }
  31. }
  32. /* ------------------------ COMPONENT STATE (data & computed & model) ------------------------ */
  33. private player: any;
  34.  
  35. /* ------------------------ WATCH ------------------------ */
  36.  
  37. /* ------------------------ METHODS ------------------------ */
  38. }
  39.  
  40. </script>
  41.  
  42. <template>
  43. <div class="module_video_player">
  44. <video ref="videoPlayer" class="video-js" autoplay></video>
  45. </div>
  46. </template>
  47.  
  48. <style lang="stylus" scoped>
  49. @import '~@/assets/styles/var.styl';
  50.  
  51. .module_video_player
  52. position relative
  53. width 780px
  54.  
  55. </style>

4. 在需要使用的模块(如show_monitor.vue)调用。组件创建后,向后台发送轻轻获取rtmp视频播放地址,并更新videoOptions中的src。触发video_player的创建、挂载等。

  1. import VideoPlayer from '@/components/video_player.vue';
  2.  
  3. components: {
  4. VideoPlayer,
  5. }
  6.  
  7. private videoOptions = {
  8. techOrder: ['flash', 'html5'],
  9. sourceOrder: true,
  10. flash: {
  11. hls: { withCredentials: false },
  12. },
  13. html5: { hls: { withCredentials: false } },
  14. sources: [{
  15. type: 'rtmp/flv',
  16. src: '', // 'rtmp://live.hkstv.hk.lxdns.com/live/hks2', // 香港卫视,可使用此地址测试
  17. }],
  18. autoplay: true,
  19. controls: true,
  20. width: '778',
  21. height: '638',
  22. };
  23. <video-player :options="videoOptions" v-if="videoOptions.sources[0].src !== ''"></video-player>

5. 心跳检测

在show_monitor.vue创建时,新建定时器,每隔3秒向后台发送一个包含当前监控设备id的请求,告知后台此设备监控被调用播放。show_monitor.vue销毁时,清空定时器,后台将停止传输视频数据。

  1. private intervalFunc: any;
  2.  
  3. private created() {
  4. // ****
  5. this.intervalFunc = setInterval(() => {
  6. EquipmentService.monitor_continue_test(this.eqmtid);
  7. }, 3000);
  8. }
  9.  
  10. private destroyed() {
  11. clearInterval(this.intervalFunc);
  12. }

注: 可以再电脑安装VLC media player下载 , 播放获取到的rtmp路径,已检测数据获取是否成功

总结

以上所述是小编给大家介绍的vue + typescript + video.js实现 流媒体播放 视频监控功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号