# Getting started

## Installation

1. This project requires the THEOplayer Web SDK to be installed.

   ```sh
   npm install theoplayer
   ```

   You can also install a different variant of the THEOplayer npm package if you don't need all features, as long as it's aliased as `theoplayer`.

   ```sh
   npm install theoplayer@npm:@theoplayer/basic-hls
   ```

2. Install the Open Video UI for Web.
   ```sh
   npm install @theoplayer/web-ui
   ```

3. Add `@theoplayer/web-ui` to your app:

   * Option 1: in your HTML.
     ```html
     <script src="/path/to/node_modules/theoplayer/THEOplayer.chromeless.js"></script>
     <script src="/path/to/node_modules/@theoplayer/web-ui/dist/THEOplayerUI.js"></script>
     ```

   * Option 2: in your JavaScript.

     ```js
     import { DefaultUI } from '@theoplayer/web-ui';
     ```

     Open Video UI will import THEOplayer from `theoplayer/chromeless` and [Lit](https://lit.dev/) from `lit`. If you're using a bundler such as Webpack or Rollup, these dependencies should automatically get bundled with your web app. Alternatively, you can use an [import map](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) to let the browser resolve it:

     ```html
     <script type="importmap">
         {
             "imports": {
                 "theoplayer/chromeless": "/path/to/node_modules/theoplayer/THEOplayer.chromeless.esm.js",
                 "lit": "/path/to/node_modules/lit/index.js",
                 "lit/decorators.js": "/path/to/node_modules/lit/decorators.js",
                 "lit/directives/": "/path/to/node_modules/lit/directives/"
             }
         }
     </script>
     <!-- Import maps polyfill for browsers without import maps support (e.g. Safari 16.3) -->
     <script async src="https://ga.jspm.io/npm:es-module-shims@2.6.2/dist/es-module-shims.js" crossorigin="anonymous"></script>
     <script type="module" src="/path/to/my_app.js"></script>
     ```

## Usage

### Default UI

`<theoplayer-default-ui>` provides a fully-featured video player experience with minimal setup, and allows for small customizations such as changing colors or fonts.

* Option 1: in your HTML.
  ```html
  <theoplayer-default-ui
      configuration='{"libraryLocation":"/path/to/node_modules/theoplayer/","license":"your_theoplayer_license_goes_here"}'
      source='{"sources":{"src":"https://example.com/stream.m3u8"}}'
  ></theoplayer-default-ui>
  <script>
      // Optionally, access the underlying THEOplayer player instance
      const ui = document.querySelector('theoplayer-default-ui');
      ui.player.addEventListener('playing', () => console.log('THEOplayer is now playing'));
  </script>
  ```
* Option 2: in your JavaScript.
  ```js
  import { DefaultUI } from '@theoplayer/web-ui';
  const ui = new DefaultUI({
      libraryLocation: '/path/to/node_modules/theoplayer/',
      license: 'your_theoplayer_license_goes_here'
  });
  // Set a source for the player to play
  ui.source = {
      sources: {
          src: 'https://example.com/stream.m3u8'
      }
  };
  // Optionally, access the underlying THEOplayer player instance
  ui.player.addEventListener('playing', () => console.log('THEOplayer is now playing'));
  ```

See [this page](https://docs-preview.optiview.dolby.com/pr-861/open-video-ui/web/examples/default-ui.md) for a complete example.

### Custom UI

If you want to fully customize your video player layout, you can use a `<theoplayer-ui>` instead.

```html
<theoplayer-ui
    configuration='{"libraryLocation":"/path/to/node_modules/theoplayer/","license":"your_theoplayer_license_goes_here"}'
    source='{"sources":{"src":"https://example.com/stream.m3u8"}}'
>
    <!-- Choose your own layout using the provided components (or your own!) -->
    <theoplayer-control-bar>
        <theoplayer-time-range></theoplayer-time-range>
    </theoplayer-control-bar>
    <theoplayer-control-bar>
        <theoplayer-play-button></theoplayer-play-button>
        <theoplayer-mute-button></theoplayer-mute-button>
        <theoplayer-volume-range></theoplayer-volume-range>
    </theoplayer-control-bar>
</theoplayer-ui>
```

See [this page](https://docs-preview.optiview.dolby.com/pr-861/open-video-ui/web/examples/custom-ui.md) for a complete example.

### Legacy browser support

By default, Open Video UI for Web targets modern browsers that support modern JavaScript syntax (such as [async/await](https://caniuse.com/async-functions)) and native [Custom Elements](https://caniuse.com/custom-elementsv1). This keeps the download size small, so your viewers can spend less time waiting for your page to load and start watching their video faster.

On older browsers (such as Internet Explorer 11 and older smart TVs), you need to load a different version of the Open Video UI that uses older JavaScript syntax. You also need to load additional polyfills for missing features such as `Promise`, `Symbol.iterator` or Custom Elements:

* For ES2015 features like `Promise` and `Symbol.iterator`, we recommend [the Cloudflare mirror of Polyfill.io](https://cdnjs.cloudflare.com/polyfill/).

* For Custom Elements, we recommend loading our polyfill bundle from `@theoplayer/web-ui/polyfills`. Alternatively, you can load the [Web Components Polyfills](https://github.com/webcomponents/polyfills) along with [Lit's `polyfill-support` module](https://lit.dev/docs/v2/tools/requirements/#polyfills).

* Option 1: in your HTML. This uses [differential serving](https://css-tricks.com/differential-serving/) so modern browsers will load the modern build (with `type="module"`), while legacy browsers will load the legacy build (with `nomodule`).

  ```html
  <!-- Modern browsers -->
  <script type="importmap">
      {
          "imports": {
              "theoplayer/chromeless": "/path/to/node_modules/theoplayer/THEOplayer.chromeless.esm.js",
              "lit": "/path/to/node_modules/lit/index.js",
              "lit/decorators.js": "/path/to/node_modules/lit/decorators.js",
              "lit/directives/": "/path/to/node_modules/lit/directives/"
          }
      }
  </script>
  <!-- Import maps polyfill for browsers without import maps support (e.g. Safari 16.3) -->
  <script async src="https://ga.jspm.io/npm:es-module-shims@2.6.2/dist/es-module-shims.js" crossorigin="anonymous"></script>
  <script type="module" src="/path/to/node_modules/@theoplayer/web-ui/dist/THEOplayerUI.mjs"></script>
  <!-- Legacy browsers -->
  <script nomodule src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es2015%2CglobalThis%2CReflect"></script>
  <script nomodule src="/path/to/node_modules/theoplayer/THEOplayer.chromeless.js"></script>
  <script nomodule src="/path/to/node_modules/@theoplayer/web-ui/dist/THEOplayerUI.polyfills.js"></script>
  <script nomodule src="/path/to/node_modules/@theoplayer/web-ui/dist/THEOplayerUI.es5.js"></script>
  ```

* Option 2: in your JavaScript. This will load the legacy build on both modern and legacy browsers, which is suboptimal. Instead, we recommend configuring your bundler to produce a modern and legacy build of your entire web app, and to import the appropriate version of Open Video UI for each build flavor.

  ```js
  import '@theoplayer/web-ui/polyfills';
  import { DefaultUI } from '@theoplayer/web-ui/es5'; // note the "/es5" suffix
  ```
