当前位置:   article > 正文

vue使用hls.js播放m3u8视频流_vue hls.js

vue hls.js

vue使用hls.js播放m3u8视频流

npm下载hls.js

npm i hls.js@1.0.9-0.canary.7938
  • 1

导入Hls

import Hls from "hls.js"
  • 1

播放视频流

mounted() {
	const that= this
	this.hls = new Hls()
	const video = this.$refs.video_player
	this.hls.attachMedia(video)
	this.hls.on(Hls.Events.MEDIA_ATTACHED, () => {
		that.hls.loadSource(that.videoUrl)
	})
	this.hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {
		video.play()
	});
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

播放组件的完整代码

<template>
	<div class="container">
		<video
				autoplay muted controls
				width="500" height="400"
				ref="video_player"></video>
	</div>
</template>

<script lang="ts">
	import {defineComponent, ref} from 'vue';
	import Hls from "hls.js";

	/**
	 * 组件名称:
	 * 组件描述:
	 */
	export default defineComponent({
		name: 'HLSVideoPlayer',
		components: {},
		props: {
			videoUrl: {
				type: String,
				default: 'http://xxx.m3u8
			}
		},
		setup() {
			return {
				hls: ref(undefined),
			};
		},
		watch: {
			videoUrl() {
				this.hls.loadSource(this.videoUrl)
			}
		},
		mounted() {
			const that= this
			this.hls = new Hls()
			const video = this.$refs.video_player
			this.hls.attachMedia(video)
			this.hls.on(Hls.Events.MEDIA_ATTACHED, () => {
				that.hls.loadSource(that.videoUrl)
			})
			this.hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {
				video.play()
			});
		},

	});
</script>
<style scoped lang="scss">
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/92447
推荐阅读
相关标签
  

闽ICP备14008679号