• react+video.js h5自定义视频暂停图标


    目录

    参考网址

    效果图,暂停时显示暂停图标,播放时隐藏暂停图标

    代码说明,代码传入url后,可直接复制使用

    VideoPausedIcon.ts 组件

    VideoCom.tsx

    Video.module.less


    参考网址

     在Video.js播放器中定制自己的组件 - acgtofe

    效果图,暂停时显示暂停图标,播放时隐藏暂停图标

    代码说明,代码传入url后,可直接复制使用

    注意:videojs升级后,不能用extend创建组件,需要按下方代码建一个组件

    VideoPausedIcon.ts 组件
    1. import videojs from "video.js";
    2. const Component: any = videojs.getComponent("Component");
    3. export class VideoPausedIcon extends Component {
    4. constructor(player: any, options: any) {
    5. super(player, options);
    6. // 监听,暂停播放,显示播放按钮
    7. player.on("pause", () => {
    8. this.visible = true;
    9. const el = this.el();
    10. el.className = "vjs-define-paused";
    11. });
    12. // 监听,开始播放,隐藏播放按钮,通过videojs自带的 vjs-hidden 类
    13. player.on("play", () => {
    14. this.visible = false;
    15. const el = this.el();
    16. el.className = "vjs-define-paused vjs-hidden";
    17. });
    18. this.on("touchstart", this.handleTouchStart);
    19. this.on("touchend", this.handleTouchEnd);
    20. // 如果需要在web端使用,必须,不兼容的话,web端点击按钮就不会暂停/播放
    21. this.on("click", (e: any) => this.handleClick(e, player));
    22. }
    23. createEl() {
    24. let pauseIcon = videojs.dom.createEl("div", {
    25. className: "vjs-define-paused",
    26. tabIndex: -1,
    27. });
    28. !this.visible && videojs.dom.appendContent(pauseIcon, "");
    29. return pauseIcon;
    30. }
    31. handleTouchStart(e: any) {
    32. // 此处必须,不然点击按钮不能播放/暂停
    33. e.stopPropagation();
    34. }
    35. handleTouchEnd(e: any) {
    36. // 此处必须,不然点击按钮不能播放/暂停
    37. e.stopPropagation();
    38. }
    39. handleClick(e: any, player: any) {
    40. e.stopPropagation();
    41. if (!player) {
    42. return;
    43. }
    44. const paused = player.paused();
    45. if (paused) {
    46. player.play();
    47. } else {
    48. player.pause();
    49. }
    50. }
    51. }
    VideoCom.tsx
    1. import React, { useEffect } from "react";
    2. import videojs from "video.js";
    3. import "video.js/dist/video-js.css";
    4. import styles from "./Video.module.less";
    5. import { VideoPausedIcon } from "./VideoPausedIcon";
    6. interface IProps {
    7. url: string;
    8. }
    9. const VideoCom: React.FC<IProps> = (props: any) => {
    10. const videoRef = React.useRef(null);
    11. const playerRef = React.useRef(null);
    12. useEffect(() => {
    13. const player: any = playerRef && playerRef.current;
    14. return () => {
    15. if (player && !player.isDisposed()) {
    16. player.dispose();
    17. playerRef.current = null;
    18. }
    19. };
    20. }, [playerRef]);
    21. useEffect(() => {
    22. if (!props.url) {
    23. return;
    24. }
    25. let options: any = {
    26. controlBar: {
    27. fullscreenToggle: true,
    28. pictureInPictureToggle: false,
    29. volumePanel: false,
    30. playToggle: false,
    31. },
    32. muted: false,
    33. controls: true, //进度条
    34. autoplay: false, //自动播放
    35. loop: false, //是否循环
    36. languages: {
    37. "zh-CN": new URL(`video.js/dist/lang/zh-CN.json`, import.meta.url).href,
    38. },
    39. language: "zh-CN",
    40. preload: "auto",
    41. nodownload: true,
    42. sources: [{ src: props.url, type: "video/mp4" }],
    43. };
    44. // Make sure Video.js player is only initialized once
    45. if (!playerRef || !playerRef.current) {
    46. // The Video.js player needs to be _inside_ the component el for React 18 Strict Mode.
    47. const videoElement = document.createElement("video-js");
    48. videoRef &&
    49. videoRef.current &&
    50. videoRef.current.appendChild(videoElement);
    51. playerRef.current = videojs(videoElement, options, () => {
    52. console.log("player is ready");
    53. const player: any = playerRef.current;
    54. player.on("error", (err: any) => {
    55. console.log("source load fail");
    56. // message.error("视频源请求出错");
    57. });
    58. let touchStartTime = 0;
    59. let touchEndTime = 0;
    60. player.on("touchstart", (e: any) => {
    61. touchStartTime = new Date().getTime();
    62. });
    63. player.on("touchend", (e: any) => {
    64. touchEndTime = new Date().getTime();
    65. if (touchEndTime - touchStartTime < 300) {
    66. const paused = player.paused();
    67. if (paused) {
    68. player.play();
    69. } else {
    70. player.pause();
    71. }
    72. }
    73. });
    74. });
    75. createPausedIcon();
    76. } else {
    77. const player: any = playerRef.current;
    78. player.src(options.sources);
    79. }
    80. }, [
    81. props.url,
    82. playerRef,
    83. videoRef
    84. ]);
    85. const createPausedIcon = () => {
    86. const player = playerRef && playerRef.current;
    87. if (!player) {
    88. return;
    89. }
    90. const Component: any = videojs.getComponent("Component");
    91. Component.registerComponent("VideoPausedIcon", VideoPausedIcon);
    92. const options = {};
    93. const properIndex = player
    94. .children()
    95. .indexOf(player.getChild("BigPlayButton"));
    96. player.addChild("VideoPausedIcon", options, properIndex);
    97. };
    98. return (
    99. <div className={styles.container}>
    100. <div className={styles.videoBox} ref={videoRef}>div>
    101. div>
    102. );
    103. };
    104. export default VideoCom;
    Video.module.less
    1. .container {
    2. width: 100%;
    3. height: 100%;
    4. .videoBox {
    5. width: 100%;
    6. height: 100%;
    7. :global {
    8. .video-js {
    9. width: 100%;
    10. height: 100%;
    11. .vjs-big-play-button {
    12. display: none;
    13. }
    14. .vjs-define-paused {
    15. width: 30px;
    16. height: 28px;
    17. background: rgba(43, 63, 46, 0.7);
    18. border: 1px solid rgb(0, 255, 140);
    19. border-radius: 10px;
    20. position: absolute;
    21. top: 50%;
    22. left: 50%;
    23. transform: translate(-50%, -50%);
    24. transition: all .4s;
    25. &::after {
    26. display: block;
    27. content: '';
    28. position: absolute;
    29. top: 50%;
    30. left: calc(50% + 5px);
    31. transform: translate(-50%, -50%);
    32. border-top: 5px solid;
    33. border-bottom: 5px solid;
    34. border-left: 8px solid;
    35. border-right: 8px solid;
    36. border-color: transparent;
    37. border-left-color: rgb(0, 255, 140);
    38. }
    39. }
    40. }
    41. }
    42. }
    43. }

  • 相关阅读:
    Java01-JDK1.8下载安装教程(win11版)
    http服务器
    2024年Nano编辑器最新使用教程
    基于Java springMVC+MySQL的大学校园BBS论坛网站设计与实现
    ATFX汇市:美国5月PCE数据来袭,EURUSD或迎剧烈波动
    连接oracle报 错误: Undefined Error
    阿里云 Oss 权限控制
    flex设置为1后为什么要设置width为0,和布局超出省略号为什么会超出容器,为什么会没有用
    【Java】Quartz定时作业的创建
    【科普】电脑屏幕刷新率:了解和选择需要的刷新率
  • 原文地址:https://blog.csdn.net/mollerlala/article/details/134440108