【问题标题】:How to add download button in video player with Plyr JS?如何使用 Plyr JS 在视频播放器中添加下载按钮?
【发布时间】:2018-12-05 17:38:51
【问题描述】:

我正在使用Plyr JS,并希望为每个video 提供download 选项。

这是我试图使 download option 可用的内容:

尽管我提供了: controlsList="nodownload"

<video controls crossorigin playsinline controlsList="nodownload" poster="https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-HD.jpg" id="player">
     <source src="https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-576p.mp4" type="video/mp4" size="576">
</video>

问题:如何让下载选项仅使用Plyr.io插件显示?

这是我的演示: https://codepen.io/msolimans/pen/xQEjmX

【问题讨论】:

  • 对 StackOverflow 的感谢是通过投票和接受回答来完成的。如果您对我下面的回答感到满意,请在答案左侧将其标记为已接受和/或投票。您还将为此行动获得 2 点声望。

标签: javascript html html5-video plyr.js


【解决方案1】:

您可以使用 Plyr JS 自定义所有 Plyr 控件。 Here is full description来自官方。

控件

这是为 Plyr 控件呈现的标记。您可以使用默认控件或根据您的需要提供自定义版本的标记。您可以将以下内容传递给控件选项:

  • Array of options(这会根据您的选择构建默认控件)
  • Element 带有控件
  • String 包含所需的 HTML
  • false(或空字符串或数组)禁用所有控件
  • Function 将被执行并应返回上述之一

DEMO:Plyr player with Custom Controls(包括下载按钮)在 CodePen.io

在 StackOverflow sn-p the download button does not work 因为它在沙盒中。请参阅 CodePen.io 上的演示(上面的链接)。

带有Array 选项的示例:

var controls =
[
    'play-large', // The large play button in the center
    'restart', // Restart playback
    'rewind', // Rewind by the seek time (default 10 seconds)
    'play', // Play/pause playback
    'fast-forward', // Fast forward by the seek time (default 10 seconds)
    'progress', // The progress bar and scrubber for playback and buffering
    'current-time', // The current time of playback
    'duration', // The full duration of the media
    'mute', // Toggle mute
    'volume', // Volume control
    'captions', // Toggle captions
    'settings', // Settings menu
    'pip', // Picture-in-picture (currently Safari only)
    'airplay', // Airplay (currently Safari only)
    'download', // Show a download button with a link to either the current source or a custom URL you specify in your options
    'fullscreen' // Toggle fullscreen
];

var player = new Plyr('#player', { controls });

【讨论】:

    【解决方案2】:

    添加带有“下载”标签的链接

    <a href="https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-576p.mp4" download> download </a>
    

    【讨论】:

    • video control 内我想为当前视频设置下载选项
    • 你不行,你需要重写视频控制器
    【解决方案3】:

    你尝试过作者的解决方案https://github.com/sampotts/plyr/issues/193#issuecomment-432629429

    您可以通过添加下载在控件选项中打开它。它会 自动指向当前源并在新窗口中打开。 您还可以在 urls 选项中指定自定义 url,特别是 设置 urls.download 属性 - 例如

    const player = new Plyr('#player', {
      urls: {
        download: 'https://example.com/path/to/download',
      },
    });
    

    您也可以在更改源时即时设置自定义 URL 设置配置:

    player.config.urls.download = 'https://example.com/path/to/download';
    

    【讨论】:

      【解决方案4】:

      这里没有看到太多活动,所以我将添加我解决问题的方法。 确保 plyr.js 文件已链接,然后使用此脚本。只需删除您不想要的元素。即重新启动按钮已被删除(我把它变成了评论)。

      <script>
      
          document.addEventListener('DOMContentLoaded', () => {
      
              const controls = [
                  'play-large', // The large play button in the center
                  //'restart', // Restart playback
                  'rewind', // Rewind by the seek time (default 10 seconds)
                  'play', // Play/pause playback
                  'fast-forward', // Fast forward by the seek time (default 10 seconds)
                  'progress', // The progress bar and scrubber for playback and buffering
                  'current-time', // The current time of playback
                  'duration', // The full duration of the media
                  'mute', // Toggle mute
                  'volume', // Volume control
                  'captions', // Toggle captions
                  'settings', // Settings menu
                  'pip', // Picture-in-picture (currently Safari only)
                  'airplay', // Airplay (currently Safari only)
                  'download', // Show a download button with a link to either the current source or a custom URL you specify in your options
                  'fullscreen' // Toggle fullscreen
              ];
      
              const player = Plyr.setup('.js-player', { controls});
       });
      </script>
      

      【讨论】:

        【解决方案5】:

        下载 Plyr.js 脚本,用 VisualStudio Code 或 Brackets 修改打开 plyr.js 脚本后,查找以下代码删除注释的选项。

        在下载选项中删除 cmets。

        // Default controls
            controls: ['play-large', // 'restart',
            // 'rewind',
            'play', // 'fast-forward',
            'progress', 'current-time', //'duration',
            'mute', 'volume', 'captions', 'settings', //'pip',
            'airplay', //'download',
            'fullscreen'],
            settings: ['captions', 'quality', 'speed'],
        

        它应该如下所示

        // Default controls
            controls: ['play-large', // 'restart',
            // 'rewind',
            'play', // 'fast-forward',
            'progress', 'current-time', //'duration',
            'mute', 'volume', 'captions', 'settings', //'pip',
            'airplay', 'download',
            'fullscreen'],
            settings: ['captions', 'quality', 'speed'],
        

        就是这样,保存更改,瞧

        【讨论】:

          猜你喜欢
          • 2015-07-06
          • 1970-01-01
          • 2016-08-27
          • 1970-01-01
          • 2019-08-13
          • 1970-01-01
          • 2018-09-17
          • 2021-12-24
          • 1970-01-01
          相关资源
          最近更新 更多