vue3中使用jessibuca.js实现视频播放

文章描述了如何在前端项目中引入并使用JessibucaVideo组件,包括在HTML中引入脚本,创建Vue组件,并管理视频播放、错误状态和全屏功能的实现过程。
摘要由CSDN通过智能技术生成

1、在public文件夹中添加decoder.js、decoder.wasm、jessibuca.js

2、在index.html中进行引入:<script src="/jessibuca.js"></script>

3、新建视频组件JessibucaVideo.vue,代码如下:

<template>

    <div class="video-bounce relative">

        <div class="header">

            <div class="name">

                {{ store.name }}

            </div>

        </div>

        <div class="video" ref="vBox"></div>

        <img

            @click="hideVideo"

            src="@/assets/images/close.png"

            class="close"

            alt=""

        />

        <img

            v-if="play === true"

            src="@/assets/images/loading.png"

            class="loading absolute"

            alt=""

        />

        <img

            src="@/assets/images/full.png"

            @click="requestFull"

            class="enlarge"

            alt=""

        />

        <div

            v-if="fail === true"

            class="error-box absolute flex flex-col items-center justify-center"

        >

            <img src="@/assets/images/error.png" class="error" alt="" />

            <div class="text">播放失败</div>

        </div>

    </div>

</template>

<script setup>

import { videoStore } from '@/store/video.js';

const store = videoStore();

const play = ref(true);

const fail = ref(false);

const vBox = ref(null);

const jessibuca = ref(null);

const initPlayer = () => {

    jessibuca.value = new window.Jessibuca({

        container: vBox.value,

        videoBuffer: 0, // 缓存时长

        isResize: false,

        useWCS: false,

        useMSE: true,

        text: '',

        loadingText: '',

        debug: false,

        supportDblclickFullscreen: true,

        showBandwidth: false, // 显示网速

        operateBtns: {

            fullscreen: false,

            screenshot: false,

            play: false,

            audio: false,

        },

        forceNoOffscreen: true,

        isNotMute: true,

        timeout: 10,

    });

    //视频播放

    jessibuca.value.play(store.url);

    //当解析出视频信息时回调

    jessibuca.value.on('videoInfo', () => {

        play.value = false;

        fail.value = false;

    });

    //触发播放失败事件

    jessibuca.value.on('error', () => {

        play.value = false;

        fail.value = true;

    });

};

const destroyPlayer = () => {

    jessibuca.value.destroy();

};

onMounted(() => {

    initPlayer();

});

onUnmounted(() => {

    destroyPlayer();

});

const hideVideo = () => {

    store.vHide();

};

watch(

    () => store.url,

    (newUrl, oldUrl) => {

        if (newUrl != oldUrl) {

            play.value = true;

            fail.value = false;

            initPlayer();

        }

    }

);

const requestFull = () => {

    vBox.value.requestFullscreen();

};

</script>

<style lang="scss" scoped>

.video-bounce {

    position: fixed;

    left: 0;

    right: 0;

    top: 0;

    bottom: 0;

    margin: auto;

    width: 2000px;

    height: 1210px;

    z-index: 999;

    background: url('@/assets/images/bounceBg.png') no-repeat center center/100%

        100%;

    .header {

        background: url('@/assets/images/vheader.png') no-repeat center center/100%

            100%;

        height: 62px;

        margin: 5px 10px 0 20px;

        .name {

            font-size: 32px;

            line-height: 62px;

            margin-left: 100px;

            font-weight: bold;

            color: #ffffff;

        }

    }

    .close {

        width: 34px;

        height: 34px;

        position: absolute;

        right: 44px;

        top: 14px;

        cursor: pointer;

    }

    .video {

        width: 1900px;

        height: 1120px;

        margin: auto;

        margin-top: 10px;

    }

    .loading {

        width: 100px;

        height: 100px;

        left: 0;

        right: 0;

        bottom: 0;

        top: -20px;

        margin: auto;

        animation: play 2s linear infinite;

    }

    @keyframes play {

        from {

            transform: rotate(0);

        }

        to {

            transform: rotate(360deg);

        }

    }

    .error-box {

        left: 0;

        right: 0;

        bottom: 0;

        top: -30px;

        margin: auto;

        width: 200px;

        height: 200px;

        img {

            width: 100px;

            height: 100px;

        }

    }

    .enlarge {

        width: 50px;

        height: 50px;

        position: absolute;

        right: 110px;

        top: 7px;

        cursor: pointer;

    }

    :deep(.jessibuca-loading) {

        display: none !important;

    }

}

</style>

4、把JessibucaVideo.vue添加到App.vue中进行弹窗显示

  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值