Giter Club home page Giter Club logo

eplayer's Introduction

eplayer logo

eplayer NPM version NPM downloads

🎯 A web-components html5 video player facing future.

who use eplayer?

eplayer.js.org - demo

clicli - C 站

Contributors

感谢大家的 pr,阿里嘎多!

Use

  1. ep 基于 web-component,为了兼容,需要事先引入 polyfill
<script src="https://unpkg.com/@webcomponents/webcomponentsjs"></script>
  1. 插入 e-player 标签,没错,只需要将平时用的 video 换成 e-player 即可
<e-player src="./001.mp4"></e-player>

type 属性可选,默认为 mp4,支持 hls 和 flv

  1. 注册 customElement,注意 type=module,使用 es6 的 import
<script type="module">
  import Eplayer from 'https://unpkg.com/eplayer?module'
  //注册 customElement
  customElements.define('e-player', Eplayer)
</script>
  1. 可选定制 css,用于穿透 shadow-dom 预留的默认样式
e-player {
  /* 主题色 默认为 clicli 同款基佬紫*/
  --theme: #00fff6;
  /* 进度条底色 一般不用设置 */
  --progress: rgba(255, 255, 255, 0.3);
  /* 进度条偏移颜色 一般不用设置 */
  --buffer: rgba(255, 255, 255, 0.6);
  /* 图标颜色 一般不用设置 */
  --icons: rgba(255, 255, 255, 0.6);
}
  1. 可选定制插件,会在右击菜单中出现一个选项,触发点击事件
Eplayer.use('github源码', ep => {
  // ep 为 shdow-root 元素
  window.location.href = 'https://github.com/132yse/eplayer'
})

hls

原生支持 mp4mkv ,如果需要支持 m3u8,需要先引入 hls.js

<script src="https://unpkg.com/hls.js"></script>

Npm

yarn add eplayer -S

同样的注册 customElement,但是注意,customElement 只能注册一次,然后还没完,往下看:

omim

omim 是腾讯前端框架 omi 的组件库分支,eplayer 已经集成进去

戳我戳我

Vue

vue 默认是不支持 web-components 的,它无法主动判断含有-的是 vue 组件还是 web 组件

所以需要手动配置,忽略掉e-player

Vue.config.ignoredElements = [
  'e-player'
]

然后,同样需要引入上面的几个文件,然后 bind 一下 src 和 type

<e-player :src="url" :type="type"></e-player>

可以封装成 vue 组件来使用:vue-web-components-wrapper

React / Fre

react 直接支持 customElement,直接在 render 函数中e-player标签

比如,下面这个 fre 的粒子

function EPlayer({ src, type }) {
  const [url, setUrl] = useState(0)
  useEffect(() => {
    fetch(`https://jx.clicli.us/jx?url=${src}@dogecloud`)
      .then(res => res.json())
      .then(data => {
        setUrl(data.url)
      })
  }, [])
  return <e-player src={url} type={type} />
}

完整代码在这里:fre-eplayer

Angular

angular.json 中添加 webcomponentsjshls.js

...
"scripts": [
  "node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js",
  "node_modules/hls.js/dist/hls.min.js"
]
...

app.component.ts 中引入 eplayer

import { Component, OnInit } from "@angular/core";
import Eplayer from "eplayer";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"],
})
export class AppComponent implements OnInit {
  ngOnInit(): void {
    customElements.define("e-player", Eplayer);
  }
}

在需要使用 eplayer 的模块中启用自定义元素的支持

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
import { CommonModule } from "@angular/common";
import { VideoComponent } from "./video.component";

