Getting Started
Copy a setup prompt with the install steps and the full markdown guide for this plugin.
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-youtube-player`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/youtube-player/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
Install
Section titled “Install”You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-pluginsThen 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:
bun add @capgo/capacitor-youtube-playerbunx cap syncImport
Section titled “Import”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, andyoutu.berequests are affected. - Requests that already define a
Refererheader keep their original value. refererHeaderis optional and defaults tohttps://www.youtube.com.- Supported on Capacitor
8.xfor installed iOS and Android platforms.
API Overview
Section titled “API Overview”initialize
Section titled “initialize”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
Section titled “destroy”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);stopVideo
Section titled “stopVideo”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);playVideo
Section titled “playVideo”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);pauseVideo
Section titled “pauseVideo”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);seekTo
Section titled “seekTo”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);loadVideoById
Section titled “loadVideoById”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);cueVideoById
Section titled “cueVideoById”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);loadVideoByUrl
Section titled “loadVideoByUrl”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);cueVideoByUrl
Section titled “cueVideoByUrl”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);cuePlaylist
Section titled “cuePlaylist”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);loadPlaylist
Section titled “loadPlaylist”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);nextVideo
Section titled “nextVideo”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);previousVideo
Section titled “previousVideo”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);playVideoAt
Section titled “playVideoAt”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
Section titled “unMute”Unmute the player audio.
import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.unMute({ playerId: 'player-id-123' });console.log(result);isMuted
Section titled “isMuted”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);setVolume
Section titled “setVolume”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);getVolume
Section titled “getVolume”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);setSize
Section titled “setSize”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);getPlaybackRate
Section titled “getPlaybackRate”Get the current playback rate.
import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getPlaybackRate({ playerId: 'player-id-123' });console.log(result);setPlaybackRate
Section titled “setPlaybackRate”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);getAvailablePlaybackRates
Section titled “getAvailablePlaybackRates”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);setLoop
Section titled “setLoop”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);setShuffle
Section titled “setShuffle”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);getVideoLoadedFraction
Section titled “getVideoLoadedFraction”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);getPlayerState
Section titled “getPlayerState”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);getAllPlayersEventsState
Section titled “getAllPlayersEventsState”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);getCurrentTime
Section titled “getCurrentTime”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);toggleFullScreen
Section titled “toggleFullScreen”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);getPlaybackQuality
Section titled “getPlaybackQuality”Get the current playback quality.
import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
const result = await YoutubePlayer.getPlaybackQuality({ playerId: 'player-id-123' });console.log(result);setPlaybackQuality
Section titled “setPlaybackQuality”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);getAvailableQualityLevels
Section titled “getAvailableQualityLevels”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);getDuration
Section titled “getDuration”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);getVideoUrl
Section titled “getVideoUrl”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);getVideoEmbedCode
Section titled “getVideoEmbedCode”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);getPlaylist
Section titled “getPlaylist”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);getPlaylistIndex
Section titled “getPlaylistIndex”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);getIframe
Section titled “getIframe”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);addEventListener
Section titled “addEventListener”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); },});removeEventListener
Section titled “removeEventListener”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); },});Type Reference
Section titled “Type Reference”PlayerIdOptions
Section titled “PlayerIdOptions”export interface PlayerIdOptions { playerId: string;}SeekToOptions
Section titled “SeekToOptions”export interface SeekToOptions extends PlayerIdOptions { playerId: string; seconds: number; allowSeekAhead: boolean;}VideoByIdMethodOptions
Section titled “VideoByIdMethodOptions”export interface VideoByIdMethodOptions extends PlayerIdOptions { playerId: string; options: IVideoOptionsById;}VideoByUrlMethodOptions
Section titled “VideoByUrlMethodOptions”export interface VideoByUrlMethodOptions extends PlayerIdOptions { playerId: string; options: IVideoOptionsByUrl;}PlaylistMethodOptions
Section titled “PlaylistMethodOptions”export interface PlaylistMethodOptions extends PlayerIdOptions { playerId: string; playlistOptions: IPlaylistOptions;}PlayVideoAtOptions
Section titled “PlayVideoAtOptions”export interface PlayVideoAtOptions extends PlayerIdOptions { playerId: string; index: number;}SetVolumeOptions
Section titled “SetVolumeOptions”export interface SetVolumeOptions extends PlayerIdOptions { playerId: string; volume: number;}SetSizeOptions
Section titled “SetSizeOptions”export interface SetSizeOptions extends PlayerIdOptions { playerId: string; width: number; height: number;}SetPlaybackRateOptions
Section titled “SetPlaybackRateOptions”export interface SetPlaybackRateOptions extends PlayerIdOptions { playerId: string; suggestedRate: number;}SetLoopOptions
Section titled “SetLoopOptions”export interface SetLoopOptions extends PlayerIdOptions { playerId: string; loopPlaylists: boolean;}SetShuffleOptions
Section titled “SetShuffleOptions”export interface SetShuffleOptions extends PlayerIdOptions { playerId: string; shufflePlaylist: boolean;}ToggleFullScreenOptions
Section titled “ToggleFullScreenOptions”export interface ToggleFullScreenOptions extends PlayerIdOptions { playerId: string; isFullScreen: boolean | null | undefined;}Source Of Truth
Section titled “Source Of Truth”This page is generated from the plugin’s src/definitions.ts. Re-run the sync when the public API changes upstream.
Keep going from Getting Started
Section titled “Keep going from Getting Started”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.