Saltar al contenido

Getting Started

Este contenido aún no está disponible en tu idioma.

  1. Install the package

    Ventana de terminal
    npm i @capgo/capacitor-video-player
  2. Sync with native projects

    Ventana de terminal
    npx cap sync

Basic Usage

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

API Reference

initPlayer(options)

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

Play the video.

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

pause(options)

Pause the video.

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

getDuration(options)

Get video duration in seconds.

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

getCurrentTime(options)

Get current playback position in seconds.

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

setCurrentTime(options)

Seek to a specific time.

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

setVolume(options)

Set volume (0.0 to 1.0).

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

getVolume(options)

Get current volume.

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

setMuted(options)

Mute or unmute the video.

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

setRate(options)

Set playback speed.

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

stopAllPlayers()

Stop all active players.

await CapacitorVideoPlayer.stopAllPlayers();

exitPlayer()

Exit the video player.

await CapacitorVideoPlayer.exitPlayer();

Complete Example

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

iOS

  • Requires iOS 10.0+
  • Uses native AVPlayer
  • Supports Picture-in-Picture
  • Background playback supported

Android

  • Requires Android 5.0 (API 21)+
  • Uses ExoPlayer
  • Supports Chromecast
  • Custom accent colors available

Web

  • Uses HTML5 video player
  • Embedded mode only
  • Limited native features

Best Practices

  1. Clean up players: Always stop players when done
  2. Handle errors: Wrap player calls in try-catch
  3. Unique player IDs: Use unique IDs for multiple players
  4. Check playback status: Verify state before operations
  5. Resource management: Release players to free memory