简体   繁体   English

用 vuejs 项目实现 hls.js 库

[英]implement hls.js library with vuejs project

I need some help trying to figure out how to use the hls.js library in vuejs as it has no specific documentation of how to implement it with vue.我需要一些帮助来弄清楚如何在 vuejs 中使用hls.js库,因为它没有关于如何使用 vue 实现它的具体文档。 The situation here is that I have to fetch the m3u8 from an api I'm able to make it work from a basic html with the tag and cdn but when I try to implement it with vuejs it doesn't work, any help is appreciated.这里的情况是我必须从 api 获取 m3u8 我能够使用带有标签和 cdn 的基本 html 使其工作,但是当我尝试使用 vuejs 实现它时它不起作用,感谢任何帮助. I've tried 2 implementations and got the same error each time.我尝试了 2 次实现,每次都得到相同的错误。 These are what I've got so far:这些是我到目前为止所得到的:

First try : use cdn and include in it component第一次尝试:使用 cdn 并在其中包含组件

export default {
  head() {
     return {
       script: [
         {
           src: 'https://cdn.jsdelivr.net/npm/hls.js@latest'
         }
       ],
     }
   }}

with function in created()在 created() 中使用 function

    checkHLS(){
      console.log('in');
      if (Hls.isSupported()) {
        console.log('working')
      }
    },

Second Try install the package using npm第二次尝试使用 npm 安装 package

npm install --save hls.js

and i got this error我得到了这个错误

                                  Hls is not defined
An error occurred while rendering the page. Check developer tools console for details.

Install hls.js via npm: npm i --save hls.js@latest通过 npm 安装 hls.js: npm i --save hls.js@latest

Here is how you can use it in a component:以下是如何在组件中使用它:

<template>
  <video ref="video" width="100%" height="640" controls></video>
</template>

<script>
import Hls from 'hls.js';

export default {
  mounted() {
    let hls = new Hls();
    let stream = "http://demo.unified-streaming.com/video/tears-of-steel/tears-of-steel.ism/.m3u8";
    let video = this.$refs["video"];
    hls.loadSource(stream);
    hls.attachMedia(video);
    hls.on(Hls.Events.MANIFEST_PARSED, function () {
      video.play();
    });
  },
};
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM