跳转至正文

快速入门

GitHub

你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:

终端窗口
bunx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下 prompt:

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

如果你更倾向于手动设置,请运行以下命令安装插件,并参考下文针对各平台的具体说明:

终端窗口
bun add @capgo/capacitor-video-player
bunx cap sync
import { VideoPlayer } from '@capgo/capacitor-video-player';
import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.initPlayer({ mode: 'fullscreen' });
console.log(result);
import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.isPlaying({ playerId: 'player-id-123' });
console.log(result);
import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.play({ playerId: 'player-id-123' });
console.log(result);
  • 原生同步: 运行 bunx cap sync 在插件升级后。
  • 单一事实来源: 信任 src/definitions.ts 在 GitHub 仓库中。

初始化视频播放器

import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.initPlayer({ mode: 'fullscreen' });
console.log(result);

返回指定 playerId 是否正在播放

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

从指定 playerId 播放当前视频

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

通过指定的 playerId 暂停当前视频

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

通过指定的 playerId 获取当前视频的时长

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

获取指定 playerId 当前视频的当前播放时间

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

通过指定的 playerId 设置当前视频的播放进度

import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.setCurrentTime({
playerId: 'player-id-123',
seektime: 10,
});
console.log(result);

通过指定的 playerId 获取当前视频的音量

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

通过指定的 playerId 设置当前视频的音量

import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.setVolume({ volume: 0.5 });
console.log(result);

通过指定的 playerId 获取当前视频是否静音

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

根据指定的 playerId 设置当前视频的静音状态

import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.setMuted({ muted: true });
console.log(result);

根据指定的 playerId 设置当前视频的播放速率

import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.setRate({ rate: 1 });
console.log(result);

根据指定的 playerId 获取当前视频的播放速率

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

停止所有正在播放的播放器

import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.stopAllPlayers();
console.log(result);

显示控制器

import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.showController();
console.log(result);

isControllerIsFullyVisible

“isControllerIsFullyVisible” 章节

isControllerIsFullyVisible

import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.isControllerIsFullyVisible();
console.log(result);

退出播放器

