vue3 使用Jessibuca播放器,实现视频流播放直播

这篇文章主要给大家介绍了关于vue3播放.flv、m3u8视频流(监控)的实现过程,包括视频流播放、视频截图、视频录制、等功能,支持国标H264/H265(FLV_EXT_ID_12标准),需要的朋友可以参考下

一、步骤介绍

1.下载插件所需的js文件 ‘decoder.js’、‘decoder.wasm’、‘jessibuca.js’、‘jessibuca.d.ts’

2.全局引入‘jessibuca.js’文件

3.创建Jessibuca.vue播放组件

4.引用组件

二、具体代码

官网地址:Jessibuca,从官网demo中下载所需要的文件

1.将下载的文件存放到vue项目的‘public’文件夹下

2.全局引用

   2.1 在项目根目录下找到index.html文件,在index.html文件中全局引用‘jessibuca.js’文件

注意:vue项目中public文件夹下的内容可以直接引用,不需要 ‘./’ 了 src="./jessibuca.js"。type="module"是问了vite打包是报错。

   2.2 jessibuca.d.ts’文件添加到typings文件夹下(typings文件夹是全局TS声明)

提示:ts项目会自动创建tsconfig.json文件,这个文件是ts 全局配置文件,只要 typings 文件夹和其中的 .d.ts 文件在 include 配置中指定的范围内,它们的类型定义就会被 TypeScript 识别并应用到项目的所有文件中。

3.创建Jessibuca.vue组件

   3.1 创建公共组件,在src/components文件夹下新建Jessibuca/index.vue文件

  3.2 Jessibuca.vue组件代码:

<template>
  <div class="w-full aspect-video">
    <div ref="playerRef"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from "vue";

const playerRef = ref<HTMLDivElement>();

// jessibuca player
const jessibucaPlayer = ref<Jessibuca>();

interface Props {
  /** 播放地址 */
  wsFlv?: string;
  wssFlv?: string;
  config: Partial<Jessibuca.Config>;
}

const props = withDefaults(defineProps<Props>(), {});

// 创建播放器
const createJessibucaPlayer = () => {
  jessibucaPlayer.value = new Jessibuca({
    container: playerRef.value!,
    ...props.config
  });
  jessibucaPlayer.value.play(props.wsFlv);
};

onMounted(() => {
  createJessibucaPlayer();
});

// 组件销毁前销毁播放器并停止视频流
onBeforeUnmount(() => {
  if (jessibucaPlayer.value) {
    jessibucaPlayer.value.destroy();
    jessibucaPlayer.value = null as unknown as undefined;
  }
});
</script>

<style scoped lang="scss"></style>

4.组件引用

   4.1 html部分

<!-- 视频流播放区 -->
<div class="videoBox-play">
      <JessibucaPlayer
        style="width: 100%; height: 230px"
        v-if="flvUrl1"
        :ws-flv="flvUrl1"
        :config="jessibucaPlayerConfig"
        />
</div>

   4.2 ts部分

<script setup lang="ts">
import { ref } from "vue"
import JessibucaPlayer from "@/components/JessibucaPlayer/index.vue";

// 存储视频流地址(这里是你自己的视频流地址)
const flvUrl1 = ref<any>("http://xxx.xxx.x.xxx:xxxx/xxx/xxxxx.flv");

// 播放器组件配置
const jessibucaPlayerConfig: Partial<Jessibuca.Config> = {
  isFlv: true,  // 是否使用flv格式
  showBandwidth: true, // 是否显示带宽使用情况
  operateBtns: { // 配置按钮对象
    screenshot: true, // 是否启用截图功能
    fullscreen: true, // 是否启用全屏功能
    play: true, // 是否启用播放功能
    audio: true, // 是否启用音频功能
    record: true // 是否启用录制功能
  },
  loadingText: "加载中..."  // 设置播放器加载时显示的文本
}; 
</script>

完整代码:

<template>
  <!-- 视频流播放区 -->
  <div class="videoBox-play">
      <JessibucaPlayer
        style="width: 100%; height: 230px"
         v-if="flvUrl1"
         :ws-flv="flvUrl1"
         :config="jessibucaPlayerConfig"
       />
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue"
import JessibucaPlayer from "@/components/JessibucaPlayer/index.vue";

// 存储视频流地址(这里是你自己的视频流地址)
const flvUrl1 = ref<any>("http://xxx.xxx.x.xxx:xxxx/xxx/xxxxx.flv");

// 播放器组件配置
const jessibucaPlayerConfig: Partial<Jessibuca.Config> = {
  isFlv: true,  // 是否使用flv格式
  showBandwidth: true, // 是否显示带宽使用情况
  operateBtns: { // 配置按钮对象
    screenshot: true, // 是否启用截图功能
    fullscreen: true, // 是否启用全屏功能
    play: true, // 是否启用播放功能
    audio: true, // 是否启用音频功能
    record: true // 是否启用录制功能
  },
  loadingText: "加载中..."  // 设置播放器加载时显示的文本
}; 
</script>

