最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue使用EasyPlayerPro播放本地MP4視頻

 更新時(shí)間:2024年11月29日 10:56:35   作者:前端熊貓  
這篇文章主要為大家詳細(xì)介紹了Vue如何使用EasyPlayerPro實(shí)現(xiàn)播放本地MP4視頻功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

加載本地mp4需指定協(xié)議http://localhost:5100/(如 http:、https:、webrtc:、ws: 等)來正確處理媒體流。

目錄結(jié)構(gòu)

├── public/
│   ├──static
│   │   ├── video.mp4
├── components/
│   ├── EasyWebRTC.vue
├── views/
│   ├── test/
│   │   ├── index.vue

組件封裝

<template>
  <div class="easy-player-container">
    <!-- 為每個(gè)播放器容器添加唯一的類 -->
    <div id="player_box1" class="player-box"></div>
  </div>
</template>
 
<script>
/* global EasyPlayerPro */
export default {
  name: 'EasyPlayerPro',
  props: {
    initialConfig: {
      type: Object,
      default: () => ({}),
    },
  },
  data () {
    return {
      player: '',
      playerInstances: {}, // 存儲(chǔ)播放器實(shí)例
      config: {
        hasAudio: true,
        isLive: true,
        MSE: false,
        WCS: false,
        demuxType: 'auto',
        ...this.initialConfig,
      },
    };
  },
 
 
 
  methods: {
    setVideoUrl (url, id, changeId) {
 
      const resolveUrl = (url) => {
        if (
          url.startsWith('http:') ||
          url.startsWith('https:') ||
          url.startsWith('webrtc:') ||
          url.startsWith('ws:') ||
          url.startsWith('wss:') ||
          url.startsWith('wt:') ||
          url.startsWith('artc:')
        ) {
          return url;
        }
        const baseUrl = `${window.location.protocol}//${window.location.host}`;
        return `${baseUrl}${url}`;
      };
 
      // 轉(zhuǎn)換 URL
      const resolvedUrl = resolveUrl(url);
      console.log(`解析后的 URL: ${resolvedUrl}`);
 
      if (!resolvedUrl) {
        console.error('URL 不能為空');
        return;
      }
 
      // 動(dòng)態(tài)設(shè)置 demuxType
      let demuxType = 'auto'; // 默認(rèn)值
      if (url.endsWith('.mp4')) {
        demuxType = 'mp4';
      } else if (url.startsWith('webrtc://')) {
        demuxType = 'native'; // WebRTC 流
      } else if (url.startsWith('ws://') || url.startsWith('wss://')) {
        demuxType = 'flv'; // WebSocket 流通常為 FLV
      }
      // 更新播放器配置
      this.config = {
        ...this.config,
        demuxType,
        isLive: url.startsWith('webrtc://') || url.includes('live'),
      };
 
 
      const player = this.playerInstances[id];
      if (player) {
        player.play(resolvedUrl).catch((e) => {
          console.error(`播放失敗 (播放器${id}):`, e);
          this.$emit('play-error', e);
        });
      } else {
        // 使用箭頭函數(shù)確保上下文
        this.$nextTick(() => {
          this.createPlayer(id, resolvedUrl);
        });
      }
    },
 
    createPlayer (id, url) {
      const container = document.getElementById(id);
      if (!container) {
        console.error(`未找到容器, ID: ${id}`);
        return;
      }
 
      const player = new EasyPlayerPro(container, {
        demuxType: this.config.demuxType || 'auto',
        autoplay: this.config.autoplay || true,
        muted: this.config.muted || true,
        isLive: this.config.isLive || true,
      });
 
      player
        .play(url)
        .then(() => {
          console.log(`播放成功: ${url}`);
          this.$emit('play-started', id);
        })
        .catch((e) => {
          console.error(`播放失敗: ${e.message || e}`, e);
          this.$emit('play-error', { id, error: e });
        });
      // 添加事件監(jiān)聽器:循環(huán)播放
      player.on('ended', () => {
        console.log(`播放結(jié)束,準(zhǔn)備循環(huán)播放: ${url}`);
        player.play(url).catch((e) => {
          console.error(`循環(huán)播放失敗: ${e.message || e}`, e);
        });
      });
      this.playerInstances[id] = player;
      // 添加事件解除靜音
      document.addEventListener('click', () => {
        player.unmute(); // 用戶交互后解除靜音
      });
    },
    // 銷毀所有播放器實(shí)例
    destroyAllPlayers () {
      Object.keys(this.playerInstances).forEach(id => {
        this.destroyPlayer(id);
      });
    },
    // 銷毀單個(gè)播放器實(shí)例
    destroyPlayer (id) {
      const player = this.playerInstances[id];
      if (player) {
        player.destroy();
        delete this.playerInstances[id];
      }
    },
    handleUnmute () {
      Object.values(this.playerInstances).forEach((player) => {
        if (player) {
          player.unmute();
        }
      });
    },
  },
 
 
 
  beforeUnmount () {
    // 銷毀所有播放器實(shí)例
    this.destroyAllPlayers();
 
    // 清除全局事件監(jiān)聽器
    document.removeEventListener('click', this.handleUnmute);
  },
 
};
</script>
 
