HTML5视频和音频播放器插件 MediaElement.js

  • 源码大小:2.02MB
  • 所需积分:1积分
  • 源码编号:19JP-3120
  • 浏览次数:934次
  • 最后更新:2023年05月08日
  • 所属栏目:其他
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

MediaElement.js是一个基于HTML5媒体API的灵活而健壮的jQuery/JavaScript音频和视频播放器插件。

支持几乎所有的音频和视频格式,如MP4、MP3、WebM以及Dailymotion、Facebook、SoundCloud、Twitch、Vimeo和Youtube。

目录:

  • 安装
  • 选项
  • 属性
  • API方法

如何使用它:

1.在文档中加载核心MediaElement.js JavaScript库、OPTIONAL渲染图和语言文件。

  1. <!-- jQuery is OPTIONAL -->
  2. <script src="/path/to/cdn/jquery.min.js"></script>
  3. <!-- Core -->
  4. <link rel="stylesheet" href="/path/to/build/mediaelementplayer.min.css" />
  5. <script src="/path/to/build/mediaelement-and-player.js"></script>
  6. <!-- Dailymotion Renderer -->
  7. <script src="/path/to/build/renderers/dailymotion.js"></script>
  8. <!-- Facebook Video Renderer -->
  9. <script src="/path/to/build/renderers/facebook.js"></script>
  10. <!-- Soundcloud Renderer -->
  11. <script src="/path/to/build/renderers/soundcloud.js"></script>
  12. <!-- Twitch Renderer -->
  13. <script src="/path/to/build/renderers/twitch.js"></script>
  14. <!-- Vimeo Renderer -->
  15. <script src="/path/to/build/renderers/vimeo.js"></script>
  16. <!-- Youtube Renderer -->
  17. <script src="/path/to/build/renderers/youtube.js"></script>
  18. <!-- All Languages -->
  19. <script src="/path/to/build/lang/ca.js"></script>
  20. <script src="/path/to/build/lang/cs.js"></script>
  21. <script src="/path/to/build/lang/de.js"></script>
  22. <script src="/path/to/build/lang/es.js"></script>
  23. <script src="/path/to/build/lang/fa.js"></script>
  24. <script src="/path/to/build/lang/fr.js"></script>
  25. <script src="/path/to/build/lang/hr.js"></script>
  26. <script src="/path/to/build/lang/hu.js"></script>
  27. <script src="/path/to/build/lang/it.js"></script>
  28. <script src="/path/to/build/lang/ja.js"></script>
  29. <script src="/path/to/build/lang/ko.js"></script>
  30. <script src="/path/to/build/lang/ms.js"></script>
  31. <script src="/path/to/build/lang/nl.js"></script>
  32. <script src="/path/to/build/lang/pl.js"></script>
  33. <script src="/path/to/build/lang/pt.js"></script>
  34. <script src="/path/to/build/lang/ro.js"></script>
  35. <script src="/path/to/build/lang/ru.js"></script>
  36. <script src="/path/to/build/lang/sk.js"></script>
  37. <script src="/path/to/build/lang/sv.js"></script>
  38. <script src="/path/to/build/lang/tr.js"></script>
  39. <script src="/path/to/build/lang/uk.js"></script>
  40. <script src="/path/to/build/lang/zh-cn.js"></script>
  41. <script src="/path/to/build/lang/zh.js"></script>

2.在HTML5视频或音频上初始化MediaElement.js并完成。

  1. // As a jQuery plugin
  2. $('video, audio').mediaelementplayer({
  3. // more configuration here
  4. });
  5.  
  6. // As a Vanilla JavaScript plugin
  7. var player = new MediaElementPlayer('player', {
  8. // more configurations here
  9. });