效果展示:

<think>好的,我现在需要在Vue2项目中集成jessibuca.js实现视频播放功能。首先,我应该回顾一下用户提供的引用内容,看看有没有相关的解决方案或者步骤。 根据引用[1],用户提到在Vue2中没有public文件夹,所以需要将jessibuca的三个文件放在static/jessibuca目录下,并在index.html中引入jessibuca.js。而引用[4]则提到在Vue2中需要将这三个文件存放在public文件夹下。这里可能存在一些差异,需要确认Vue2项目的结构。Vue2通常有static或public目录吗?其实,Vue2默认使用static目录作为静态资源目录,而Vue3使用public。所以用户可能需要根据项目结构调整文件位置。 接下来,用户需要下载所需的文件。引用[2]和引用[4]都提到了decoder.js、decoder.wasm、jessibuca.js这三个文件。用户需要从Jessibuca的官方仓库或DEMO中获取最新版本的文件,确保兼容性。 然后是引入这些文件到项目中。根据引用[1],在Vue2中,可以通过在index.html中使用script标签引入jessibuca.js。例如,在index.html的head部分添加<script src="/static/jessibuca/jessibuca.js"></script>。但要注意路径是否正确,可能需要根据实际存放位置调整src属性。 接下来是组件的封装。用户需要创建一个Vue组件,在mounted生命周期中初始化Jessibuca实例。配置参数如视频容器、解码路径等需要正确设置。引用[3]提到在视频流断开时处理重连,可以通过监听timeout事件并重新调用播放方法。因此,在组件中需要添加相应的事件监听和重连逻辑。 另外,解码路径的问题需要注意,引用[4]指出在Vue2中,decoder.js和decoder.wasm应该放在public或static目录下,并在初始化Jessibuca时指定正确的decoder路径,比如decoder: &#39;/static/jessibuca/decoder.js&#39;。这里需要确保路径正确,否则可能导致解码器加载失败。 最后,用户可能需要处理跨域问题,尤其是在访问不同源的视频流时。引用[1]和引用[4]都提到了这一点,需要确保服务器配置正确的CORS头信息。同时,测试时可以先使用本地视频流或同源流,排除跨域问题的影响。 总结步骤:准备文件、引入脚本、创建组件、初始化实例、配置解码路径、处理事件和错误、注意跨域问题。同时,参考官方文档和社区博客中的解决方案,确保步骤正确。</think>### Vue2集成jessibuca.js实现视频播放的步骤 #### 1. 文件准备 - 从Jessibuca官网或GitHub仓库下载必需文件: - `jessibuca.js`(主库) - `decoder.js`(解码器) - `decoder.wasm`(WebAssembly模块) - 在项目根目录的`static`文件夹下新建`jessibuca`子目录,存放上述文件[^1][^4] #### 2. 引入脚本 在`public/index.html`中添加全局引用: ```html <script src="/static/jessibuca/jessibuca.js"></script> ``` #### 3. 创建视频组件 新建`JessibucaPlayer.vue`组件: ```vue <template> <div class="container"> <div ref="videoContainer" class="video-wrapper"></div> <div v-if="error" class="error-tip">{{ errorMessage }}</div> </div> </template> <script> export default { props: { src: { type: String, required: true } }, data() { return { jessibuca: null, error: false, errorMessage: &#39;&#39; } }, mounted() { this.initPlayer() }, methods: { initPlayer() { this.jessibuca = new Jessibuca({ container: this.$refs.videoContainer, videoBuffer: 0.2, // 缓冲时长 decoder: &#39;/static/jessibuca/decoder.js&#39;, hasAudio: false }) this.jessibuca.play(this.src).catch(err => { this.handleError(&#39;播放失败: &#39; + err.message) }) // 事件监听 this.jessibuca.on(&#39;timeout&#39;, () => { this.handleError(&#39;连接超时&#39;, true) this.reconnect() // 实现自动重连[^3] }) }, reconnect() { setTimeout(() => { this.jessibuca.destroy() this.initPlayer() }, 3000) }, handleError(msg, isRetry) { this.error = true this.errorMessage = isRetry ? `${msg},3秒后重试...` : msg } }, beforeDestroy() { this.jessibuca?.destroy() } } </script> ``` #### 4. 使用组件 ```vue <template> <div> <jessibuca-player :src="rtsp://your-stream-url" /> </div> </template> <script> import JessibucaPlayer from &#39;./components/JessibucaPlayer&#39; export default { components: { JessibucaPlayer } } </script> ``` #### 关键配置说明 1. **解码路径**:`decoder`参数需指向实际文件路径,例如: ```js decoder: process.env.NODE_ENV === &#39;production&#39; ? &#39;/static/jessibuca/decoder.js&#39; : &#39;/jessibuca/decoder.js&#39; ``` 2. **跨域处理**:需在视频流服务器配置: ```http Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET,POST ``` 3. **性能优化**:建议设置`videoBuffer:0.2`(200ms缓冲)平衡延迟与流畅性
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值