入门
-
安装软件包
Terminal window npm i @capgo/capacitor-video-playerTerminal window pnpm add @capgo/capacitor-video-playerTerminal window yarn add @capgo/capacitor-video-playerTerminal window bun add @capgo/capacitor-video-player -
与原生项目同步
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync
import { CapacitorVideoPlayer } from '@capgo/capacitor-video-player';
// Initialize a video playerconst playVideo = async () => { const result = await CapacitorVideoPlayer.initPlayer({ mode: 'fullscreen', url: 'https://example.com/video.mp4', playerId: 'myPlayer', componentTag: 'div' });
console.log('Player initialized:', result);};
// Play the videoawait CapacitorVideoPlayer.play({ playerId: 'myPlayer' });
// Pause the videoawait CapacitorVideoPlayer.pause({ playerId: 'myPlayer' });
// Get current timeconst { value } = await CapacitorVideoPlayer.getCurrentTime({ playerId: 'myPlayer' });console.log('Current time:', value);
// Seek to positionawait CapacitorVideoPlayer.setCurrentTime({ playerId: 'myPlayer', seektime: 30 // seconds});
// Set volume (0-1)await CapacitorVideoPlayer.setVolume({ playerId: 'myPlayer', volume: 0.5});API 参考
Section titled “API 参考”initPlayer(选项)
Section titled “initPlayer(选项)”初始化视频播放器实例。
await CapacitorVideoPlayer.initPlayer({ mode: 'fullscreen', // or 'embedded' url: 'https://example.com/video.mp4', playerId: 'player1', subtitle: 'https://example.com/subtitles.vtt', language: 'en', rate: 1.0, exitOnEnd: true, loopOnEnd: false, pipEnabled: true, showControls: true});播放(选项)
Section titled “播放(选项)”播放视频。
await CapacitorVideoPlayer.play({ playerId: 'player1' });暂停(选项)
Section titled “暂停(选项)”暂停视频。
await CapacitorVideoPlayer.pause({ playerId: 'player1' });getDuration(选项)
Section titled “getDuration(选项)”获取视频时长(以秒为单位)。
const { value } = await CapacitorVideoPlayer.getDuration({ playerId: 'player1' });console.log('Duration:', value, 'seconds');getCurrentTime(选项)
Section titled “getCurrentTime(选项)”以秒为单位获取当前播放位置。
const { value } = await CapacitorVideoPlayer.getCurrentTime({ playerId: 'player1' });设置当前时间(选项)
Section titled “设置当前时间(选项)”寻求具体时间。
await CapacitorVideoPlayer.setCurrentTime({ playerId: 'player1', seektime: 60 // seconds});设置音量(选项)
Section titled “设置音量(选项)”设置音量(0.0 至 1.0)。
await CapacitorVideoPlayer.setVolume({ playerId: 'player1', volume: 0.8});getVolume(选项)
Section titled “getVolume(选项)”获取当前音量。
const { value } = await CapacitorVideoPlayer.getVolume({ playerId: 'player1' });设置静音(选项)
Section titled “设置静音(选项)”将视频静音或取消静音。
await CapacitorVideoPlayer.setMuted({ playerId: 'player1', muted: true});设置速率(选项)
Section titled “设置速率(选项)”设置播放速度。
await CapacitorVideoPlayer.setRate({ playerId: 'player1', rate: 1.5 // 1.5x speed});停止所有玩家()
Section titled “停止所有玩家()”停止所有活跃玩家。
await CapacitorVideoPlayer.stopAllPlayers();退出播放器()
Section titled “退出播放器()”退出视频播放器。
await CapacitorVideoPlayer.exitPlayer();import { CapacitorVideoPlayer } from '@capgo/capacitor-video-player';
export class VideoPlayerService { private playerId = 'mainPlayer';
async initializePlayer(videoUrl: string, subtitleUrl?: string) { try { const result = await CapacitorVideoPlayer.initPlayer({ mode: 'fullscreen', url: videoUrl, playerId: this.playerId, subtitle: subtitleUrl, language: 'en', rate: 1.0, exitOnEnd: true, loopOnEnd: false, pipEnabled: true, bkmodeEnabled: true, showControls: true, displayMode: 'all' });
console.log('Player initialized:', result); return result; } catch (error) { console.error('Failed to initialize player:', error); throw error; } }
async togglePlayPause() { const { value: isPlaying } = await CapacitorVideoPlayer.isPlaying({ playerId: this.playerId });
if (isPlaying) { await CapacitorVideoPlayer.pause({ playerId: this.playerId }); } else { await CapacitorVideoPlayer.play({ playerId: this.playerId }); } }
async seekForward(seconds: number = 10) { const { value: currentTime } = await CapacitorVideoPlayer.getCurrentTime({ playerId: this.playerId });
await CapacitorVideoPlayer.setCurrentTime({ playerId: this.playerId, seektime: currentTime + seconds }); }
async seekBackward(seconds: number = 10) { const { value: currentTime } = await CapacitorVideoPlayer.getCurrentTime({ playerId: this.playerId });
await CapacitorVideoPlayer.setCurrentTime({ playerId: this.playerId, seektime: Math.max(0, currentTime - seconds) }); }
async setPlaybackSpeed(speed: number) { await CapacitorVideoPlayer.setRate({ playerId: this.playerId, rate: speed }); }
async getProgress() { const { value: currentTime } = await CapacitorVideoPlayer.getCurrentTime({ playerId: this.playerId }); const { value: duration } = await CapacitorVideoPlayer.getDuration({ playerId: this.playerId });
return { currentTime, duration, percentage: (currentTime / duration) * 100 }; }
async cleanup() { await CapacitorVideoPlayer.stopAllPlayers(); }}- 需要 iOS 10.0+
- 使用原生AVPlayer
- 支持画中画
- 支持后台播放
Android
Section titled “Android”- 需要 Android 5.0 (API 21)+
- 使用ExoPlayer
- 支持 Chromecast
- 提供定制强调色
- 使用 HTML5 视频播放器
- 仅嵌入模式
- 有限的本机功能
- 清理玩家:完成后始终停止玩家
- 处理错误:将玩家调用包装在 try-catch 中
- 唯一玩家ID:为多个玩家使用唯一ID
- 检查播放状态:操作前验证状态
- 资源管理:释放玩家释放内存