3.配置媒体播放器的可能选项。请注意,该插件支持所有本地HTML5视频和音频属性。有关更多详细信息,请参阅<video/>和<audio/>。

  1. $('video, audio').mediaelementplayer({
  2.  
  3. /* Core Options */
  4.  
  5. // List of renderers to use
  6. renderers: [],
  7.  
  8. // Name of the container
  9. fakeNodeName: mediaelementwrapper,
  10.  
  11. // Path where Flash shims are located
  12. pluginPath: 'build/',
  13.  
  14. // path where the svg icon sprite is located
  15. iconSprite: 'mejs-controls.svg',
  16.  
  17. // Possible values: always (CDN version) or sameDomain (local files)
  18. shimScriptAccess: 'sameDomain',
  19.  
  20. // Success callback
  21. // media: the wrapper that mimics all the native events/properties/methods for all renderers
  22. // node: the original HTML video, audio or iframe tag where the media was loaded originally
  23. success: function(media, node) {},
  24.  
  25. // Error callback
  26. error: function(media, node) {},
  27.  
  28. // Dailymotion options
  29. // See: https://developer.dailymotion.com/player
  30. dailymotion: {},
  31.  
  32. // Dash options
  33. // See: https://github.com/Axinom/drm-quick-start
  34. dash: {},
  35.  
  36. // Facebook Video options
  37. // See: https://developers.facebook.com/docs/plugins/embedded-video-player/api#setup
  38. facebook: {},
  39.  
  40. // flv.js options
  41. // See: https://github.com/Bilibili/flv.js/blob/master/docs/api.md
  42. flv: {},
  43.  
  44. // hls.js options
  45. // See: https://github.com/dailymotion/hls.js/blob/master/API.md#fine-tuning
  46. hls: {},
  47.  
  48. // Youtube options
  49. // See: https://developers.google.com/youtube/player_parameters#Parameters
  50. youtube: {
  51. nocookie: false,
  52. imageQuality: 'default', // hqdefault, mqdefault, sddefault and maxresdefault
  53. // Youtube options here
  54. },
  55.  
  56. // Class prefix for player elements
  57. classPrefix: 'mejs__',
  58.  
  59. // Poster URL
  60. poster: '',
  61.  
  62. // Show the poster when the video is ended
  63. showPosterWhenEnded: false,
  64.  
  65. // Show the poster when the video is paused
  66. showPosterWhenPaused: false,
  67.  
  68. // Default video width/height
  69. defaultVideoWidth: 480,
  70. defaultVideoHeight: 270,
  71.  
  72. // video width/height
  73. videoWidth: -1,
  74. videoHeight: -1,
  75.  
  76. // Default audio width/height
  77. defaultAudioWidth: 400,
  78. defaultAudioHeight: 30,
  79.  
  80. // audio width/height
  81. audioWidth : -1 Width of audio player
  82. audioHeight: -1 Height of audio player
  83.  
  84. // Default amount to move back when back key is pressed
  85. defaultSeekBackwardInterval: function(media) {return (media.duration * 0.05);},
  86.  
  87. // Default amount to move forward when forward key is pressed
  88. defaultSeekForwardInterval: function(media) {return (media.duration * 0.05);},
  89.  
  90. // Set dimensions via JS instead of CSS
  91. setDimensions: true,
  92.  
  93. // Initial volume when the player starts
  94. startVolume: 0.8,
  95.  
  96. // Infinite loop
  97. loop: false,
  98.  
  99. // Auto rewind when the media ends
  100. autoRewind: true,
  101.  
  102. // Auto resize
  103. enableAutosize: true,
  104.  
  105. // Time format
  106. timeFormat: 'mm:ss',
  107.  
  108. // Always show hours
  109. alwaysShowHours: false,
  110.  
  111. // Show frame count in timecode (##:00:00:00)
  112. showTimecodeFrameCount: false,
  113.  
  114. // Frames per second
  115. framesPerSecond: 25,
  116.  
  117. // Automatically calculate the width of the progress bar based on the sizes of other elements
  118. autosizeProgress: true,
  119.  
  120. // Hide controls when playing and mouse is not over the video
  121. alwaysShowControls: false,
  122.  
  123. // Hide the video control when the media is loading
  124. hideVideoControlsOnLoad: false,
  125.  
  126. // Hide the video controls when the media is paused
  127. hideVideoControlsOnPause: false,
  128.  
  129. // Clicking video element to toggle play/pause
  130. clickToPlayPause: true,
  131.  
  132. // Time in ms to hide controls
  133. controlsTimeoutDefault: 1500,
  134.  
  135. // Time in ms to trigger the timer when your mouse moves
  136. controlsTimeoutMouseEnter: 2500,
  137.  
  138. // Time in ms to trigger the timer when your mouse leaves
  139. controlsTimeoutMouseLeave: 1000,
  140.  
  141. // Use iPad's native controls
  142. iPadUseNativeControls: false,
  143.  
  144. // Use iPhone's native controls
  145. iPhoneUseNativeControls: false,
  146.  
  147. // Use Android's native controls
  148. AndroidUseNativeControls: false,
  149.  
  150. // List of features/plugin to use in the player
  151. features: [playpause, current, progress, duration, tracks, volume, fullscreen],
  152.  
  153. // Use all the default controls
  154. useDefaultControls: false,
  155.  
  156. // Only for dynamic purposes
  157. isVideo: true,
  158.  
  159. // Stretching modes for video player
  160. // or 'fill'
  161. stretching: 'auto',
  162.  
  163. // Enable keyboard
  164. enableKeyboard: true,
  165.  
  166. // Pause other players when the current one is playing
  167. pauseOtherPlayers: true,
  168.  
  169. // Ignore pauseOtherPlayers option on the current player
  170. ignorePauseOtherPlayersOption: true,
  171.  
  172. // Number of decimal places to show if frames are shown
  173. secondsDecimalLength: 0,
  174.  
  175. // Custom error
  176. // string or function
  177. customError: function(media, node){},
  178.  
  179. // Keyboard actions
  180. keyActions: {keys: [1,2,3...], action: function(player, media) { ... }},
  181.  
  182. // Start point
  183. duration: -1,
  184.  
  185. // Separator between the current time and the total duration
  186. timeAndDurationSeparator: '<span> &#124; </span>'
  187.  
  188. // Hide the volume on touch devices
  189. hideVolumeOnTouchDevices: true,
  190.  
  191. // Enable tooltip on the progress bar
  192. enableProgressTooltip: true,
  193.  
  194. // Enable smooth behavior when hovering over the progress bar
  195. useSmoothHover: true,
  196.  
  197. // If set to true, the Live Broadcast message will be displayed and progress bar will be hidden, no matter if duration is a valid number
  198. forceLive: false,
  199.  
  200. // Position of volume slider
  201. audioVolume: 'horizontal',
  202. videoVolume: 'vertical',
  203.  
  204. // Activate detection of Pointer events when on the fullscreen mode
  205. usePluginFullScreen: true,
  206.  
  207. // Bypass native capabilities on mobile devices and use the fake-fullscreen mode
  208. useFakeFullscreen: false,
  209.  
  210. // Remove the [cc] button when no <track kind="subtitles"> are present
  211. hideCaptionsButtonWhenEmpty: true,
  212.  
  213. // If true and we only have one track, change captions to toggle button
  214. toggleCaptionsButtonWhenOnlyOne: false,
  215.  
  216. // Default cue line in which to display cues if the cue is set to "auto" (no line entry in VTT).
  217. // Can be set to false to disable.
  218. defaultTrackLine: -3,
  219.  
  220. // Automatically turn on a <track> element.
  221. autoplayCaptionLanguage: '',
  222.  
  223. // Set the language of the chapters track.
  224. chaptersLanguage: '',
  225.  
  226. // Hide the video player screen reader title so it can be added by the website
  227. hideScreenReaderTitle: false,
  228.  
  229. // Text for accessibility
  230. tracksText: null,
  231. chaptersText: null,
  232. muteText: null,
  233. unmuteText: null,
  234. allyVolumeControlText: null,
  235. fullscreenText: null,
  236. playText: null,
  237. pauseText: null,
  238.  
  239. });