<style scoped>
.easy-player-container {
  width: 100%;
  background: #000;
  height: 100%;
  position: relative;
}
 
 
.player-box {
  background: #000;
}
</style>

應(yīng)用:

<template>
  <div class="video-content">
    <EasyWebRTC ref="baseVideoRef"
      :initialConfig="{ demuxType: 'native', isLive: false, hasAudio: true, autoplay: true, muted: true }">
    </EasyWebRTC>
    <EasyWebRTC ref="videoRef" :initialConfig="{ demuxType: 'flv', isLive: true, hasAudio: true, }"></EasyWebRTC>
  </div>
</template>
<script setup>
import { ref, onMounted, nextTick } from "vue";
import EasyWebRTC from "../components/EasyWebRTC.vue";
 
// 定義組件引用
const baseVideoRef = ref(null);
const videoRef = ref(null);
 
onMounted(() => {
  nextTick(() => {
    // 配置 baseVideoRef
    if (baseVideoRef.value) {
      const baseVideoUrl = "/video.mp4"; // 替換為實(shí)際的視頻文件路徑
      const baseContainerId = "baseVideo";
 
      try {
        baseVideoRef.value.setVideoUrl(baseVideoUrl, baseContainerId, baseContainerId);
        console.log("baseVideoRef:", baseVideoRef.value);
 
        // 修改 DOM 元素 ID
        const targetChild = baseVideoRef.value.$el?.firstElementChild;
        if (targetChild) {
          targetChild.id = baseContainerId; // 修改 ID
          console.log(`成功修改 baseVideo 的 ID 為: ${baseContainerId}`);
        } else {
          console.warn("未找到 baseVideoRef 的子節(jié)點(diǎn)");
        }
      } catch (error) {
        console.error("設(shè)置 baseVideo URL 時(shí)出錯(cuò):", error);
      }
    } else {
      console.error("baseVideoRef 未掛載");
    }
 
    // 配置 videoRef
    if (videoRef.value) {
      const videoUrl = "http://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-360p.mp4";
      const videoContainerId = "video";
 
      try {
        videoRef.value.setVideoUrl(videoUrl, videoContainerId, videoContainerId);
 
        // 修改父元素中的 ID
        const parentElement = document.querySelector(".video-content");
        if (parentElement) {
          const targetChild = parentElement.querySelector("#player_box1");
          if (targetChild) {
            targetChild.id = videoContainerId; // 修改 ID
            console.log(`成功修改 video 的 ID 為: ${videoContainerId}`);
          } else {
            console.warn("未找到 ID 為 'player_box1' 的子節(jié)點(diǎn)");
          }
        } else {
          console.warn("未找到父元素 .video-content");
        }
      } catch (error) {
        console.error("設(shè)置 video URL 時(shí)出錯(cuò):", error);
      }
    } else {
      console.error("videoRef 未掛載");
    }
  });
});
</script>
<style>
.video-content {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
</style>

效果:

到此這篇關(guān)于Vue使用EasyPlayerPro播放本地MP4視頻的文章就介紹到這了,更多相關(guān)Vue EasyPlayerPro播放視頻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue修改對象的屬性值后頁面不重新渲染的實(shí)例

    vue修改對象的屬性值后頁面不重新渲染的實(shí)例

    今天小編就為大家分享一篇vue修改對象的屬性值后頁面不重新渲染的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Vue純前端如何實(shí)現(xiàn)導(dǎo)出簡單Excel表格的功能

    Vue純前端如何實(shí)現(xiàn)導(dǎo)出簡單Excel表格的功能

    這篇文章主要介紹了如何在Vue項(xiàng)目中使用vue-json-excel插件實(shí)現(xiàn)Excel表格的導(dǎo)出功能,包括安裝依賴、引入插件、使用組件、設(shè)置表頭和數(shù)據(jù)、處理空數(shù)據(jù)情況、源代碼修改以解決常見問題,需要的朋友可以參考下
    2025-01-01
  • Vue3?響應(yīng)式高階用法之customRef()的使用

    Vue3?響應(yīng)式高階用法之customRef()的使用

    customRef()是Vue3的高級工具,允許開發(fā)者創(chuàng)建具有復(fù)雜依賴跟蹤和自定義更新邏輯的ref對象,本文詳細(xì)介紹了customRef()的使用場景、基本用法、功能詳解以及最佳實(shí)踐,包括防抖、異步更新等用例,旨在幫助開發(fā)者更好地理解和使用這一強(qiáng)大功能
    2024-09-09
  • vue的.vue文件是怎么run起來的(vue-loader)

    vue的.vue文件是怎么run起來的(vue-loader)

    通過vue-loader,解析.vue文件,在webpack解析,拆解vue組件 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下
    2018-12-12
  • Vue實(shí)現(xiàn)鼠標(biāo)懸浮隱藏與顯示圖片效果@mouseenter和@mouseleave事件詳解

    Vue實(shí)現(xiàn)鼠標(biāo)懸浮隱藏與顯示圖片效果@mouseenter和@mouseleave事件詳解

    在所做的Vue項(xiàng)目中,有時(shí)候需要在鼠標(biāo)移動(dòng)文字框的時(shí)候顯示一些詳細(xì)信息,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)鼠標(biāo)懸浮隱藏與顯示圖片效果@mouseenter和@mouseleave事件的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • 正確更改Ant?Design?of?Vue樣式的問題

    正確更改Ant?Design?of?Vue樣式的問題

    這篇文章主要介紹了正確更改Ant?Design?of?Vue樣式的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue.js父與子組件之間傳參示例

    Vue.js父與子組件之間傳參示例

    本篇文章主要介紹了Vue.js父與子組件之間傳參示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • Django+Vue.js實(shí)現(xiàn)搜索功能

    Django+Vue.js實(shí)現(xiàn)搜索功能

    本文主要介紹了Django+Vue.js實(shí)現(xiàn)搜索功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • Vue3使用Signature Pad實(shí)現(xiàn)電子簽名功能

    Vue3使用Signature Pad實(shí)現(xiàn)電子簽名功能

    Vue3簽名板是一種在Vue3應(yīng)用中實(shí)現(xiàn)用戶簽名功能的組件,Vue3-signature-pad的實(shí)現(xiàn)旨在為開發(fā)者提供一個(gè)簡單易用的工具,使用戶能夠在前端界面上進(jìn)行簽名操作,常見于電子商務(wù)、電子合同等場景,本文介紹了Vue3使用Signature Pad實(shí)現(xiàn)電子簽名功能,需要的朋友可以參考下
    2025-04-04
  • vue項(xiàng)目接口域名動(dòng)態(tài)獲取操作

    vue項(xiàng)目接口域名動(dòng)態(tài)獲取操作

    這篇文章主要介紹了vue項(xiàng)目接口域名動(dòng)態(tài)獲取操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08

最新評論

巴马| 无棣县| 金湖县| 大田县| 城固县| 扬州市| 重庆市| 上饶市| 固原市| 和龙市| 江孜县| 西贡区| 黄大仙区| 五河县| 梁山县| 永春县| 郧西县| 安吉县| 宜城市| 湘潭市| 吴江市| 通榆县| 榆中县| 江川县| 松阳县| 喀什市| 禄丰县| 甘孜县| 平泉县| 城口县| 安泽县| 武冈市| 务川| 通山县| 应用必备| 长宁县| 泾源县| 泗阳县| 沁阳市| 林西县| 寻乌县|