简体   繁体   English

如何使用 Plyr JS 在视频播放器中添加下载按钮?

[英]How to add download button in video player with Plyr JS?

I'm using Plyr JS and want to provide download option for each video .我正在使用Plyr JS并希望为每个video提供download选项。

Here is what I have tried to make download option available:这是我试图使download option可用的内容:

despite I have provided: controlsList="nodownload"尽管我提供了: 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>

Question: How do I make the download option to appear using Plyr.io plugin only?问题:如何使下载选项仅使用Plyr.io插件出现?

Here is my Demo: https://codepen.io/msolimans/pen/xQEjmX这是我的演示: https ://codepen.io/msolimans/pen/xQEjmX

You can customize all Plyr controls with Plyr JS.您可以使用 Plyr JS 自定义所有 Plyr 控件。 Here is full description from official source. 这是官方来源的完整描述

Controls控件

This is the markup that is rendered for the Plyr controls.这是为 Plyr 控件呈现的标记。 You can use the default controls or provide a customized version of markup based on your needs.您可以使用默认控件或根据需要提供自定义版本的标记。 You can pass the following to the controls option:您可以将以下内容传递给控件选项:

  • Array of options (this builds the default controls based on your choices)选项Array (这会根据您的选择构建默认控件)
  • Element with the controls带有控件的Element
  • String containing the desired HTML包含所需 HTML 的String
  • false (or empty string or array) to disable all controls false (或空字符串或数组)禁用所有控件
  • Function that will be executed and should return one of the above将被执行并应返回上述之一的Function

DEMO: Plyr player with Custom Controls (download button inclusive) on CodePen.io演示: CodePen.io 上带有自定义控件(包括下载按钮)的 Plyr 播放器

In StackOverflow snippet the download button does not work because it is in sandbox.在 StackOverflow 片段中,下载按钮不起作用,因为它在沙箱中。 Please see the demo on CodePen.io (link above).请参阅 CodePen.io 上的演示(上面的链接)。

Example with Array of options:带有选项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 });

add a link with the "download" tag添加带有“下载”标签的链接

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

Have you tried the author's solution : https://github.com/sampotts/plyr/issues/193#issuecomment-432629429您是否尝试过作者的解决方案https ://github.com/sampotts/plyr/issues/193#issuecomment-432629429

You can turn it on in the controls option by adding download.您可以通过添加下载在控件选项中将其打开。 It'll automatically point to the current source and open in a new window.它会自动指向当前源并在新窗口中打开。 You can also specify a custom url in the urls option, specifically setting the urls.download property - eg您还可以在 urls 选项中指定自定义 url,具体设置 urls.download 属性 - 例如

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

You can set the custom URL on the fly when changing source too by setting the config:您也可以通过设置配置在更改源时即时设置自定义 URL:

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

Haven't seen much activity here so i'm going to add how I fixed the issue.在这里没有看到太多活动,所以我将添加解决问题的方法。 Make sure the plyr.js file has been linked then use this script.确保已链接 plyr.js 文件,然后使用此脚本。 Just remove elements you don't want.只需删除不需要的元素。 ie the restart button has been removed (I turned it into a comment).即重启按钮已被删除(我把它变成了评论)。

<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>

Download the Plyr.js script to modify it with VisualStudio Code or Brackets once open the plyr.js script, look for the following code to delete the option that is commented.下载Plyr.js脚本,用VisualStudio Code或Brackets修改,打开plyr.js脚本,找到下面的代码,删除注释掉的选项。

In the download option delete the comments.在下载选项中删除评论。

// 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'],

it should look like the following它应该如下所示

// 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'],

that's it, save the changes and voila就是这样,保存更改,瞧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM