콘텐츠로 건너뛰기

시작하기

  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 사용
  • Picture-in-Picture 지원
  • 백그라운드 재생 지원
  • Android 5.0 (API 21)+ 필요
  • ExoPlayer 사용
  • Chromecast 지원
  • 커스텀 액센트 색상 사용 가능
  • HTML5 비디오 플레이어 사용
  • 임베디드 모드만 지원
  • 제한된 네이티브 기능
  1. 플레이어 정리: 작업 완료 후 항상 플레이어 중지
  2. 오류 처리: 플레이어 호출을 try-catch로 감싸기
  3. 고유한 플레이어 ID: 여러 플레이어에 고유한 ID 사용
  4. 재생 상태 확인: 작업 전 상태 확인
  5. 리소스 관리: 메모리 확보를 위해 플레이어 해제