@NgModule({
  declarations: [VideoComponent],
  imports: [CommonModule],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class VideoModule {}

在相应的 html 文件中对 srctype 绑定

<e-player [attr.src]="src" [attr.type]="type"></e-player>

完整项目示例: @cliclitv/pwa

ssr

ssr 服务端没有 web-components 的 API,通常 web-components 的 ssr 都会通过一些库去模拟这些 API

eplayer 不推荐这么做,请直接和正常的 html 引入方式一样,引入 cdn

WAP

WAP 端建议使用原生播放器,国产浏览器太乱了,没得兼容,同时建议往 APP 上引流

Screenshot

eplayer's People

Contributors

aixiu avatar maoqxxmm avatar monkeywithacupcake avatar wu-yu-xuan avatar yisar avatar zhmushan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eplayer's Issues

对eplayer的一点建议

1:兼容性问题是一个成熟产品必须要考虑的问题,希望作者慎重考虑
2:对移动端友好
3:增加点击播放画面,暂停功能
4:增加键盘可以方向键控制音量
5:增加弹幕功能

eplayer mobile rfcs

web-component

是否使用 customElement,受否使用 shadow-dom

移动端众多国内浏览器无比奇葩,shadow-dom 兼容性极差,customElement 会稍稍好些
如果我们继续使用 customElement,API 讲和 pc 端保持一致

<e-player src="./mp4" type="mp4"></e-player>

如果放弃 customElement,会直接 new 实例

new Eplayer({
src:'./mp4',
type:'mp4'
}, document.getElementById('eplayer'))
移动端全屏

两个重要属性

screen.orientation.lock('landscape')
webkit-playsinline="true"

有待研究:IOS 的兼容

b站 svg logo

    <span class="bp-svgicon"
      ><svg width="64" height="64" viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <defs>
          <filter x="-11.2%" y="-10.8%" width="122.4%" height="125.5%" filterUnits="objectBoundingBox" id="pid-1-svgo-a">
            <feGaussianBlur stdDeviation="2" in="shadowOffsetOuter1" result="shadowBlurOuter1"></feGaussianBlur>
            <feComposite in="shadowBlurOuter1" in2="SourceAlpha" operator="out" result="shadowBlurOuter1"></feComposite>
            <feColorMatrix values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.2 0" in="shadowBlurOuter1"></feColorMatrix>
          </filter>
          <path
            d="M52.352 13.5c4.837 0 8.707 4.32 8.647 9.72v21.06C61 49.62 57.128 54 52.29 54h-2.479c0 1.681-1.452 3-3.206 3S43.4 55.62 43.4 54H20.841c0 1.681-1.452 3-3.204 3-1.756 0-3.206-1.38-3.206-3h-2.722C6.87 54 3 49.68 3 44.28V23.22c0-5.4 3.87-9.72 8.709-9.72h11.25l-4.78-4.44c-.725-.661-.725-1.8 0-2.52.424-.36.908-.54 1.391-.54.546 0 1.029.18 1.392.54l7.5 6.96h7.318l7.5-6.96c.422-.36.907-.54 1.39-.54.544 0 1.029.18 1.392.54.725.659.725 1.8 0 2.52l-4.78 4.44h11.07zM26.527 45.54l17.418-10.08c1.45-.901 1.45-2.221 0-3.122L26.527 22.2c-1.452-.84-2.662-.12-2.662 1.56v20.22c0 1.74 1.21 2.4 2.662 1.561z"
            id="pid-1-svgo-b"
          ></path>
        </defs>
        <g fill="none" fill-rule="evenodd">
          <use fill="#000" filter="url(#pid-1-svgo-a)" xlink:href="#pid-1-svgo-b"></use>
          <path
            d="M26.527 45.541c-1.452.84-2.662.18-2.662-1.56V23.76c0-1.68 1.21-2.4 2.662-1.56L43.945 32.34c1.45.9 1.45 2.22 0 3.121L26.527 45.541z"
            fill="#000"
            opacity=".02"
          ></path>
        </g></svg>

播放器的问题

点播放器暂停后,音量会恢复成默认值
点到播放器上,会从当前进度直接跳到初始进度

记录

  1. 监听 error
  2. 增加版权信息

webgl ……

webgl

移动端的浏览器太坑,video 标签基本无解,会被替换 UI

更有 qq 浏览器这种,替换后还自带广告……………………

试过使用 canvas 逐帧的去画视频,但是太卡了,直接播放不了,能播放的清晰度又超级低

我也是实在没办法

webgl 看了一些用例,发现能放,至少 pc 端能接受………………

不知道移动端能不能行……

移动端不够硬的原因有两个:

  1. 性能
  2. 兼容性
  3. three.js 太大了受不了

我觉得我宁愿放弃兼容,也不要被替换 UI

使用 web-components 重构 eplayer

eplayer 当前版本,写的非常仓促,代码质量堪忧
还伴随着 bug

bug 有两种:

  1. 事件冒泡问题。由于播放器纯操作 dom,然后最初设计的时候 dom 结构设计不理想,导致父子事件冒泡问题、click 事件和 mouse 事件的冒泡问题
  2. 移动端兼容问题,当初为了尽可能兼顾移动端的各种奇葩浏览器,甚至独创了 h5 横屏播放::>_<::
    现在想想,有点傻

解决方案

web-components
大家都知道 web-components 有个很*的特性就是 shadow dom 和 scoped css

在一般项目里,它并不好用,直接剥夺了 css 复用的能力(如 reset 就没法用)

但是播放器的场景不一样,看图

image

这个播放框是独立的,甚至很多做法都是通过 iframe,这个独立的播放器可以接入网站任何地方,并且不希望 css 收到外部污染
这样想来,web-components 简直绝配

这样一来,我可以只关注这个 shadow dom 中的 dom 和 css 了

放弃移动端

移动端直接放弃,使用默认的播放器,供各种国产浏览器替换 ui
包括小程序,也直接使用默认播放器,这不是一种妥协,而是更好的兼容::>_<::因为只有默认播放器,是所有移动浏览器完美支持的

这里还有个问题,就是如何判断是否移动端,因为 ssr 环境得不到 ua,所以……得另想他法

bug 收集:#3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.