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

5
我正在使用 Plyr JS,想为每个视频提供下载选项。
以下是我尝试使下载选项可用的方法:
尽管我已经提供了: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个声望点。 - Bharata
5个回答

6
你可以使用Plyr JS自定义所有Plyr控件。从官方来源这里是完整说明

控件

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

  • 选项数组(基于您的选择构建默认控件)
  • 带有控件的元素
  • 包含所需HTML的字符串
  • false(或空字符串或数组)以禁用所有控件
  • 将执行并应返回上述内容之一的函数

演示:带有自定义控件的Plyr播放器(包括下载按钮)在CodePen.io上

在StackOverflow片段中下载按钮无法使用,因为它位于沙盒中。请查看CodePen.io上的演示(上面的链接)。
带有选项数组的示例:
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 });

3

使用“download”标签添加链接

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

在“视频控制”内,我想为当前视频添加“下载选项”。 - EaBengaluru
你不能这样做,你需要重写视频控制器。 - Max Alexander Hanna

3

你尝试过作者的解决方案吗: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 - e.g.

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:

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

1
我看这里没有太多活动,所以我要添加如何解决这个问题的说明。 确保已经链接了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>

0

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

在下载选项中删除注释。

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

就是这样,保存更改,然后唰


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接