跳转到内容

入门

  1. 安装软件包

    Terminal window
    npm i @capgo/capacitor-video-player
  2. 与原生项目同步

    Terminal window
    npx cap sync
import { CapacitorVideoPlayer } from '@capgo/capacitor-video-player';
// Initialize a video player
const 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 video
await CapacitorVideoPlayer.play({ playerId: 'myPlayer' });
// Pause the video
await CapacitorVideoPlayer.pause({ playerId: 'myPlayer' });
// Get current time
const { value } = await CapacitorVideoPlayer.getCurrentTime({ playerId: 'myPlayer' });
console.log('Current time:', value);
// Seek to position
await CapacitorVideoPlayer.setCurrentTime({
playerId: 'myPlayer',
seektime: 30 // seconds
});
// Set volume (0-1)
await CapacitorVideoPlayer.setVolume({
playerId: 'myPlayer',
volume: 0.5
});

初始化视频播放器实例。

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

播放视频。

await CapacitorVideoPlayer.play({ playerId: 'player1' });

暂停视频。

await CapacitorVideoPlayer.pause({ playerId: 'player1' });

获取视频时长(以秒为单位)。

const { value } = await CapacitorVideoPlayer.getDuration({ playerId: 'player1' });
console.log('Duration:', value, 'seconds');

以秒为单位获取当前播放位置。

const { value } = await CapacitorVideoPlayer.getCurrentTime({ playerId: 'player1' });

寻求具体时间。

await CapacitorVideoPlayer.setCurrentTime({
playerId: 'player1',
seektime: 60 // seconds
});

设置音量(0.0 至 1.0)。

await CapacitorVideoPlayer.setVolume({
playerId: 'player1',
volume: 0.8
});

获取当前音量。

const { value } = await CapacitorVideoPlayer.getVolume({ playerId: 'player1' });

将视频静音或取消静音。

await CapacitorVideoPlayer.setMuted({
playerId: 'player1',
muted: true
});

设置播放速度。

await CapacitorVideoPlayer.setRate({
playerId: 'player1',
rate: 1.5 // 1.5x speed
});

停止所有活跃玩家。

await CapacitorVideoPlayer.stopAllPlayers();

退出视频播放器。

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 5.0 (API 21)+
  • 使用ExoPlayer
  • 支持 Chromecast
  • 提供定制强调色
  • 使用 HTML5 视频播放器
  • 仅嵌入模式
  • 有限的本机功能
  1. 清理玩家:完成后始终停止玩家
  2. 处理错误:将玩家调用包装在 try-catch 中
  3. 唯一玩家ID:为多个玩家使用唯一ID
  4. 检查播放状态:操作前验证状态
  5. 资源管理:释放玩家释放内存