4.性能:

  1. // returns true or false
  2. myPlayer.autoplay
  3.  
  4. // returns an object representing the buffered parts of the audio/video
  5. myPlayer.buffered
  6.  
  7. // returns true or false
  8. myPlayer.controls
  9.  
  10. // returns the URL
  11. myPlayer.currentSrc
  12.  
  13. // returns the current playback position in the audio/video
  14. myPlayer.currentTime
  15.  
  16. // returns the length in seconds
  17. myPlayer.duration
  18.  
  19. // returns true or false
  20. myPlayer.ended
  21.  
  22. // returns a MediaError object representing the error state
  23. myPlayer.error
  24.  
  25. // returns true or false
  26. myPlayer.loop
  27.  
  28. // returns true or false
  29. myPlayer.muted
  30.  
  31. // returns true or false
  32. myPlayer.paused
  33.  
  34. // return the current ready state
  35. myPlayer.readyState
  36.  
  37. // returns true or false
  38. myPlayer.seeking
  39.  
  40. // return the current source
  41. myPlayer.src
  42.  
  43. // returns the volume
  44. myPlayer.volume

5.API方法:

  1. // enable/disable autoplay
  2. myPlayer.autoplay(true/false);
  3.  
  4. // show/hide controls
  5. myPlayer.controls(true/false);
  6.  
  7. // set the current time
  8. myPlayer.currentTime(time);
  9.  
  10. // enable/disable loop
  11. myPlayer.loop(true/false);
  12.  
  13. // mute/unmute
  14. myPlayer.muted(true/false);
  15.  
  16. // sets the source
  17. myPlayer.src(url);
  18.  
  19. // sets the volume
  20. myPlayer.volume(volume);
  21.  
  22. // reload
  23. myPlayer.load();
  24.  
  25. // play
  26. myPlayer.play();
  27.  
  28. // pause
  29. myPlayer.pause();
  30.  
  31. // stop
  32. myPlayer.stop();
  33.  
  34. // destroy
  35. myPlayer.remove();
  36.  
  37. // determine whether current player can/cannot play a specific media type
  38. // type is MIME type and each renderer has a whitelist of them
  39. myPlayer.canPlayType(type);
  40.  
  41. // set player's width and height
  42. myPlayer.setPlayerSize(width, height);
  43.  
  44. // set poster
  45. myPlayer.setPoster(url);
  46.  
  47. // mute/unmute the player
  48. myPlayer.setMuted(muted);
  49.  
  50. // set a new time
  51. myPlayer.setCurrentTime(time);
  52.  
  53. // get the current time
  54. myPlayer.getCurrentTime();
  55.  
  56. // set a volume leveal (between 0 and 1)
  57. myPlayer.setVolume(volume);
  58.  
  59. // retrieve the current volume level
  60. myPlayer.getVolume();
  61.  
  62. // set a new URL for the player
  63. myPlayer.setSrc(src);
  64.  
  65. // retrieve the media URL/path
  66. myPlayer.getSrc();

