react中實(shí)現(xiàn)將一個(gè)視頻流為m3u8格式的轉(zhuǎn)換
react將一個(gè)視頻流為m3u8格式的轉(zhuǎn)換
在React中實(shí)現(xiàn)M3U8格式的視頻流轉(zhuǎn)換需要使用一些庫(kù)和工具。
一個(gè)簡(jiǎn)單的示例,演示如何將M3U8格式的視頻流轉(zhuǎn)換為可播放的URL。
首先:
你需要安裝videojs-contrib-hls庫(kù),它是一個(gè)用于處理M3U8格式的視頻流的React組件。
npm install --save video.js videojs-contrib-hls
接下來(lái):
你需要在你的React組件中引入所需的庫(kù)和樣式文件。
import React from 'react';
import videojs from 'video.js';
import 'video.js/dist/video-js.css';
import 'videojs-contrib-hls/dist/videojs-contrib-hls';
class VideoPlayer extends React.Component {
componentDidMount() {
// 在組件掛載后初始化視頻播放器
this.player = videojs(this.videoNode, this.props, function onPlayerReady() {
console.log('視頻播放器已準(zhǔn)備好');
});
}
componentWillUnmount() {
// 在組件卸載前銷毀視頻播放器
if (this.player) {
this.player.dispose();
}
}
render() {
return (
<div>
<div data-vjs-player>
<video ref={(node) => (this.videoNode = node)} className="video-js"></video>
</div>
</div>
);
}
}
export default VideoPlayer;以上是一個(gè)簡(jiǎn)單的視頻播放器組件示例。
你可以將其用作React應(yīng)用中顯示M3U8格式視頻流的容器。
你可以通過(guò)將M3U8地址作為組件的props傳遞給它來(lái)播放視頻。
例如:
<VideoPlayer src="https://example.com/video.m3u8" />
react實(shí)現(xiàn)網(wǎng)頁(yè)播放m3u8
m3u8是直播常見的格式,如何在網(wǎng)頁(yè)上播放它呢?
一、如果是safari,則非常簡(jiǎn)單
因?yàn)閟afari本身就可以支持這種格式,直接用video標(biāo)簽即可,唯一注意的是type一定要指定成application/x-mpegURL
<video height="100%" width="100%" controls>
<source src={m3u8Url} type="application/x-mpegURL" />
</video>二、如果用chrome,則需要用到video.js包
具體的解決步驟如下:
1、安裝video.js相關(guān)的包
npm install --save video.js
網(wǎng)上說(shuō)還要安裝videojs-contrib-hls,但似乎沒有裝它也是可以正常播放的,這個(gè)庫(kù)具體的作用,待研究
2、寫一個(gè)videoPlayer.js
import React, { Component } from "react";
import Videojs from "video.js";
//import "videojs-contrib-hls";
import "video.js/dist/video-js.css";
class VideoPlayer extends Component {
constructor(props) {
super(props);
}
componentWillUnmount() {
// 銷毀播放器
if (this.player) {
this.player.dispose();
}
}
componentDidMount() {
const { height, width, src } = this.props;
this.player = Videojs(
"custom-video",
{
height,
width,
bigPlayButton: true,
textTrackDisplay: false,
errorDisplay: false,
controlBar: true,
type: "application/x-mpegURL",
},
function () {
this.play();
}
);
this.player.src({ src });
}
render() {
return (
<video
id="custom-video"
className="video-js"
controls
preload="auto"
></video>
);
}
}
export default VideoPlayer;
注意:
1)this.player中的id與video標(biāo)簽中的id一定要一致,react就是用這個(gè)id進(jìn)行綁定的;
2)this.player.src({ src });這行一定要放在player的定義的后面,直接放到Vediojs的初始化的src字段中是沒用的。
3)className=“video-js” 這個(gè)className一定要用video-js,否則視頻播放控件就沒有樣式了
3、在調(diào)用頁(yè)直接引用VedioPlayer
<VideoPlayer src={m3u8url} width="250" />這里的m3u8url如果是從服務(wù)端獲取的,則一定要保證先獲取成功了再加載VideoPlayer,否則m3u8url為空,頁(yè)面依然是播放不了
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- React videojs 實(shí)現(xiàn)自定義組件(視頻畫質(zhì)/清晰度切換) 的操作代碼
- React+TS+IntersectionObserver實(shí)現(xiàn)視頻懶加載和自動(dòng)播放功能
- react-native 封裝視頻播放器react-native-video的使用
- React自定義視頻全屏按鈕實(shí)現(xiàn)全屏功能
- react-player實(shí)現(xiàn)視頻播放與自定義進(jìn)度條效果
- React中使用react-player 播放視頻或直播的方法
- react-native-video實(shí)現(xiàn)視頻全屏播放的方法
相關(guān)文章
React應(yīng)用中避免白屏現(xiàn)象的方法小結(jié)
在開發(fā)React應(yīng)用程序時(shí),我們都曾遇到過(guò)這樣的場(chǎng)景:一個(gè)未被捕獲的異常突然中斷了組件的渲染流程,導(dǎo)致用戶界面呈現(xiàn)出一片空白,也就是俗稱的“白屏”現(xiàn)象,本文將探討如何在React應(yīng)用中有效捕獲并處理這些錯(cuò)誤,避免白屏現(xiàn)象的發(fā)生,需要的朋友可以參考下2024-06-06
React和Vue中實(shí)現(xiàn)錨點(diǎn)定位功能
在React中,可以使用useState和useEffect鉤子來(lái)實(shí)現(xiàn)錨點(diǎn)定位功能,在Vue中,可以使用指令來(lái)實(shí)現(xiàn)錨點(diǎn)定位功能,在React和Vue中實(shí)現(xiàn)錨點(diǎn)定位功能的方法略有不同,下面我將分別介紹,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
高性能React開發(fā)React Server Components詳解
ReactServerComponents通過(guò)服務(wù)器端渲染、自動(dòng)代碼分割等技術(shù),實(shí)現(xiàn)了高性能的React開發(fā),它解決了客戶端數(shù)據(jù)請(qǐng)求鏈?zhǔn)窖舆t、敏感數(shù)據(jù)暴露風(fēng)險(xiǎn)等問(wèn)題,提供了更好的用戶體驗(yàn)和安全性,本文介紹高性能React開發(fā)React Server Components詳解,感興趣的朋友一起看看吧2025-03-03
React 遠(yuǎn)程動(dòng)態(tài)組件實(shí)踐示例詳解
這篇文章主要為大家介紹了React 遠(yuǎn)程動(dòng)態(tài)組件實(shí)踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
React Hook用法示例詳解(6個(gè)常見hook)
這篇文章主要介紹了React Hook用法詳解(6個(gè)常見hook),本文通過(guò)實(shí)例代碼給大家介紹了6個(gè)常見hook,需要的朋友可以參考下2021-04-04
React中表單的雙向數(shù)據(jù)綁定的處理方法詳解
在前端開發(fā)中,雙向數(shù)據(jù)綁定(Two-way Data Binding)是指視圖(View)與數(shù)據(jù)模型(Model)之間保持同步,本文將詳細(xì)講解如何在 React 中實(shí)現(xiàn)雙向數(shù)據(jù)綁定,涵蓋原理、常見表單控件(如文本框、單選框、復(fù)選框、下拉框)的處理方式、優(yōu)化技巧以及最佳實(shí)踐2025-06-06
React實(shí)現(xiàn)模糊搜索和關(guān)鍵字高亮的示例代碼
這篇文章主要為大家詳細(xì)介紹了React如何實(shí)現(xiàn)模糊搜索和關(guān)鍵字高亮的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11

