使用西瓜视频官网来创造一个上一集,下一集的按钮,进行视频的切换操作

需求:

仿照西瓜视频写一个视频播放和上一集下一集的按钮功能

回答:

先访问官网:

西瓜播放器

这是西瓜视频的官网,

点击官网的示例按钮,可以看到相关的视频示例以及相关的代码,

我们复制下来代码,然后添加按钮和切换视频的方法,

完整代码:

javascript 复制代码
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,minimal-ui"
    />
    <meta name="referrer" content="no-referrer" />
    <title>播放器</title>
    <style type="text/css">
      html,
      body {
        width: 100%;
        height: 100%;
        margin: auto;
        overflow: hidden;
      }
      body {
        /* display: flex; */
      }
      #mse {
        flex: auto;
        width: 889px !important;
        height: 500px !important;
        margin-bottom: 20px !important;
      }
    </style>
    <script type="text/javascript">
      window.addEventListener("resize", function () {
        document.getElementById("mse").style.height = window.innerHeight + "px";
      });
    </script>
  </head>
  <body>
    <div id="mse"></div>
    <button onclick="playBack()">上一集</button>
    <button onclick="playNext()">下一集</button>
    <script
      src="https://unpkg.byted-static.com/xgplayer/2.31.2/browser/index.js"
      charset="utf-8"
    ></script>
    <script type="text/javascript">
      // 设置视频数组
      let videoArr = [
        "https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-360p.mp4",
        "https://player.caixin.com/video/2021/2/7/2021271612673141018_116_4222.mp4",
        "https://vd2.bdstatic.com/mda-pkveyt87ccdibcj6/sc/cae_h264/1701483084537162496/mda-pkveyt87ccdibcj6.mp4?v_from_s=hkapp-haokan-hbf&auth_key=1704261123-0-0-6e2f86b0c57691e183c7501e2cf89640&bcevod_channel=searchbox_feed&cr=2&cd=0&pd=1&pt=3&logid=3123035774&vid=10328216808716942050&klogid=3123035774&abtest=",
        "https://vd4.bdstatic.com/mda-qa1mniexb3f7r1dj/720p/h264/1704208673349687591/mda-qa1mniexb3f7r1dj.mp4?v_from_s=hkapp-haokan-hbf&auth_key=1704261202-0-0-1df311398200508f9b181ab9b343b5dd&bcevod_channel=searchbox_feed&pd=1&cr=2&cd=0&pt=3&logid=3202891224&vid=1343133119504452132&klogid=3202891224&abtest=",
      ];
      let player = new Player({
        id: "mse",
        autoplay: true,
        volume: 0.3,
        url: "https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-360p.mp4",
        poster:
          "https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/byted-player-videos/1.0.0/poster.jpg",
        playsinline: true,
        thumbnail: {
          pic_num: 44,
          width: 160,
          height: 90,
          col: 10,
          row: 10,
          urls: [
            "https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/byted-player-videos/1.0.0/xgplayer-demo-thumbnail.jpg",
          ],
        },
        danmu: {
          comments: [
            {
              duration: 15000,
              id: "1",
              start: 3000,
              txt: "长弹幕长弹幕长弹幕长弹幕长弹幕",
              style: {
                //弹幕自定义样式
                color: "#ff9500",
                fontSize: "20px",
                border: "solid 1px #ff9500",
                borderRadius: "50px",
                padding: "5px 11px",
                backgroundColor: "rgba(255, 255, 255, 0.1)",
              },
            },
          ],
          area: {
            start: 0,
            end: 1,
          },
        },
        height: window.innerHeight,
        width: window.innerWidth,
        whitelist: [""],
      });
      player.emit("resourceReady", [
        {
          name: "超清",
          url: "https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4",
        },
        {
          name: "高清",
          url: "https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-480p.mp4",
        },
        {
          name: "标清",
          url: "https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-360p.mp4",
        },
      ]);
      let currentIndex = 0;
      function playNext() {
        currentIndex++;
        if (currentIndex >= videoArr.length) {
          currentIndex = 0; // 如果已经是最后一个视频,则切换到第一个视频
        }
        let nextVideoUrl = videoArr[currentIndex];
        player.src = nextVideoUrl;
        // player.src = "https://player.caixin.com/video/2021/2/7/2021271612673141018_116_4222.mp4";
        player.play();
      }

      
      function playBack() {
        currentIndex--;
        if (currentIndex < 0) {
          currentIndex = videoArr.length - 1; // 如果已经是第一个视频,切换到最后一个视频
        }
        let backVideoUrl = videoArr[currentIndex];
        player.src = backVideoUrl;
        // player.src = "https://player.caixin.com/video/2021/2/7/2021271612673141018_116_4222.mp4";
        player.play();
      }
      player.on("ended", function () {
        playNext();
      });
    </script>
  </body>
</html>

这样就实现了一个简单的视频切换功能

相关推荐
安步当歌18 分钟前
【WebRTC】视频编码链路中各个类的简单分析——VideoEncoder
音视频·webrtc·视频编解码·video-codec
hunandede3 小时前
FFmpeg 4.3 音视频-多路H265监控录放C++开发十三:将AVFrame转换成AVPacket。视频编码,AVPacket 重要函数,结构体成员学习
c++·ffmpeg·音视频
橘子味的茶二3 小时前
SDL读取PCM音频
ffmpeg·音视频·pcm
MediaTea14 小时前
Pr 视频过渡:沉浸式视频 - VR 色度泄漏
音视频·vr
LNTON羚通14 小时前
算法定制LiteAIServer摄像机实时接入分析平台烟火检测算法的主要功能
音视频·视频监控
EasyCVR20 小时前
EHOME视频平台EasyCVR视频融合平台使用OBS进行RTMP推流,WebRTC播放出现抖动、卡顿如何解决?
人工智能·算法·ffmpeg·音视频·webrtc·监控视频接入
冷凝女子1 天前
【QT】海康视频及openCv抓拍正脸接口
qt·opencv·音视频·海康
安步当歌1 天前
【WebRTC】视频编码链路中各个类的简单分析——VideoStreamEncoder
音视频·webrtc·视频编解码·video-codec
顾北川_野1 天前
Android CALL关于电话音频和紧急电话设置和获取
android·音视频
顶呱呱程序1 天前
2-143 基于matlab-GUI的脉冲响应不变法实现音频滤波功能
算法·matlab·音视频·matlab-gui·音频滤波·脉冲响应不变法