Skip to main content

Embed the player on your website

You embed the nanoStream Player in one of two ways: with the JavaScript API (nanoplayer.5.min.js) for full control, or with a ready-hosted iframe for the quickest result. Both use MOQ with automatic H5Live fallback and work on desktop and mobile browsers.

Which option to use​

OptionBest forLimits
JavaScript APIProduction sites, custom UI, events, stream switching, token workflowsYou write a few lines of JavaScript
Hosted iframe embedCMS pages, quick tests, sites where you can't add scriptsConfiguration via URL parameters only; no domain-bound (referrer-restricted) tokens
Your own player page in an iframeIsolating the player from the rest of your pageYou host the player page yourself

For most sites, embedding the player directly in the page with the JavaScript API is the best choice. Use an iframe only when your setup requires it.

tip

You don't have to write the snippet from scratch. The nanoStream Dashboard shows ready-to-copy code snippets for every stream. In the Builder Hub, Share and embed gives you an embed snippet too, and Developer mode shows the player code for your stream on every tool page that plays it.

What you need​

  • A live stream and either its stream group ID, its stream name (XXXXX-YYYYY) or its stream ID. You find them in the Builder Hub, the nanoStream Dashboard or the nanoStream REST API.
  • For token-protected organisations: a playback token. See Playback tokens.

Embed with the JavaScript API​

  1. Add a container element and load the player script:

    <div id="playerDiv"></div>
    <script src="https://demo.nanocosmos.de/nanoplayer/api/release/nanoplayer.5.min.js"></script>
  2. Create a configuration. With a stream group (recommended, works for single streams and ABR):

    <script>
    var config = {
    source: {
    group: {
    id: '<STREAM_GROUP_ID>', // your stream group ID
    startQuality: 'medium' // optional: high | medium-high | medium | medium-low | low
    }
    },
    playback: {
    autoplay: true,
    automute: true,
    muted: false,
    faststart: true,
    latencyControlMode: 'balancedadaptive'
    }
    };
    </script>
  3. Create the player and call setup() once the page has loaded:

    <script>
    var player;
    document.addEventListener('DOMContentLoaded', function () {
    player = new NanoPlayer('playerDiv');
    player.setup(config).then(function (config) {
    console.log('setup success');
    }, function (error) {
    console.log(error.message);
    });
    });
    </script>
  4. Open the page while the stream is live. The player picks MOQ or H5Live automatically.

info

Load nanoplayer.5.min.js, not a 4.x script. Check that the <script> tag is closed correctly: a missing > stops the script from loading and NanoPlayer is undefined.

Source options​

Use exactly one of these source forms:

Source formConfigSince
Stream groupsource.group: { id, startQuality, security }4.18.0
Stream name with nanocosmos defaultssource.defaults.service: 'bintu' + source.entries[].h5live.rtmp.streamname4.13.0
Stream IDsource.entries[].bintu.streamid
Custom server URLssource.entries[].h5live.rtmp + h5live.server (include webtransport for MOQ)

Stream name with defaults:

var config = {
source: {
defaults: { service: 'bintu' },
entries: [
{ h5live: { rtmp: { streamname: 'XXXXX-YYYYY' } } } // your stream name
]
},
playback: { autoplay: true, automute: true, muted: false, faststart: true }
};

Stream ID:

var config = {
source: {
entries: [
{ bintu: { streamid: '<STREAM_ID>' } } // stream ID, not the stream name
]
}
};
note

With a group source, the player chooses the ABR rule and start rendition itself. Set the start rendition with group.startQuality; don't add source.startIndex or source.options.adaption next to group. Custom server URLs are only needed for special setups; see Playback technologies.

All configuration keys are listed in the Player configuration reference.

Token-protected streams​

If secure playback is enabled for your organisation, add the playback token as a security object to the group or entry:

var config = {
source: {
group: {
id: '<STREAM_GROUP_ID>',
security: { jwtoken: '<YOUR_JWT>' }
}
}
};

Create tokens on your server and hand them to the page; never put API keys in the page. See Playback tokens.

Frameworks and CMS​

  • React: load the script in public/index.html, render <div id="playerDiv" />, and create the player in useEffect(() => { ... }, []) (or componentDidMount). Sample: https://github.com/nanocosmos/nanoplayer-react-sample

  • Vue.js: load the script in index.html, put the config in data(), and create new window.NanoPlayer('playerDiv') in mounted(). Sample: https://github.com/nanocosmos/nanoplayer-vuejs-sample

  • WordPress: add a Custom HTML block with the script, the config and the setup call. Wrap playerDiv in a sized container, otherwise the player isn't visible:

    <div style="width: 480px; height: 360px; overflow: hidden; position: relative;">
    <div id="playerDiv"></div>
    </div>

