Skip to content

Getting Started

GitHub

You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:

Terminal window
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-youtube-player` plugin in my project.

If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:

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

Fix YouTube Referer Blocking in the Main WebView

Section titled “Fix YouTube Referer Blocking in the Main WebView”

If YouTube works inside the plugin but fails when the same app loads YouTube pages, embeds, or APIs through Capacitor’s main WebView, enable patchRefererHeader in your Capacitor config.

When enabled, the plugin patches Capacitor during sync/update so intercepted YouTube requests include a valid Referer header.

{
"plugins": {
"YoutubePlayer": {
"patchRefererHeader": true,
"refererHeader": "https://www.youtube.com"
}
}
}
  • Only youtube.com, youtube-nocookie.com, and youtu.be requests are affected.
  • Requests that already define a Referer header keep their original value.
  • refererHeader is optional and defaults to https://www.youtube.com.
  • Supported on Capacitor 8.x for installed iOS and Android platforms.

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';
const result = await YoutubePlayer.destroy({ playerId: 'player-id-123' });
console.log(result);

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

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.stopVideo({ playerId: 'player-id-123' });
console.log(result);

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

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.playVideo({ playerId: 'player-id-123' });
console.log(result);

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

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.pauseVideo({ playerId: 'player-id-123' });
console.log(result);

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';
const result = await YoutubePlayer.seekTo({
playerId: 'player-id-123',
seconds: 10,
allowSeekAhead: true,
});
console.log(result);

Load and play a video by its YouTube ID.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.loadVideoById({
playerId: 'player-id-123',
options: { videoId: 'video-id-123' },
});
console.log(result);

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';
const result = await YoutubePlayer.cueVideoById({
playerId: 'player-id-123',
options: { videoId: 'video-id-123' },
});
console.log(result);

Load and play a video by its full URL.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.loadVideoByUrl({
playerId: 'player-id-123',
options: { mediaContentUrl: 'https://example.com' },
});
console.log(result);

Cue a video by URL without playing it.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.cueVideoByUrl({
playerId: 'player-id-123',
options: { mediaContentUrl: 'https://example.com' },
});
console.log(result);

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

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.cuePlaylist({
playerId: 'player-id-123',
playlistOptions: { listType: 'playlist' },
});
console.log(result);

Load and play a playlist.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.loadPlaylist({
playerId: 'player-id-123',
playlistOptions: { listType: 'playlist' },
});
console.log(result);

Play the next video in the playlist.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.nextVideo({ playerId: 'player-id-123' });
console.log(result);

Play the previous video in the playlist.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.previousVideo({ playerId: 'player-id-123' });
console.log(result);

Play a specific video in the playlist by index.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.playVideoAt({
playerId: 'player-id-123',
index: 1,
});
console.log(result);

Mute the player audio.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.mute({ playerId: 'player-id-123' });
console.log(result);

Unmute the player audio.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.unMute({ playerId: 'player-id-123' });
console.log(result);

Check if the player is currently muted.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.isMuted({ playerId: 'player-id-123' });
console.log(result);

Set the player volume level.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.setVolume({
playerId: 'player-id-123',
volume: 0.5,
});
console.log(result);

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

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getVolume({ playerId: 'player-id-123' });
console.log(result);

Set the player dimensions in pixels.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.setSize({
playerId: 'player-id-123',
width: 1080,
height: 1920,
});
console.log(result);

Get the current playback rate.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getPlaybackRate({ playerId: 'player-id-123' });
console.log(result);

Set the playback speed.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.setPlaybackRate({
playerId: 'player-id-123',
suggestedRate: 1,
});
console.log(result);

Get list of available playback rates for current video.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getAvailablePlaybackRates({ playerId: 'player-id-123' });
console.log(result);

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

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.setLoop({
playerId: 'player-id-123',
loopPlaylists: true,
});
console.log(result);

Enable or disable playlist shuffle.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.setShuffle({
playerId: 'player-id-123',
shufflePlaylist: true,
});
console.log(result);

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

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getVideoLoadedFraction({ playerId: 'player-id-123' });
console.log(result);

Get the current state of the player.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getPlayerState({ playerId: 'player-id-123' });
console.log(result);

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

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getAllPlayersEventsState();
console.log(result);

Get the current playback position in seconds.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getCurrentTime({ playerId: 'player-id-123' });
console.log(result);

Toggle fullscreen mode on or off.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.toggleFullScreen({
playerId: 'player-id-123',
isFullScreen: true,
});
console.log(result);

Get the current playback quality.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getPlaybackQuality({ playerId: 'player-id-123' });
console.log(result);

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

import { YoutubePlayer, IPlaybackQuality } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.setPlaybackQuality({
playerId: 'player-id-123',
suggestedQuality: IPlaybackQuality.SMALL,
});
console.log(result);

Get list of available quality levels for current video.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getAvailableQualityLevels({ playerId: 'player-id-123' });
console.log(result);

Get the duration of the current video in seconds.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getDuration({ playerId: 'player-id-123' });
console.log(result);

Get the YouTube.com URL for the current video.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getVideoUrl({ playerId: 'player-id-123' });
console.log(result);

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

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getVideoEmbedCode({ playerId: 'player-id-123' });
console.log(result);

Get array of video IDs in the current playlist.

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getPlaylist({ playerId: 'player-id-123' });
console.log(result);

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

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getPlaylistIndex({ playerId: 'player-id-123' });
console.log(result);

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

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getIframe({ playerId: 'player-id-123' });
console.log(result);

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({
playerId: 'player-id-123',
eventName: 'onReady',
listener: (event) => {
console.log(event);
},
});
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.

If you are using Getting Started to plan dashboard and API operations, connect it with Using @capgo/capacitor-youtube-player for the native capability in Using @capgo/capacitor-youtube-player, API Overview for the implementation detail in API Overview, Introduction for the implementation detail in Introduction, API Keys for the implementation detail in API Keys, and Devices for the implementation detail in Devices.