twitch-player
Version:
A Typescript wrapper for the Twitch embeddable, interactive media player.
243 lines • 13.2 kB
JavaScript
import { Player } from './Player';
/**
* A TS wrapper for the Twitch interactive media player.
*/
export class TwitchPlayer {
/**
* Creates a new TwitchPlayer from a Player instance.
* @param player The Player instance.
* @constructor Creates a new TwitchPlayer instance.
*/
static FromPlayer(player) {
const twitchPlayer = new this();
twitchPlayer._player = player;
return twitchPlayer;
}
/**
* Creates a new TwitchPlayer given a div element identifier and some options for the player.
* @param divId The div element identiefier where the player will appear.
* @param options The player options.
* @constructor Creates a new TwitchPlayer instance.
*/
static FromOptions(divId, options) {
const twitchPlayer = new this();
try {
if (window.Twitch && window.Twitch.Player) {
twitchPlayer._player = new window.Twitch.Player(divId, options);
}
else {
console.warn('Player was created using the static file, from inside the package. ' +
'Please add the Twitch.Embed script to *index.html*, if you want to download the script directly from Twitch.');
twitchPlayer._player = new Player(divId, options);
}
}
catch (e) {
console.exception('Player was created using the static file, from inside the package. ' +
'Please add the Twitch.Player script to *index.html*, if you want to download the script directly from Twitch.', e);
twitchPlayer._player = new Player(divId, options);
}
return twitchPlayer;
}
/**
* Disables the captions for the content that is currently playing.
* @deprecated Use with caution, not inculded in the official Twitch documentation.
*/
disableCaptions() {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.disableCaptions();
}
/**
* Enables the captions for the content that is currently playing.
* @deprecated Use with caution, not inculded in the official Twitch documentation.
*/
enableCaptions() {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.enableCaptions();
}
/**
* Pauses the player.
*/
pause() {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.pause();
}
/**
* Begins playing the specified video.
*/
play() {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.play();
}
/**
* Seeks to the specified timestamp (in seconds) in the video and resumes playing if paused. Does not work for live streams.
* @param timestamp The specified timestamp (in seconds).
*/
seek(timestamp) {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.seek(timestamp);
}
/**
* Sets the channel to be played.
* @param channel The selected channel.
*/
setChannel(channel) {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.setChannel(channel);
}
/**
* Sets the channel to be played.
* @param channelId The selected channel's identifier.
*/
setChannelId(channelId) {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.setChannelId(channelId);
}
/**
* Retrieves the playback statistics for this player.
* The statistics contain information such as video FPS, resolution, latency and dropped frames.
* @deprecated Use with caution, not inculded in the official Twitch documentation.
*/
getPlaybackStatistics() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getPlaybackStats();
}
/**
* Sets the collection to be played.
* Optionally also specifies the video within the collection, from which to start playback.
* If a video ID is not provided here or the specified video is not part of the collection,
* playback starts with the first video in the collection.
* @param collectionId The identifier for the collection.
* @param videoId The identifier for the video.
*/
setCollection(collectionId, videoId) {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.setCollection(collectionId, videoId);
}
/**
* Sets the quality of the video. quality should be a string value returned by getQualities.
* @param quality The quality to be set.
*/
setQuality(quality) {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.setQuality(quality);
}
/**
* Sets the video to be played to be played and starts playback at timestamp (in seconds).
* @param videoID The identifier of the video to be played.
* @param timestamp The spot where the playback will be started (in seconds).
*/
setVideo(videoID, timestamp) {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.setVideo(videoID, timestamp);
}
/**
* Returns true if the player is muted; otherwise, false.
*/
getMuted() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getMuted();
}
/**
* If true, mutes the player; otherwise, unmutes it. This is independent of the volume setting.
* @param muted If true, player will be muted. Otherwise, it will be unmuted.
*/
setMuted(muted) {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.setMuted(muted);
}
/**
* Returns the volume level, a value between 0.0 and 1.0.
*/
getVolume() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getVolume();
}
/**
* Sets the volume to the specified volume level, a value between 0.0 and 1.0.
* @param volumeLevel A number between 0 and 1.
*/
setVolume(volumeLevel) {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 : _a.setVolume(volumeLevel);
}
/**
* Returns the channel’s name. Works only for live streams, not VODs.
*/
getChannel() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getChannel();
}
/**
* Returns the channel’s identifier. Works only for live streams, not VODs.
*/
getChannelId() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getChannelId();
}
/**
* Returns the current video’s timestamp, in seconds. Works only for VODs, not live streams.
*/
getCurrentTime() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getCurrentTime();
}
/**
* Returns the duration of the video, in seconds. Works only for VODs,not live streams.
*/
getDuration() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getDuration();
}
/**
* Returns true if the live stream or VOD has ended; otherwise, false.
*/
getEnded() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getEnded();
}
/**
* Returns the available video qualities. For example, chunked (pass-through of the original source).
*/
getQualities() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getQualities();
}
/**
* Returns the current quality of video playback.
*/
getQuality() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getQuality();
}
/**
* Returns the video ID. Works only for VODs, not live streams.
*/
getVideo() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getVideo();
}
/**
* Returns true if the video is paused; otherwise, false. Buffering or seeking is considered playing.
*/
isPaused() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.isPaused();
}
/**
* Returns the name of the collection currently being played.
*/
getCollection() {
var _a;
return (_a = this._player) === null || _a === void 0 ? void 0 : _a.getCollection();
}
/**
* Adds an event listener for the given event.
* @param event The event type to which the listener should react.
* @param callback The logic that should happen when the listener fires.
*/
addEventListener(event, callback) {
var _a;
(_a = this._player) === null || _a === void 0 ? void 0 :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