# Agamo player setup (partner / Confluence)

Use this page to embed the Sportradar Agamo web player.

**Recommended:** initialize with **`setupPlayerAsync`**.  
**`setupPlayer` is legacy.** Keep it only if you already call it and cannot change the integration yet. New implementations must use `setupPlayerAsync`.

---

## What you need

1. The player script and stylesheet (`avvpl-player.js` and `avvpl-player.css`), **or** the npm package `@streaming/avvpl-agamo`.
2. A container element in the page (`id` or a DOM node).
3. A valid **`playerLicenceKey`**. Without it, the player cannot start.

At least one of `id` or `rootElement` is required, plus `playerLicenceKey`.

---

## Recommended: `setupPlayerAsync`

`setupPlayerAsync` returns a **Promise**. After a successful licence check it resolves when:

- config merge finished, and
- the real player instance is ready.

Then you can call any player method (`on`, `load`, `playVideo`, …) on the resolved object.

Licence codes **350** (missing key), **351**, and **352** still show an error message in the container. For those codes the Promise **resolves** an error emitter with `on`, `once`, and `off`. Call `player.on("error", …)` (or `once`) to receive the stored error payload. Each new subscriber is invoked once. The resolved object is not a player: `load` and the rest of the player API are absent, and `.catch` / `try/catch` do not run for these three codes.

Other setup failures still **reject**: a missing container, and licence failures whose code is not 350, 351, or 352. The container still shows an error when one is rendered.

### Script tag (CDN)

```html
<div id="playercontainer"></div>
<link rel="stylesheet" href="https://avplayer.sportradar.com/agamo/latest/avvpl-player.css" />
<script src="https://avplayer.sportradar.com/agamo/latest/avvpl-player.js"></script>
<script>
  const config = {
    id: "playercontainer",
    playerLicenceKey: "YOUR_LICENCE_KEY",
    // configUrl: "https://example.com/player-config.json", // optional
  };

  avvpl
    .setupPlayerAsync(config)
    .then((player) => {
      player.on("error", (error) => {
        console.error("Player error", error);
      });
      if (typeof player.load !== "function") {
        return; // licence 350, 351, or 352 — error emitter only
      }
      player.on("loaded", () => {
        console.log("Player is ready");
      });
      player.load("https://example.com/stream.m3u8");
    })
    .catch((error) => {
      console.error("Player setup failed:", error.message);
    });
</script>
```

### Async / await

```javascript
try {
  const player = await avvpl.setupPlayerAsync(config);
  player.on("error", (error) => console.error("Player error", error));
  if (typeof player.load !== "function") {
    return; // licence 350, 351, or 352 — error emitter only
  }
  player.on("loaded", () => console.log("Player is ready"));
  player.load("https://example.com/stream.m3u8");
} catch (error) {
  console.error("Player setup failed:", error.message);
}
```

### npm

```javascript
import { setupPlayerAsync } from "@streaming/avvpl-agamo";
import "@streaming/avvpl-agamo/style";

const player = await setupPlayerAsync({
  id: "playercontainer",
  playerLicenceKey: "YOUR_LICENCE_KEY",
});

player.load("https://example.com/stream.m3u8");
```

Do **not** use `new` with `setupPlayerAsync`. Call it as a function.

---

## Legacy: `setupPlayer` (backward compatibility only)

`setupPlayer` is kept so existing pages do not break. It is **not** the recommended API.

Behaviour:

- It returns **immediately** (not a Promise). The object is a **proxy** until the real player is created.
- Before the real player exists, only these methods are queued: `on`, `once`, `load`, `showTimelineMarkers`, `setBrowserAutoplayPolicy`, `remove`. Other methods may not work yet.
- If `playerLicenceKey` is missing (350), it returns `{ on, once, off }` that replays `"error"`. `player.on` is safe to call.
- Licence check failures are shown in the container. They are not thrown. Codes 351 and 352 replay `"error"` on the proxy that was already returned. `setupPlayerAsync` resolves an error emitter for codes 350, 351, and 352; its other setup failures still reject.
- `new avvpl.setupPlayer(config)` still works for old pages, but new code should call `setupPlayer(config)` without `new` if you must stay on this API.

```javascript
const player = avvpl.setupPlayer(config);
if (!player) {
  return; // missing licence key
}
player.on("loaded", () => console.log("Player is ready"));
player.load("https://example.com/stream.m3u8");
```

Migrate to `setupPlayerAsync` when you can, so you get a real instance and proper error handling.

---

## Required config (minimum)

```javascript
{
  id: "playercontainer",          // or rootElement: myDiv
  playerLicenceKey: "YOUR_KEY"
}
```

Optional but common:

- `configUrl` — JSON config merged with the inline object
- `streamUrl` — stream to play (or resolve via stream-access in the remote config)
- `loglevel` — `0` quiet … higher = more logs

Full option list: product config documentation / `PlayerConfig`.

---

## Loading a stream

After setup succeeds:

```javascript
player.load("https://example.com/stream.m3u8");
```

You can also wait for the `loaded` event before calling `load`. If you call `load` before handlers are registered, the player queues it.

---

## Removing the player

```javascript
player.remove();
```

---

## Multiple players on one page

Create a separate container and a separate `setupPlayerAsync` call for each instance. Each player must have its own `id` (or `rootElement`).

---

## Why `setupPlayerAsync` is recommended

| | `setupPlayerAsync` | `setupPlayer` (legacy) |
|---|---|---|
| When you get a handle | After licence check: player on success, error emitter for 350/351/352 | Immediately (proxy) |
| Errors | 350, 351, 352 resolve an error emitter (`on("error")`); other setup failures reject | 350 returns an error emitter; 351 and 352 replay `"error"` on the proxy. Shown in the container; not thrown |
| Methods | Full API on the resolved instance | Only a small queue until ready |
| New integrations | **Use this** | Compatibility only |
