twitch-player
Version:
A Typescript wrapper for the Twitch embeddable, interactive media player.
229 lines • 12.2 kB
JavaScript
import { Embed } from './Embed';
import { TwitchPlayer } from './video/TwitchPlayer';
/**
* A TS wrapper for the Twitch interactive media player, that can also include the chat.
*/
export class TwitchEmbed {
/**
* Creates a new TwitchEmbed instance.
* @param divId The div HTML element where the player will appear.
* @param options The player initialization options.
*/
constructor(divId, options) {
this._divId = divId;
this._twitchEmbedOptions = options;
try {
if (window.Twitch && window.Twitch.Embed) {
this._embed = new window.Twitch.Embed(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');
this._embed = new Embed(divId, options);
}
this._player = TwitchPlayer.FromPlayer(this._embed.getPlayer());
}
catch (e) {
this._embed = new Embed(divId, options);
this._player = TwitchPlayer.FromPlayer(this._embed.getPlayer());
console.exception('Player was created using the static, packaged file. ' +
'Please add the Twitch.Embed script to *index.html*, if you want to download the script directly from Twitch.', e);
}
}
/**
* The div HTML element where the player will appear.
*/
get divId() {
return this._divId;
}
/**
* The player options that were provided when the instance was constructed.
*/
get twitchEmbedOptions() {
return this._twitchEmbedOptions;
}
/**
* 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._embed) === null || _a === void 0 ? void 0 : _a.addEventListener(event.toString(), callback);
}
/**
* Disables the captions for the content that is currently playing.
* @deprecated Use with caution, not inculded in the official Twitch documentation.
*/
disableCaptions() {
this._embed.disableCaptions();
}
/**
* Enables the captions for the content that is currently playing.
* @deprecated Use with caution, not inculded in the official Twitch documentation.
*/
enableCaptions() {
this._embed.enableCaptions();
}
/**
* 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() {
return this._embed.getPlaybackStats();
}
/**
* Returns the channel’s identifier. Works only for live streams, not VODs.
*/
getChannelId() {
return this._embed.getChannelId();
}
/**
* Returns the name of the collection currently being played.
*/
getCollection() {
return this._embed.getCollection();
}
/**
* Sets the channel to be played.
* @param channelId The selected channel's identifier.
*/
setChannelId(channelId) {
this._embed.setChannelId(channelId);
}
/**
* Returns the Twitch.Player object.
* Can be used for more fine-grained control of the player.
*/
getPlayer() {
return this._player;
}
/**
* Pauses the player.
*/
pause() {
this._embed.pause();
}
/**
* Begins playing the specified video.
*/
play() {
this._embed.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) {
this._embed.seek(timestamp);
}
/**
* Sets the channel to be played.
* @param channel The selected channel.
*/
setChannel(channel) {
this._embed.setChannel(channel);
}
/**
* 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) {
this._embed.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) {
this._embed.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) {
this._embed.setVideo(videoID, timestamp);
}
/**
* Returns true if the player is muted; otherwise, false.
*/
getMuted() {
return this._embed.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) {
this._embed.setMuted(muted);
}
/**
* Returns the volume level, a value between 0.0 and 1.0.
*/
getVolume() {
return this._embed.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) {
this._embed.setVolume(volumeLevel);
}
/**
* Returns the channel’s name. Works only for live streams, not VODs.
*/
getChannel() {
return this._embed.getChannel();
}
/**
* Returns the current video’s timestamp, in seconds. Works only for VODs, not live streams.
*/
getCurrentTime() {
return this._embed.getCurrentTime();
}
/**
* Returns the duration of the video, in seconds. Works only for VODs,not live streams.
*/
getDuration() {
return this._embed.getDuration();
}
/**
* Returns true if the live stream or VOD has ended; otherwise, false.
*/
getEnded() {
return this._embed.getEnded();
}
/**
* Returns the available video qualities. For example, chunked (pass-through of the original source).
*/
getQualities() {
return this._embed.getQualities();
}
/**
* Returns the current quality of video playback.
*/
getQuality() {
return this._embed.getQuality();
}
/**
* Returns the video ID. Works only for VODs, not live streams.
*/
getVideo() {
return this._embed.getVideo();
}
/**
* Returns true if the video is paused; otherwise, false. Buffering or seeking is considered playing.
*/
isPaused() {
return this._embed.isPaused();
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiVHdpdGNoRW1iZWQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvbGliL2V2ZXJ5dGhpbmcvVHdpdGNoRW1iZWQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLEtBQUssRUFBRSxNQUFNLFNBQVMsQ0FBQztBQUloQyxPQUFPLEVBQUUsWUFBWSxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFHcEQ7O0dBRUc7QUFDSCxNQUFNLE9BQU8sV0FBVztJQXVCdEI7Ozs7T0FJRztJQUNILFlBQVksS0FBYSxFQUFFLE9BQTJCO1FBQ3BELElBQUksQ0FBQyxNQUFNLEdBQUcsS0FBSyxDQUFDO1FBQ3BCLElBQUksQ0FBQyxtQkFBbUIsR0FBRyxPQUFPLENBQUM7UUFDbkMsSUFBSTtZQUNGLElBQVUsTUFBTyxDQUFDLE1BQU0sSUFBVSxNQUFPLENBQUMsTUFBTSxDQUFDLEtBQUssRUFBRTtnQ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