Comenzando
-
Instalar el paquete
Ventana de terminal npm i @Capgo/Capacitor-video-playerVentana de terminal pnpm add @Capgo/Capacitor-video-playerVentana de terminal yarn add @Capgo/Capacitor-video-playerVentana de terminal bun add @Capgo/Capacitor-video-player -
Sincronizar con proyectos nativos
Ventana de terminal npx cap syncVentana de terminal pnpm cap syncVentana de terminal yarn cap syncVentana de terminal bunx cap sync
Uso Básico
Section titled “Uso Básico”import { CapacitorVideoPlayer } from '@capgo/capacitor-video-player';
// Inicializar un reproductor de videoconst playVideo = async () => { const result = await CapacitorVideoPlayer.initPlayer({ mode: 'fullscreen', url: 'https://example.com/video.mp4', playerId: 'myPlayer', componentTag: 'div' });
console.log('Player initialized:', result);};
// Reproducir el videoawait CapacitorVideoPlayer.play({ playerId: 'myPlayer' });
// Pausar el videoawait CapacitorVideoPlayer.pause({ playerId: 'myPlayer' });
// Obtener tiempo actualconst { value } = await CapacitorVideoPlayer.getCurrentTime({ playerId: 'myPlayer' });console.log('Current time:', value);
// Buscar posiciónawait CapacitorVideoPlayer.setCurrentTime({ playerId: 'myPlayer', seektime: 30 // segundos});
// Establecer volumen (0-1)await CapacitorVideoPlayer.setVolume({ playerId: 'myPlayer', volume: 0.5});Referencia API
Section titled “Referencia API”initPlayer(Opciones)
Section titled “initPlayer(Opciones)”Inicializa una instancia del reproductor de video.
await CapacitorVideoPlayer.initPlayer({ mode: 'fullscreen', // o '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(Opciones)
Section titled “play(Opciones)”Reproduce el video.
await CapacitorVideoPlayer.play({ playerId: 'player1' });pause(Opciones)
Section titled “pause(Opciones)”Pausa el video.
await CapacitorVideoPlayer.pause({ playerId: 'player1' });getDuration(Opciones)
Section titled “getDuration(Opciones)”Obtiene la duración del video en segundos.
const { value } = await CapacitorVideoPlayer.getDuration({ playerId: 'player1' });console.log('Duration:', value, 'seconds');getCurrentTime(Opciones)
Section titled “getCurrentTime(Opciones)”Obtiene la posición de reproducción actual en segundos.
const { value } = await CapacitorVideoPlayer.getCurrentTime({ playerId: 'player1' });setCurrentTime(Opciones)
Section titled “setCurrentTime(Opciones)”Busca un tiempo específico.
await CapacitorVideoPlayer.setCurrentTime({ playerId: 'player1', seektime: 60 // segundos});setVolume(Opciones)
Section titled “setVolume(Opciones)”Establece el volumen (0.0 a 1.0).
await CapacitorVideoPlayer.setVolume({ playerId: 'player1', volume: 0.8});getVolume(Opciones)
Section titled “getVolume(Opciones)”Obtiene el volumen actual.
const { value } = await CapacitorVideoPlayer.getVolume({ playerId: 'player1' });setMuted(Opciones)
Section titled “setMuted(Opciones)”Silencia o activa el sonido del video.
await CapacitorVideoPlayer.setMuted({ playerId: 'player1', muted: true});setRate(Opciones)
Section titled “setRate(Opciones)”Establece la velocidad de reproducción.
await CapacitorVideoPlayer.setRate({ playerId: 'player1', rate: 1.5 // velocidad 1.5x});stopAllPlayers()
Section titled “stopAllPlayers()”Detiene todos los reproductores activos.
await CapacitorVideoPlayer.stopAllPlayers();exitPlayer()
Section titled “exitPlayer()”Sale del reproductor de video.
await CapacitorVideoPlayer.exitPlayer();Ejemplo Completo
Section titled “Ejemplo Completo”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(); }}Notas de Plataforma
Section titled “Notas de Plataforma”- Requiere iOS 10.0+
- Usa AVPlayer nativo
- Soporta Picture-in-Picture
- Reproducción en segundo plano soportada
Android
Section titled “Android”- Requiere Android 5.0 (API 21)+
- Usa ExoPlayer
- Soporta Chromecast
- Colores de acento personalizados disponibles
- Usa reproductor de video HTML5
- Solo modo embebido
- Características nativas limitadas
Mejores Prácticas
Section titled “Mejores Prácticas”- Limpiar reproductores: Siempre detén los reproductores cuando termines
- Manejar errores: Envuelve llamadas del reproductor en try-catch
- IDs únicos de reproductor: Usa IDs únicos para múltiples reproductores
- Verificar estado de reproducción: Verifica el estado antes de operaciones
- Gestión de recursos: Libera reproductores para liberar memoria