Skip to content

Getting Started

Terminal window
bun add @capgo/capacitor-youtube-player
bunx cap sync
import { YoutubePlayer } from '@capgo/capacitor-youtube-player';

Initialize a new YouTube player instance.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.initialize({
playerId: 'my-player',
videoId: 'dQw4w9WgXcQ',
playerSize: { width: 640, height: 360 },
privacyEnhanced: true
});

Destroy a player instance and free resources.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.destroy({} as PlayerIdOptions);

Stop video playback and cancel loading. Use this sparingly - pauseVideo() is usually preferred.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.stopVideo({} as PlayerIdOptions);

Play the currently cued or loaded video. Final player state will be PLAYING (1).

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.playVideo({} as PlayerIdOptions);

Pause the currently playing video. Final player state will be PAUSED (2), unless already ENDED (0).

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.pauseVideo({} as PlayerIdOptions);

Seek to a specific time in the video. If player is paused, it remains paused. If playing, continues playing.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.seekTo({} as SeekToOptions);

Load and play a video by its YouTube ID.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.loadVideoById({} as VideoByIdMethodOptions);

Cue a video by ID without playing it. Loads thumbnail and prepares player, but doesn’t request video until playVideo() called.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.cueVideoById({} as VideoByIdMethodOptions);

Load and play a video by its full URL.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.loadVideoByUrl({} as VideoByUrlMethodOptions);

Cue a video by URL without playing it.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.cueVideoByUrl({} as VideoByUrlMethodOptions);

Cue a playlist without playing it. Loads playlist and prepares first video.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.cuePlaylist({} as PlaylistMethodOptions);

Load and play a playlist.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.loadPlaylist({} as PlaylistMethodOptions);

Play the next video in the playlist.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.nextVideo({} as PlayerIdOptions);

Play the previous video in the playlist.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.previousVideo({} as PlayerIdOptions);

Play a specific video in the playlist by index.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.playVideoAt({} as PlayVideoAtOptions);

Mute the player audio.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.mute({} as PlayerIdOptions);

Unmute the player audio.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.unMute({} as PlayerIdOptions);

Check if the player is currently muted.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.isMuted({} as PlayerIdOptions);

Set the player volume level.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.setVolume({} as SetVolumeOptions);

Get the current player volume level. Returns volume even if player is muted.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getVolume({} as PlayerIdOptions);

Set the player dimensions in pixels.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.setSize({} as SetSizeOptions);

Get the current playback rate.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getPlaybackRate({} as PlayerIdOptions);

Set the playback speed.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.setPlaybackRate({} as SetPlaybackRateOptions);

Get list of available playback rates for current video.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getAvailablePlaybackRates({} as PlayerIdOptions);

Enable or disable playlist looping. When enabled, playlist will restart from beginning after last video.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.setLoop({} as SetLoopOptions);

Enable or disable playlist shuffle.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.setShuffle({} as SetShuffleOptions);

Get the fraction of the video that has been buffered. More reliable than deprecated getVideoBytesLoaded/getVideoBytesTotal.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getVideoLoadedFraction({} as PlayerIdOptions);

Get the current state of the player.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getPlayerState({} as PlayerIdOptions);

Get event states for all active players. Useful for tracking multiple player instances.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getAllPlayersEventsState();

Get the current playback position in seconds.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getCurrentTime({} as PlayerIdOptions);

Toggle fullscreen mode on or off.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.toggleFullScreen({} as ToggleFullScreenOptions);

Get the current playback quality.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getPlaybackQuality({} as PlayerIdOptions);

Set the suggested playback quality. Actual quality may differ based on network conditions.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.setPlaybackQuality({} as SetPlaybackQualityOptions);

Get list of available quality levels for current video.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getAvailableQualityLevels({} as PlayerIdOptions);

Get the duration of the current video in seconds.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getDuration({} as PlayerIdOptions);

Get the YouTube.com URL for the current video.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getVideoUrl({} as PlayerIdOptions);

Get the embed code for the current video. Returns HTML iframe embed code.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getVideoEmbedCode({} as PlayerIdOptions);

Get array of video IDs in the current playlist.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getPlaylist({} as PlayerIdOptions);

Get the index of the currently playing video in the playlist.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getPlaylistIndex({} as PlayerIdOptions);

Get the iframe DOM element for the player. Web platform only.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.getIframe({} as PlayerIdOptions);

Add an event listener to the player. Web platform only.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
YoutubePlayer.addEventListener({
playerId: 'my-player',
eventName: 'onStateChange',
listener: (event) => {
console.log('Player state:', event.data);
},
});

Remove an event listener from the player. Web platform only.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.removeEventListener({} as PlayerEventListenerOptions<TEvent>);
export interface PlayerIdOptions {
playerId: string;
}
export interface SeekToOptions extends PlayerIdOptions {
playerId: string;
seconds: number;
allowSeekAhead: boolean;
}
export interface VideoByIdMethodOptions extends PlayerIdOptions {
playerId: string;
options: IVideoOptionsById;
}
export interface VideoByUrlMethodOptions extends PlayerIdOptions {
playerId: string;
options: IVideoOptionsByUrl;
}
export interface PlaylistMethodOptions extends PlayerIdOptions {
playerId: string;
playlistOptions: IPlaylistOptions;
}
export interface PlayVideoAtOptions extends PlayerIdOptions {
playerId: string;
index: number;
}
export interface SetVolumeOptions extends PlayerIdOptions {
playerId: string;
volume: number;
}
export interface SetSizeOptions extends PlayerIdOptions {
playerId: string;
width: number;
height: number;
}
export interface SetPlaybackRateOptions extends PlayerIdOptions {
playerId: string;
suggestedRate: number;
}
export interface SetLoopOptions extends PlayerIdOptions {
playerId: string;
loopPlaylists: boolean;
}
export interface SetShuffleOptions extends PlayerIdOptions {
playerId: string;
shufflePlaylist: boolean;
}
export interface ToggleFullScreenOptions extends PlayerIdOptions {
playerId: string;
isFullScreen: boolean | null | undefined;
}

This page is generated from the plugin’s src/definitions.ts. Re-run the sync when the public API changes upstream.