Skip to content

YouTube プレーヤー __CAPGO_KEEP_0__ リポジトリ

GitHub

CapgoのAI-Assisted セットアップを使用してプラグインをインストールできます。AIツールにCapgoスキルを追加するには、以下のコマンドを実行してください。

ターミナル画面
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

次に、以下のコマンドを使用してください:

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

手動設定を使用する場合は、以下のコマンドを実行してください。プラットフォーム固有の指示に従ってください。

ターミナル画面
bun add @capgo/capacitor-youtube-player
bunx cap sync
import { YoutubePlayer } from '@capgo/capacitor-youtube-player';

メイン WebView で YouTube のリファラブロッキングを修正する

「メイン WebView で YouTube のリファラブロッキングを修正する」のセクション

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 Capacitorの設定で

Capacitorがsync/updateの際にパッチされるので、インターセプトされたYouTubeの要求には有効なヘッダーが含まれます。 Referer コピー

{
"plugins": {
"YoutubePlayer": {
"patchRefererHeader": true,
"refererHeader": "https://www.youtube.com"
}
}
}
  • youtube.com, youtube-nocookie.comyoutu.be の要求は影響を受けません。
  • すでにヘッダーを定義している要求は元の値を保持します。 Referer は省略可能であり、
  • refererHeader でデフォルト値になります。 https://www.youtube.com.
  • はCapacitorでサポートされています。 8.x iOSおよびAndroidプラットフォームのインストール済み。

新しいYouTubeプレイヤーインスタンスを初期化します。

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

プレイヤーインスタンスを破棄してリソースを解放します。

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

動画再生を停止し、ロードをキャンセルします。 この機能はまれに使用されるため、通常はpauseVideo()を使用します。

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

現在のビデオを再生します。 最終的なプレイヤー状態はPLAYING (1)になります。

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

現在再生中のビデオを一時停止します。 最終的なプレイヤー状態はPAUSED (2)になります。終了 (0) していた場合はそのままです。

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

ビデオの特定の時間にジャンプします。 再生中の場合、再生を続けます。停止中の場合、停止状態が維持されます。

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

YouTube IDで指定されたビデオを読み込み、再生します。

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

IDで指定されたビデオをプレイせずに表示します。 サムネイルとプレーヤーを読み込み、ビデオをプレイするまでの準備を行います。

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

URLで指定されたビデオを読み込み、プレイします。

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

URLで指定されたビデオをプレイせずに表示します。

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

プレイリストをプレイせずに表示します。 プレイリストと最初のビデオを読み込みます。

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

プレイリストを読み込み、再生します。

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

プレイリスト内の次の動画を再生します。

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

プレイリスト内の前の動画を再生します。

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

プレイリスト内の特定の動画を指定されたインデックスで再生します。

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

プレイヤーアウディオをミュートします。

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

プレイヤーアウディオをアンミュートします。

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

プレイヤーが現在ミュートされているかどうかを確認します。

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

プレイヤーの音量レベルを設定します。

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

getVolume

getVolume

現在の再生中の音量を取得します。 ミュート状態でも音量を取得します。

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

setSize

setSize

指定したピクセル数で再生画面のサイズを設定します。

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

getPlaybackRate

getPlaybackRate

現在の再生速度を取得します。

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

setPlaybackRate

setPlaybackRate

再生速度を指定した値に設定します。

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

現在の動画の利用可能な再生速度のリストを取得します。

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

再生リストのループを有効または無効にします。 有効にすると、最後の動画の終了後、再生リストは最初から再生されます。

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

再生リストのシャッフルを有効または無効にします。

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

バッファリングされた動画の割合を取得します。 古いgetVideoBytesLoaded/getVideoBytesTotalより信頼性が高くなりました。

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

プレイヤーの現在の状態を取得します。

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

すべてのアクティブなプレイヤーのイベント状態を取得します。 複数のプレイヤーインスタンスを追跡するのに役立ちます。

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

現在の再生位置(秒)を取得します。

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

フルスクリーンモードをオンまたはオフに切り替えます。

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

現在の再生品質を取得します。

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

推奨再生品質を設定します。ネットワーク状況によって実際の品質が異なる場合があります。

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

現在のビデオの利用可能な品質レベルの一覧を取得します。

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

現在のビデオの長さ(秒)を取得します。

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

getVideoUrl

getVideoUrl

現在の動画のYouTube.com URLを取得します。

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

getVideoEmbedCode

getVideoEmbedCode

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);

getPlaylist

getPlaylist

現在のプレイリスト内の動画IDの配列を取得します。

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

getPlaylistIndex

getPlaylistIndex

現在再生中の動画のプレイリスト内のインデックスを取得します。

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

getIframe

getIframe

プレイヤーのiframe DOM要素を取得します。Webプラットフォームのみ。

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

addEventListener

addEventListener

イベントリスナーをプレイヤーに追加します。Webプラットフォームのみ。

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

removeEventListener

removeEventListener

イベントリスナーをプレイヤーから削除します。Webプラットフォームのみ。

import { YoutubePlayer } from '@capgo/capacitor-youtube-player';
await YoutubePlayer.removeEventListener({} as PlayerEventListenerOptions<TEvent>);

型リファレンス

型リファレンス

PlayerIdOptions

PlayerIdOptions
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;
}

ソース・オブ・トラース

真実の源

このページはプラグインから生成されています。 src/definitions.tsAPIのパブリック変更がアップストリームで発生した場合に、再度同期を実行してください。

Getting Startedから続けてください

Getting Startedから続けてください

あなたが Getting Started ダッシュボードとAPIの操作を計画する場合に、接続してください。 native capabilityのために@capgo/capacitor-youtube-player native capabilityのために@capgo/capacitor-youtube-player APIの概要 APIの実装詳細 概要 概要の実装詳細について API キー API キーの実装詳細について デバイス デバイスの実装詳細について