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

时间:2018-12-05 17:38:52

标签: javascript html html5-video plyr.js

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

以下是我尝试提供的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

5 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

  

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

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

您也可以通过以下方式即时更改自定义网址:   设置配置:

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

答案 2 :(得分:1)

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

  

控件

     

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

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

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

在StackOverflow片段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 });

答案 3 :(得分: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>

答案 4 :(得分:0)

一旦打开plyr.js脚本,请下载Plyr.js脚本以使用VisualStudio Code或Brackets对其进行修改,查找以下代码以删除带有注释的选项。

在下载选项中删除评论。

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

就是这样,保存更改并确认