Embed with the hosted iframe​

The hosted embed page (version 2.0.0) runs the latest nanoStream Player 5.x. You configure it entirely through URL parameters.

<iframe
src="https://demo.nanocosmos.de/nanoplayer/embed/2.0.0/nanoplayer.html?group.id=<STREAM_GROUP_ID>"
frameborder="0" allowfullscreen width="1280" height="720">
</iframe>

The src must include a source parameter (group.id or entry.rtmp.streamname). Without one, the player loads but has nothing to play. Keep allowfullscreen if viewers should be able to go fullscreen.

URL parameters​

Parameters map to the player config in dot notation. Leave out the source. prefix; keep every other object name (playback., style., …).

ParameterExampleNotes
group.idgroup.id=<STREAM_GROUP_ID>Stream group source
group.security.jwtokengroup.security.jwtoken=<YOUR_JWT>Token for a secure group
group.startQualitygroup.startQuality=mediumStart rendition for a group
entry.rtmp.streamnameentry.rtmp.streamname=XXXXX-YYYYYSingle stream; entry2., entry3. for more entries
entry.info.bitrateentry.info.bitrate=1500Bitrate (kbit/s) for entry-based ABR
startIndexstartIndex=2Start entry for entry-based ABR
options.adaption.ruleoptions.adaption.rule=deviationOfMean2ABR switching rule for entries
defaults.servicedefaults.service=bintuUse nanocosmos defaults
playback.autoplay, playback.automute, playback.mutedplayback.autoplay=true
playback.faststart, playback.metadataplayback.metadata=true
playback.latencyControlModeplayback.latencyControlMode=balancedadaptiveclassic, balancedadaptive, fastadaptive
playback.timeouts.bufferingplayback.timeouts.buffering=20Seconds
style.fullScreenControl, style.displayMutedAutoplaystyle.fullScreenControl=true
style.backgroundColor, style.posterstyle.backgroundColor=blackPoster is an image URL
tweaks.buffer.*tweaks.buffer.limit=1.7Pass the complete set
metrics.accountId, metrics.accountKeyPlayer metrics
titletitle=My%20EventPage title of the embed
info

Booleans must be the strings true or false. Since embed 2.0.0, 1 and 0 are not converted. If you used playback.metadata=1 with embed 1.x, change it to playback.metadata=true.

Example with ABR over three entries and muted autoplay:

https://demo.nanocosmos.de/nanoplayer/embed/2.0.0/nanoplayer.html?entry.rtmp.streamname=XXXXX-AAAAA&entry.info.bitrate=1500&entry2.rtmp.streamname=XXXXX-BBBBB&entry2.info.bitrate=800&entry3.rtmp.streamname=XXXXX-CCCCC&entry3.info.bitrate=200&options.adaption.rule=deviationOfMean2&startIndex=2&playback.autoplay=true&playback.automute=true
warning

Tokens restricted to a domain (referrer binding) don't work with the hosted iframe, because playback requests come from the embed page's domain, not yours. Use the JavaScript API on your own page instead.

Host your own player page in an iframe​

  1. Build a page on your domain with the JavaScript API, as above.

  2. Embed that page on the distributing page:

    <iframe id="ifvideo" width="640" height="480" scrolling="no" frameborder="0" allowfullscreen
    src="https://yourdomain.com/playerpage.html">
    </iframe>

Test with the demo player​

Before embedding, check that the stream plays in the hosted demo player:

https://demo.nanocosmos.de/nanoplayer/release/nanoplayer.html?group.id=<STREAM_GROUP_ID>

For a token-protected stream, the token parameter must match the source form: with group.id= use group.security.jwtoken=<YOUR_JWT>. With any other prefix the player ignores the token and playback fails, even though the token is valid.

Troubleshooting​

SymptomCause and fix
Nothing appears, NanoPlayer is not definedThe script didn't load. Check the URL and that the <script> tag is closed.
Player stays black or shows "connecting"The stream isn't live, or the ID or name is wrong. Watch onError for codes 2001 and 2002, and onPause with reason streamnotfound. See Events & error codes.
No autoplay with sound on mobileBrowsers block autoplay with sound. Keep automute: true so the player starts muted.
Secure stream doesn't playMissing, expired or mis-scoped token, or the token param doesn't match the source form. See Playback tokens.
Player invisible in WordPressWrap playerDiv in a sized container.

Next steps​

tip

Try it in the Builder Hub: start an Instant Live Stream and watch it in the nanoStream Player in seconds. See the Builder Hub guide. Start for Free