Commencer
-
Installer the package
Fenêtre de terminal npm i @capgo/capacitor-video-playerFenêtre de terminal pnpm add @capgo/capacitor-video-playerFenêtre de terminal yarn add @capgo/capacitor-video-playerFenêtre de terminal bun add @capgo/capacitor-video-player -
Synchroniser with Natif projects
Fenêtre de terminal npx cap syncFenêtre de terminal pnpm cap syncFenêtre de terminal yarn cap syncFenêtre de terminal bunx cap sync
Basic Utilisation
Section titled “Basic Utilisation”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 Référence
Section titled “API Référence”initPlayer(Options)
Section titled “initPlayer(Options)”Initialiser a video player instance.
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});play(Options)
Section titled “play(Options)”Play the video.
await CapacitorVideoPlayer.play({ playerId: 'player1' });pause(Options)
Section titled “pause(Options)”Pause the video.
await CapacitorVideoPlayer.pause({ playerId: 'player1' });getDuration(Options)
Section titled “getDuration(Options)”Get video duration in seconds.
const { value } = await CapacitorVideoPlayer.getDuration({ playerId: 'player1' });console.log('Duration:', value, 'seconds');getCurrentTime(Options)
Section titled “getCurrentTime(Options)”Get current playback position in seconds.
const { value } = await CapacitorVideoPlayer.getCurrentTime({ playerId: 'player1' });setCurrentTime(Options)
Section titled “setCurrentTime(Options)”Seek to a specific time.
await CapacitorVideoPlayer.setCurrentTime({ playerId: 'player1', seektime: 60 // seconds});setVolume(Options)
Section titled “setVolume(Options)”Set volume (0.0 to 1.0).
await CapacitorVideoPlayer.setVolume({ playerId: 'player1', volume: 0.8});getVolume(Options)
Section titled “getVolume(Options)”Get current volume.
const { value } = await CapacitorVideoPlayer.getVolume({ playerId: 'player1' });setMuted(Options)
Section titled “setMuted(Options)”Mute or unmute the video.
await CapacitorVideoPlayer.setMuted({ playerId: 'player1', muted: true});setRate(Options)
Section titled “setRate(Options)”Set playback speed.
await CapacitorVideoPlayer.setRate({ playerId: 'player1', rate: 1.5 // 1.5x speed});stopAllPlayers()
Section titled “stopAllPlayers()”Arrêter all Actif players.
await CapacitorVideoPlayer.stopAllPlayers();exitPlayer()
Section titled “exitPlayer()”Exit the video player.
await CapacitorVideoPlayer.exitPlayer();Terminé Exemple
Section titled “Terminé Exemple”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(); }}Platform Notes
Section titled “Platform Notes”- Requires iOS 10.0+
- Uses Natif AVPlayer
- Supports Picture-in-Picture
- Background playback supported
Android
Section titled “Android”- Requires Android 5.0 (API 21)+
- Uses ExoPlayer
- Supports Chromecast
- Custom accent colors Disponible
- Uses HTML5 video player
- Embedded mode only
- Limited Natif Fonctionnalités
Best Practices
Section titled “Best Practices”- Clean up players: Always Arrêter players when done
- Handle errors: Wrap player calls in try-catch
- Unique player IDs: Use unique IDs for multiple players
- Vérifier playback status: Verify state before operations
- Resource management: Libération players to free memory