更新日志:

版本6.0.3(2023-03-29)

  • 将轮廓样式:auto替换为轮廓样式:solid,使Firefox能够在定义时显示正确的轮廓颜色。

版本6.0.2(2023-03-08)

  • 添加了轮廓样式,使Firefox能够在定义的情况下显示正确的轮廓颜色。

版本6.0.1(2023-02-10)

  • 错误修复

版本6.0.0(2023-02-07)

  • 修复删除播放器后丢失的视频曲目
  • 添加了浏览器本机字幕支持,并删除了自定义VTT/DFXP解析
  • 删除了对不起作用的“幻灯片”音轨的支持
  • 删除了选项:“tracksArialLive”、“captionTextPreprocessor”、“slidesSelector”
  • 为清晰起见,将“startLanguage”选项重命名为“autoplayCaptionLanguage”
  • 新选项“章节语言”可设置所显示章节轨迹的语言。
  • 新选项“defaultTrackLine”控制默认情况下显示哪行字幕
  • 修正了无法激活禁用的字幕按钮的问题
  • 修正了无法自动播放的“默认”曲目(仅限字幕和字幕曲目)

v5.1.1 (2023-02-06)

  • 解决mejs__container未接收到可见焦点,违反WCAG SC 2

v.1.0版本(2022-09-07)

  • 用户交互调用搜索时检测
  • 在JS打包器中使用语言文件中的严格模式可能会导致javascript错误
  • 修复css停用时的容器高度和宽度计算

v5.0.5 (2021-11-05)

  • 添加了隐藏屏幕阅读器标题的新选项(可选)
  • WCAG:在海报图像中添加空的alt

v5.0.4 (2021-11-05)

  • 将数据src切换为src

v5.0.3 (2021-11-01)

  • 将描述的属性aria添加到水平音量按钮
  • 将type=“button”重新包含到按钮中,以防止在表单中提交
  • 所有浏览器中空间和返回键的一致使用

v5.0.2 (2021-10-19)

  • 反映第一次点击时叠加播放按钮的正确咏叹调按下状态
  • 正确反映叠加播放按钮的aria按下状态*通过将音频/视频元素设置为tabindex=“-1”,最大限度地减少Firefox所需的选项卡

v5.0.1 (2021-10-07)

  • 修复全屏视频不在移动视图中居中的问题
  • 修复了启用自定义高对比度设置时,当前音量和总音量无法区分的问题

v5.0.0 (2021-09-17)

  • 将fakeNodeName的默认值更改为div
  • 使用grunt任务将所有文件从/demo文件夹复制到/build文件夹
  • 用svg图标精灵替换控件的背景精灵
  • 新选项iconSprite:svg图标sprite所在的路径,请参阅独立文档
  • 将样式中的px值更改为rem值
  • 将描述的属性aria添加到音量按钮

v4.2.1.7 (2021-07-05)

  • 修复player.options.startVolume被0覆盖的问题
  • 修复一些html注释的拼写错误,删除旧的404来源
  • 修复Safari下的全屏问题
  • 获取mov文件的正确mime类型
  • 修复enableAutosize:false错误
  • 修复屏幕外的罕见问题.remove()
  • 修复DFXP解析:删除jQuery使用的残余
  • 计算isNative时使用相同的renderName变量

预览截图