시작하기
-
패키지 설치
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
기본 사용법
Section titled “기본 사용법”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(options)
Section titled “initPlayer(options)”비디오 플레이어 인스턴스를 초기화합니다.
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)”비디오를 재생합니다.
await CapacitorVideoPlayer.play({ playerId: 'player1' });pause(options)
Section titled “pause(options)”비디오를 일시 중지합니다.
await CapacitorVideoPlayer.pause({ playerId: 'player1' });getDuration(options)
Section titled “getDuration(options)”비디오 길이를 초 단위로 가져옵니다.
const { value } = await CapacitorVideoPlayer.getDuration({ playerId: 'player1' });console.log('Duration:', value, 'seconds');getCurrentTime(options)
Section titled “getCurrentTime(options)”현재 재생 위치를 초 단위로 가져옵니다.
const { value } = await CapacitorVideoPlayer.getCurrentTime({ playerId: 'player1' });setCurrentTime(options)
Section titled “setCurrentTime(options)”특정 시간으로 탐색합니다.
await CapacitorVideoPlayer.setCurrentTime({ playerId: 'player1', seektime: 60 // seconds});setVolume(options)
Section titled “setVolume(options)”볼륨을 설정합니다 (0.0에서 1.0).
await CapacitorVideoPlayer.setVolume({ playerId: 'player1', volume: 0.8});getVolume(options)
Section titled “getVolume(options)”현재 볼륨을 가져옵니다.
const { value } = await CapacitorVideoPlayer.getVolume({ playerId: 'player1' });setMuted(options)
Section titled “setMuted(options)”비디오를 음소거하거나 음소거를 해제합니다.
await CapacitorVideoPlayer.setMuted({ playerId: 'player1', muted: true});setRate(options)
Section titled “setRate(options)”재생 속도를 설정합니다.
await CapacitorVideoPlayer.setRate({ playerId: 'player1', rate: 1.5 // 1.5x speed});stopAllPlayers()
Section titled “stopAllPlayers()”모든 활성 플레이어를 중지합니다.
await CapacitorVideoPlayer.stopAllPlayers();exitPlayer()
Section titled “exitPlayer()”비디오 플레이어를 종료합니다.
await CapacitorVideoPlayer.exitPlayer();완전한 예제
Section titled “완전한 예제”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(); }}플랫폼 참고사항
Section titled “플랫폼 참고사항”- iOS 10.0+ 필요
- 네이티브 AVPlayer 사용
- Picture-in-Picture 지원
- 백그라운드 재생 지원
Android
Section titled “Android”- Android 5.0 (API 21)+ 필요
- ExoPlayer 사용
- Chromecast 지원
- 커스텀 액센트 색상 사용 가능
- HTML5 비디오 플레이어 사용
- 임베디드 모드만 지원
- 제한된 네이티브 기능
- 플레이어 정리: 작업 완료 후 항상 플레이어 중지
- 오류 처리: 플레이어 호출을 try-catch로 감싸기
- 고유한 플레이어 ID: 여러 플레이어에 고유한 ID 사용
- 재생 상태 확인: 작업 전 상태 확인
- 리소스 관리: 메모리 확보를 위해 플레이어 해제