react中實(shí)現(xiàn)將一個(gè)視頻流為m3u8格式的轉(zhuǎn)換
react將一個(gè)視頻流為m3u8格式的轉(zhuǎn)換
在React中實(shí)現(xiàn)M3U8格式的視頻流轉(zhuǎn)換需要使用一些庫和工具。
一個(gè)簡(jiǎn)單的示例,演示如何將M3U8格式的視頻流轉(zhuǎn)換為可播放的URL。
首先:
你需要安裝videojs-contrib-hls
庫,它是一個(gè)用于處理M3U8格式的視頻流的React組件。
npm install --save video.js videojs-contrib-hls
接下來:
你需要在你的React組件中引入所需的庫和樣式文件。
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格式視頻流的容器。
你可以通過將M3U8地址作為組件的props傳遞給它來播放視頻。
例如:
<VideoPlayer src="https://example.com/video.m3u8" />
react實(shí)現(xiàn)網(wǎng)頁播放m3u8
m3u8是直播常見的格式,如何在網(wǎng)頁上播放它呢?
一、如果是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)上說還要安裝videojs-contrib-hls,但似乎沒有裝它也是可以正常播放的,這個(gè)庫具體的作用,待研究
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)用頁直接引用VedioPlayer
<VideoPlayer src={m3u8url} width="250" />
這里的m3u8url如果是從服務(wù)端獲取的,則一定要保證先獲取成功了再加載VideoPlayer,否則m3u8url為空,頁面依然是播放不了
總結(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)文章
引入代碼檢查工具stylelint實(shí)戰(zhàn)問題經(jīng)驗(yàn)總結(jié)分享
eslint的配置引入比較簡(jiǎn)單,網(wǎng)上有比較多的教程,而stylelint的教程大多語焉不詳。在這里,我會(huì)介紹一下我在引入stylelint所遇到的坑,以及解決方法2021-11-11關(guān)于react ant 組件 Select下拉框 值回顯的問題
這篇文章主要介紹了關(guān)于react ant 組件 Select下拉框 值回顯的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08React18使用Echarts和MUI實(shí)現(xiàn)一個(gè)交互性的溫度計(jì)
這篇文章我們將結(jié)合使用React 18、Echarts和MUI(Material-UI)庫,展示如何實(shí)現(xiàn)一個(gè)交互性的溫度計(jì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01使用React代碼動(dòng)態(tài)生成柵格布局的方法
這篇文章主要介紹了使用React簡(jiǎn)短代碼動(dòng)態(tài)生成柵格布局的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05React?createRef循環(huán)動(dòng)態(tài)賦值ref問題
這篇文章主要介紹了React?createRef循環(huán)動(dòng)態(tài)賦值ref問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01reactjs學(xué)習(xí)解決unknown at rule @tailwind css
這篇文章主要介紹了reactjs學(xué)習(xí)解決unknown at rule @tailwind css問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02React Native中WebView與html雙向通信遇到的坑
這篇文章主要介紹了React Native中WebView與html雙向通信的一些問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01