文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

react-player如何实现视频播放与自定义进度条效果

2023-06-28 23:44

关注

这篇文章将为大家详细讲解有关react-player如何实现视频播放与自定义进度条效果,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

环境

react.js   ant design pro 4.0

实现效果

react-player如何实现视频播放与自定义进度条效果

代码

import React from 'react'import { PageHeaderWrapper } from '@ant-design/pro-layout';import { Col ,Row,Button,Form,Input,Card,Slider,Select,message} from 'antd'; import {CaretRightOutlined,PauseOutlined } from '@ant-design/icons';import ReactPlayer from 'react-player'const formItemLayout = {    labelCol: { span: 4,},    wrapperCol: { span: 20},};// 上一条按钮样式const prevStyle={ float: 'left', width: '160px', fontWeight: 'bold', }// 废弃按钮样式const abandonStyle={ float: 'right', marginRight: '15%', width: '120px', color: '#FF0F0F', borderColor: '#FF0F0F', fontWeight: 'bold', }// 完成按钮样式const finishStyle={ float: 'left', marginLeft: '15%', width: '120px', color: '#56CB8B', borderColor: '#56CB8B', fontWeight: 'bold', }// 下一条按钮样式const nextStyle={ float: 'right', width: '160px', fontWeight: 'bold', }// 计时器let interval// 数据回流编辑页export default class OperationEdit extends React.Component{    state={        slideValue:0, // 进度条当前取值        playing:false,  // 视频true播放 false暂停        playbackRate:1,   // 视频播放倍速        timeConsuming:0,   // 本页耗时        duration:0,        // 视频总时长        controllerTimeConsuming:true,  // 本页计时开关 true:计时中   false:计时暂停    }        // 初始化启动计时器    componentDidMount(){        interval = setInterval(this.addTimeConsuming, 1000)    // 操作视频播放组件的对象    ref = player => { this.player = player }    // 本页耗时 计时    addTimeConsuming = () =>{        const {timeConsuming} = this.state        this.setState({timeConsuming:timeConsuming+1})    // 时间格式化    formatTimeConsuming = val =>{        const formateDate = parseInt(val,0) || this.state.timeConsuming        if(formateDate>60){            const m = parseInt((formateDate/60),0)    // 分钟            const s = formateDate - 60*m              // 秒             let res = ""            if(m<10){res +=`0${m}:`}else{res +=`${m}:`}            if(s<10){res +=`0${s}`}else{res +=`${s}`}            return res        }        if(formateDate<10){            return `00:0${formateDate}`         return `00:${formateDate}`        // 视频进度条改变    handleSliderChange = value => {        this.setState({ slideValue: value})   // 设置进度条当前值        this.player.seekTo(parseFloat(value))  // 改变视频进度    };    // 设置视频播放倍速    handleVideoSpeedChange = value =>{        this.setState({playbackRate:value})    // 视频总时长    handleDuration = (duration) => {        this.setState({ duration })    // 清除计时器 或继续启用计时器    changeInterval = controllerTimeConsuming =>{        if(controllerTimeConsuming){  // 开始计时            interval = setInterval(this.addTimeConsuming, 1000)        }else{  // 暂停计时            clearInterval(interval);         this.setState({controllerTimeConsuming})    // 当前播放进度    handleProgress = state => {        this.setState({slideValue:parseFloat(state.playedSeconds)})    render(){        const {slideValue,duration,playing,playbackRate,controllerTimeConsuming} = this.state                return(            <PageHeaderWrapper>                 <Card style={{padding:'0px',marginBottom:15}}>                    {}                    <Row gutter={24}>                        {}                        <Col span={10}>                            <Row gutter={24}>                                {}                                                                       <ReactPlayer                                          ref={this.ref}                                         height="300px"                                         style={{width:"100%",}}                                         url='https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218093206z8V1JuPlpe.mp4'                                          playing={playing}                                         playbackRate={playbackRate}                                         onDuration={this.handleDuration}                                                                                  // onSeek={e => console.log('onSeek', e)} // 当媒体使用seconds参数搜索时调用                                         progressInterval={100}                 // onProgress 回调的速度  太大会导致进度条走动不平滑                                         onProgress={this.handleProgress}                                         onError={e => message.error(e)}  // 视频播放错误                                         // controls // 设置为true或false显示本机播放器控件                                     />                                {}                            </Row>                            <Row gutter={24} style={{marginTop:'10px',textAlign:"center"}}>                                <Col span={8}>                                    <Button onClick={()=>this.setState({playing:!playing})}>{playing?"暂停":"播放"}</Button>                                </Col>                                    <Button>跳过重复</Button>                                    <Select showArrow={false}  // 不显示小箭头                                        defaultValue={playbackRate} onChange={this.handleVideoSpeedChange}                                     >                                        <Select.Option value={0.5}>0.5x</Select.Option>                                        <Select.Option value={0.75}>0.75x</Select.Option>                                        <Select.Option value={1}>倍速</Select.Option>                                        <Select.Option value={1.25}>1.25x</Select.Option>                                        <Select.Option value={1.5}>1.5x</Select.Option>                                        <Select.Option value={2}>2x</Select.Option>                                    </Select>                            </Row>                          </Col>                        {}                        <Col span={12}>                            {}                                                        <Form {...formItemLayout} >                                <Row gutter={24}>                                    <Col span={24}>                                         <Form.Item  name="zhenPhoto" label="静态图">                                            <Input  style={{height:80}} placeholder="请输入静态图" />                                        </Form.Item>                                    </Col>                                </Row>                                        <Form.Item  name="distinguishResult" label="识别结果">                                            <Input style={{height:80}} placeholder="请输入识别结果" />                                        <Form.Item  name="markResult" label="标注结果">                                            <Input style={{height:80}} placeholder="标注结果" />                            </Form>                         {}                        <Col span={2}>                            <Row gutter={24} style={{height:100}}><Col span={24}><Button>V1.0.01</Button></Col></Row>                            <Row gutter={24} style={{height:100}}><Col span={24}><Button>V1.0.00</Button></Col></Row>                    </Row>                    {}                    <Row gutter={24} style={{height:200}}>                        <Col span={22}>                            {}                            <div style={{border:'1px solid red',height:'180px'}} />                            <Slider                                 style={{marginBottom:10}} value={slideValue} max={duration}                                 step={0.01} onChange={this.handleSliderChange}                                 tipFormatter={val=>this.formatTimeConsuming(val)}                                />                    {}                    <Row gutter={24} style={{height:80}}>                            <Row gutter={24} style={{textAlign:'center'}}>                                <Col span={12} style={{textAlign:"left"}}>                                    <Button size="large" style={prevStyle}>上一条</Button>                                    <Button size="large" style={abandonStyle}>废弃</Button>                                <Col span={12}>                                    <Button size="large" style={finishStyle}>完成</Button>                                    <Button size="large" style={nextStyle}>下一条</Button>                    {}                    <Row gutter={22} >                        <Col span={5}>                            <div style={{float:'left'}}> <Button>本页耗时:{this.formatTimeConsuming()}</Button> </div>                              <div style={{float:'left'}}>                                {controllerTimeConsuming?                                    <PauseOutlined  onClick={()=>this.changeInterval(false)} style={{color:"#1296DB",fontSize:30}}/>                                    :<CaretRightOutlined onClick={()=>this.changeInterval(true)} style={{color:"#1296DB",fontSize:30}} />}                            </div>                         <Col span={12} style={{textAlign:'center'}}><span style={{fontSize:'20px',fontWeight: 'bold'}}>32 / 100</span></Col>                        <Col span={5} style={{textAlign:'right'}}><Button>总耗时:08:25</Button></Col>                </Card>            </PageHeaderWrapper>        )}

关于react-player如何实现视频播放与自定义进度条效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