import { VideoPlayer } from '@capgo/capacitor-video-player';
const result = await VideoPlayer.exitPlayer();
console.log(result);
export interface capVideoPlayerOptions {
/**
* Player mode
* - "fullscreen"
* - "embedded" (Web only)
*/
mode?: string;
/**
* The url of the video to play
*/
url?: string;
/**
* The url of subtitle associated with the video
*/
subtitle?: string;
/**
* The language of subtitle
* see https://github.com/libyal/libfwnt/wiki/Language-Code-identifiers
*/
language?: string;
/**
* SubTitle Options
*/
subtitleOptions?: SubTitleOptions;
/**
* Id of DIV Element parent of the player
*/
playerId?: string;
/**
* Initial playing rate
*/
rate?: number;
/**
* Exit on VideoEnd (iOS, Android)
* default: true
*/
exitOnEnd?: boolean;
/**
* Loop on VideoEnd when exitOnEnd false (iOS, Android)
* default: false
*/
loopOnEnd?: boolean;
/**
* Picture in Picture Enable (iOS, Android)
* default: true
*/
pipEnabled?: boolean;
/**
* Background Mode Enable (iOS, Android)
* default: true
*/
bkmodeEnabled?: boolean;
/**
* Show Controls Enable (iOS, Android)
* default: true
*/
showControls?: boolean;
/**
* Display Mode ["all", "portrait", "landscape"] (iOS, Android)
* default: "all"
*/
displayMode?: string;
/**
* Component Tag or DOM Element Tag (React app)
*/
componentTag?: string;
/**
* Player Width (mode "embedded" only)
*/
width?: number;
/**
* Player height (mode "embedded" only)
*/
height?: number;
/**
* Headers for the request (iOS, Android)
* by Manuel García Marín (https://github.com/PhantomPainX)
*/
headers?: {
[key: string]: string;
};
/**
* Title shown in the player (Android)
* by Manuel García Marín (https://github.com/PhantomPainX)
*/
title?: string;
/**
* Subtitle shown below the title in the player (Android)
* by Manuel García Marín (https://github.com/PhantomPainX)
*/
smallTitle?: string;
/**
* ExoPlayer Progress Bar and Spinner color (Android)
* by Manuel García Marín (https://github.com/PhantomPainX)
* Must be a valid hex color code
* default: #FFFFFF
*/
accentColor?: string;
/**
* Chromecast enable/disable (Android)
* by Manuel García Marín (https://github.com/PhantomPainX)
* default: true
*/
chromecast?: boolean;
/**
* Artwork url to be shown in Chromecast player
* by Manuel García Marín (https://github.com/PhantomPainX)
* default: ""
*/
artwork?: string;
/**
* DRM configuration for protected content (iOS: FairPlay, Android: Widevine)
*/
drm?: DrmOptions;
}
export interface capVideoPlayerResult {
/**
* result set to true when successful else false
*/
result?: boolean;
/**
* method name
*/
method?: string;
/**
* value returned
*/
value?: any;
/**
* message string
*/
message?: string;
}
export interface capVideoPlayerIdOptions {
/**
* Id of DIV Element parent of the player
*/
playerId?: string;
}
export interface capVideoTimeOptions {
/**
* Id of DIV Element parent of the player
*/
playerId?: string;
/**
* Video time value you want to seek to
*/
seektime?: number;
}
export interface capVideoVolumeOptions {
/**
* Id of DIV Element parent of the player
*/
playerId?: string;
/**
* Volume value between [0 - 1]
*/
volume?: number;
}
export interface capVideoMutedOptions {
/**
* Id of DIV Element parent of the player
*/
playerId?: string;
/**
* Muted value true or false
*/
muted?: boolean;
}
export interface capVideoRateOptions {
/**
* Id of DIV Element parent of the player
*/
playerId?: string;
/**
* Rate value
*/
rate?: number;
}
export interface SubTitleOptions {
/**
* Foreground Color in RGBA (default rgba(255,255,255,1)
*/
foregroundColor?: string;
/**
* Background Color in RGBA (default rgba(0,0,0,1)
*/
backgroundColor?: string;
/**
* Font Size in pixels (default 16)
*/
fontSize?: number;
/**
* Get the native Capacitor plugin version
*
* @returns {Promise<{ id: string }>} an Promise with version for this device
* @throws An error if the something went wrong
*/
getPluginVersion(): Promise<{ version: string }>;
}
export interface DrmOptions {
/**
* FairPlay DRM configuration (iOS)
*/
fairplay?: FairPlayDrmOptions;
/**
* PlayReady DRM configuration
*/
playready?: PlayreadyDrmOptions;
/**
* Widevine DRM configuration (Android)
*/
widevine?: WidevineDrmOptions;
}
export interface FairPlayDrmOptions {
/**
* The URL to fetch the FairPlay certificate
*/
certificateUrl?: string;
/**
* The URL to send the SPC and receive the CKC license (FairPlay license server URL)
*/
contentKeySpcUrl?: string;
}
export interface PlayreadyDrmOptions {
/**
* The URL to fetch the PlayReady license
*/
certificateUrl?: string;
}
export interface WidevineDrmOptions {
/**
* The URL to fetch the Widevine license
*/
certificateUrl?: string;
}

本页面由该插件的 src/definitions.ts当上游的公共 API 发生变化时,请重新运行 sync。

如果你正在使用 快速入门 来规划原生媒体和界面行为,并将其与 使用 @capgo/capacitor-video-player 关于“使用 @capgo/capacitor-video-player”中的原生能力, 使用 @capgo/capacitor-live-activities 有关“使用 @capgo/capacitor-live-activities”中的原生能力, @capgo/capacitor-live-activities 关于 @capgo/capacitor-live-activities 的实现细节, @capgo/capacitor-video-player 关于 @capgo/capacitor-video-player 的实现细节,以及 使用 @capgo/capacitor-native-navigation 关于“使用 @capgo/capacitor-native-navigation.”中的原生能力