NanoPlayer v5
NanoPlayer
NanoPlayer (H5Live) Public API Class 5.1.0
Kind: global class Version: 5.1.0
new NanoPlayer(playerDivId)
The constructor. The source can be loaded via script tag, AMD (requirejs) or CommonJS
| Param | Type | Description |
|---|---|---|
| playerDivId | string | The div element the player will be embedded into. |
Example
<!-- Example: load player with new video element into playerDiv -->
<div id="playerDiv"></div>
<script type="text/javascript" src="nanoplayer.5.min.js"></script>
<script type="text/javascript">
var player;
var config = {
"source": {
"entries": [
{
"h5live": {
// your rtmp stream
"rtmp": {
"url": "rtmp://bintu-play.nanocosmos.de/play",
"streamname": "XXXXX-YYYYY"
},
"server": {
"websocket": "wss://bintu-h5live.nanocosmos.de:443/h5live/stream.mp4",
"hls": "https://bintu-h5live.nanocosmos.de:443/h5live/http/playlist.m3u8",
"progressive": "https://bintu-h5live.nanocosmos.de:443/h5live/http/stream.mp4",
"webtransport": "https://bintu-h5live.nanocosmos.de:443/h5live/stream/stream.mp4" // important for MOQ playback
}
}
}
]
}
};
function initPlayer() {
player = new NanoPlayer('playerDiv');
player.setup(config).then(function (config) {
console.log('setup ok with config: ' + JSON.stringify(config));
}, function (error) {
console.log(error);
});
}
// load player from playerDiv
document.addEventListener('DOMContentLoaded', function () {
initPlayer();
});
</script>
Example
<!-- Example: load player with existing html video element -->
<div id="playerDiv">
<video id="myPlayer"></video>
<!-- HLS PLAYBACK ONLY uses 2 video elements for playback if more than one stream is configured, required for seamless stream switching -->
<video id="myPlayer2"></video>
</div>
<script>
var player;
var config = {
"source": {
"entries": [
{
"h5live": {
// your rtmp stream
"rtmp": {
"url": "rtmp://bintu-play.nanocosmos.de/play",
"streamname": "XXXXX-YYYYY"
},
"server": {
"websocket": "wss://bintu-h5live.nanocosmos.de:443/h5live/stream.mp4",
"hls": "https://bintu-h5live.nanocosmos.de:443/h5live/http/playlist.m3u8",
"progressive": "https://bintu-h5live.nanocosmos.de:443/h5live/http/stream.mp4",
"webtransport": "https://bintu-h5live.nanocosmos.de:443/h5live/stream/stream.mp4" // important for MOQ playback
}
}
}
]
},
"playback": {
"videoId": ["myPlayer", "myPlayer2"]
}
};
function initPlayer() {
player = new NanoPlayer('playerDiv');
player.setup(config).then(function (config) {
console.log('setup ok with config: ' + JSON.stringify(config));
}, function (error) {
console.log(error);
});
}
document.addEventListener('DOMContentLoaded', function () {
initPlayer();
});
</script>
Example
<!-- Example: load player with require.js -->
<script type="text/javascript" src="require.js"></script>
<script type="text/javascript">
var player;
requirejs.config({
paths: {
// loads the player ...
// for a local copy of the minified player use a relative path e.g. 'js/nanoplayer.5.min'
// if 'baseUrl' is defined a local path have to be relative to the base path
nanoplayer: '//demo.nanocosmos.de/nanoplayer/api/release/nanoplayer.5.min.js'
},
waitSeconds: 20, // timeout for loading modules
});
require('nanoplayer', function() {
initPlayer();
});
</script>
nanoPlayer.version : string
The version of the player.
Kind: instance property of NanoPlayer
nanoPlayer.coreversion : string
The version of the core.
Kind: instance property of NanoPlayer
nanoPlayer.viewversion : string
The version of the view.
Kind: instance property of NanoPlayer
nanoPlayer.type : string
The type of the player.
Kind: instance property of NanoPlayer
nanoPlayer.id : string
The unique id of the player.
Kind: instance property of NanoPlayer
nanoPlayer.capabilities : Array.<string>
The supported tech names of the player.
Kind: instance constant of NanoPlayer
nanoPlayer.setup(config) ⇒ Promise.<(config|error)>
Initializes the player with a given config object.
Kind: instance method of NanoPlayer
See: config
| Param | Type | Description |
|---|---|---|
| config | config | The config object for the player including sources, events, styles. |
Example
// player instance of NanoPlayer
player.setup(config).then(function (config) {
console.log('setup ok with config: ' + JSON.stringify(config));
}, function (error) {
console.log(error);
});
nanoPlayer.destroy()
Cleans up the player and removes all nested elements from the container div.
Kind: instance method of NanoPlayer
Example
// player instance of NanoPlayer
player.destroy();
player.setup(config);
nanoPlayer.play()
Plays the player.
Kind: instance method of NanoPlayer
Example
// player instance of NanoPlayer
player.play();
nanoPlayer.pause()
Pauses the player.
Kind: instance method of NanoPlayer
Example
// player instance of NanoPlayer
player.pause();
nanoPlayer.mute()
Mutes the player.
Kind: instance method of NanoPlayer
Example
// player instance of NanoPlayer
player.mute();
nanoPlayer.unmute()
Unmutes the player.
Kind: instance method of NanoPlayer
Example
// player instance of NanoPlayer
player.unmute();
nanoPlayer.setVolume(volume)
Sets the volume of the player.
See here for more informations.
Kind: instance method of NanoPlayer
| Param | Type | Description |
|---|---|---|
| volume | number | The volume to set in a range from 0.0 to 1.0. |
Example
// player instance of NanoPlayer
player.setVolume(0.3);
nanoPlayer.updateSource(source) ⇒ Promise.<(config|error)>
Updates the source of the player.
Kind: instance method of NanoPlayer
See: config
| Param | Type | Default | Description |
|---|---|---|---|
| source | object | The object to configure the source to play, one of the following properties have to be set. | |
| [source.entries] | Array.<entry> | The source entries array for a set of streams. USE INSTEAD OF SOURCE.H5LIVE. Used to configure stream entries. Can have one to many 'entry' objects. Only one existing entry is similar than a single source. In this case no entries options are needed. | |
| [source.startIndex] | number | entries.length-1 | The index of the entry to start playback with. Can be in the range from 0 to 'entries.length-1'. |
| [source.options] | object | The object to configure the source entries options. | |
| [source.options.switch] | object | The object to configure the stream switch options like method etc. | |
| [source.options.switch.method] | string | "server" | The update method. Possible values are 'server' (default) and 'client'. |
| [source.options.switch.pauseOnError] | boolean | false | If set the player stops if an error occure during the stream switch. Default is false. |
| [source.options.switch.forcePlay] | boolean | true | If set the player starts playback in case the player is paused. Default is true. |
| [source.options.switch.fastStart] | boolean | true | DEPRECATED. Only if method is 'server'. Tries to accelerate the startup time of the new source. Default is true. |
| [source.options.switch.timeout] | number | 20 | The timeout for the update source request in seconds. If reached the error 4006 will thrown in the 'onUpdateSourceFail' event. Default is 20 seconds, valid range is between 5 and 30. |
| [source.options.switch.tag] | string | A custom field that can be any string like 'stream-800k' or '720p'. This tag will be returned in any completion event of the 'updateSource' request like 'onUpdateSourceSuccess', 'onUpdateSourceFail' and 'onUpdateSourceAbort'. | |
| [source.options.adaption] | object | The object to set an adaption for switching. | |
| [source.options.adaption.rule] | string | "deviationOfMean2" | The switch rule if multiple entries are defined. Possible values are 'deviationOfMean' (ABR automatic), 'deviationOfMean2' (ABR automatic) and 'none' (means only manual stream switch via 'switchStream' possible). The default depends on the source: with 3 or more entries and with a bintu stream group ( |
| [source.options.adaption.downStep] | number | 2 | The minimum number of steps during a ABR down switch ('deviationOfMean' and 'deviationOfMean2' only). The default is 2 with 3 or more entries and 1 otherwise. |
Example
var source = {
"entries": [
{
"index": 0,
"label": "high",
"tag": "this is a high quality stream",
"info": {
"bitrate": 1200,
"width": 1280,
"height": 720,
"framerate": 30
},
"hls": "",
"h5live": {
"rtmp": {
"url": "rtmp://bintu-play.nanocosmos.de/play",
"streamname": "XXXXX-YYYY1"
},
"server": {
"websocket": "wss://bintu-h5live.nanocosmos.de:443/h5live/stream.mp4",
"hls": "https://bintu-h5live.nanocosmos.de:443/h5live/http/playlist.m3u8",
"progressive": "https://bintu-h5live.nanocosmos.de:443/h5live/http/stream.mp4",
"webtransport": "https://bintu-h5live.nanocosmos.de:443/h5live/stream/stream.mp4" // important for MOQ playback
},
"token": "",
"security": {}
},
"bintu": {}
},
{
"index": 1,
"label": "medium",
"tag": "this is a medium quality stream",
"info": {
"bitrate": 800,
"width": 864,
"height": 480,
"framerate": 30
},
"hls": "",
"h5live": {
"rtmp": {
"url": "rtmp://bintu-play.nanocosmos.de/play",
"streamname": "XXXXX-YYYY2"
},
"server": {
"websocket": "wss://bintu-h5live.nanocosmos.de:443/h5live/stream.mp4",
"hls": "https://bintu-h5live.nanocosmos.de:443/h5live/http/playlist.m3u8",
"progressive": "https://bintu-h5live.nanocosmos.de:443/h5live/http/stream.mp4",
"webtransport": "https://bintu-h5live.nanocosmos.de:443/h5live/stream/stream.mp4" // important for MOQ playback
},
"token": "",
"security": {}
},
"bintu": {}
},
{
"index": 2,
"label": "low",
"tag": "this is a low quality stream",
"info": {
"bitrate": 400,
"width": 426,
"height": 240,
"framerate": 15
},
"hls": "",
"h5live": {
"rtmp": {
"url": "rtmp://bintu-play.nanocosmos.de/play",
"streamname": "XXXXX-YYYY3"
},
"server": {
"websocket": "wss://bintu-h5live.nanocosmos.de:443/h5live/stream.mp4",
"hls": "https://bintu-h5live.nanocosmos.de:443/h5live/http/playlist.m3u8",
"progressive": "https://bintu-h5live.nanocosmos.de:443/h5live/http/stream.mp4",
"webtransport": "https://bintu-h5live.nanocosmos.de:443/h5live/stream/stream.mp4" // important for MOQ playback
},
"token": "",
"security": {}
},
"bintu": {}
}
],
"options": {
"adaption": {
"rule": "deviationOfMean2",
"downStep": 1
},
"switch": {
'method': 'server',
'pauseOnError': false,
'forcePlay': true,
'timeout': 20
}
},
"startIndex": 2 // lowest
};
// player instance of NanoPlayer
player.updateSource(source).then(function (config) {
console.log('update source ok with config: ' + JSON.stringify(config));
}, function (error) {
console.log(error);
});
Example
var source = {
"entries": [
{
"index": 0,
"label": "high", // optional
"tag": "this is a high quality stream", // optional
"info": { // optional
"bitrate": 1200,
"width": 1280,
"height": 720,
"framerate": 30
},
"h5live": {
// your rtmp stream
"rtmp": {
"url": "rtmp://bintu-play.nanocosmos.de/play",
"streamname": "XXXXX-YYYYY"
},
"server": {
"websocket": "wss://bintu-h5live.nanocosmos.de:443/h5live/stream.mp4",
"hls": "https://bintu-h5live.nanocosmos.de:443/h5live/http/playlist.m3u8",
"progressive": "https://bintu-h5live.nanocosmos.de:443/h5live/http/stream.mp4",
"webtransport": "https://bintu-h5live.nanocosmos.de:443/h5live/stream/stream.mp4" // important for MOQ playback
},
// optional (secure token)
"security": {
"token": 'awe456b367g4e6rm8f56hbe6gd8f5m8df6n8idf6tf8mfd68ndi',
"expires": '1519819200',
"options": '15',
"tag": 'anyTag'
}
}
}
]
};
// player instance of NanoPlayer
player.updateSource(source).then(function (config) {
console.log('update source initialized with config: ' + JSON.stringify(config));
}, function (error) {
console.log(error);
});
nanoPlayer.switchStream(index) ⇒ Promise.<(config|error)>
Switch to a stream given over source entries.
Kind: instance method of NanoPlayer
See: entry
| Param | Type | Description |
|---|---|---|
| index | number | The index of the stream in the given stream set to switch to. |
Example
// player instance of NanoPlayer
player.switchStream(1).then(function (config) {
console.log('switch stream initialized with config: ' + JSON.stringify(config));
}, function (error) {
console.log(error);
});
nanoPlayer.setAdaption(adaption)
Set a desired adaption rule or disable adaption on the fly.
Kind: instance method of NanoPlayer
See: config
| Param | Type | Description |
|---|---|---|
| adaption | object | The adaption object similar than the object 'config.source.options.adaption'. |
Example
// player instance of NanoPlayer
var adaption = {
"rule": "deviationOfMean2",
"downStep": 2
}
if (!useAdaption) {
adaption.rule = "none";
}
player.setAdaption(adaption);
nanoPlayer.requestFullscreen() ⇒ Promise.<(undefined|error)>
Request fullscreen mode for the player if not entered.
Kind: instance method of NanoPlayer
Example
// player instance of NanoPlayer
player.requestFullscreen()
.then(function (){
console.log('requestFullscreen resolved');
})
.catch(function(err) {
// error reasons can be 'denied' or 'disabled' (e.g. in audio player mode)
console.log('requestFullscreen rejected: ' + err.reason);
});
nanoPlayer.exitFullscreen() ⇒ Promise.<(undefined|error)>
Exit fullscreen mode if entered.
Kind: instance method of NanoPlayer
Example
// player instance of NanoPlayer
player.exitFullscreen()
.then(function (){
console.log('exitFullscreen resolved');
})
.catch(function(err) {
// error reasons can be 'denied' or 'disabled' (e.g. in audio player mode)
console.log('exitFullscreen rejected: ' + err.reason);
});
"onReady"
The ready event to pass in the 'config.events' object at the setup call. Fires if the player is ready to play after successful setup.
Kind: event emitted by NanoPlayer
See: config