Getting Started
此内容尚不支持你的语言。
-
Install the package
Terminal window npm i @capgo/ivs-playerTerminal window pnpm add @capgo/ivs-playerTerminal window yarn add @capgo/ivs-playerTerminal window bun add @capgo/ivs-player -
Sync with native projects
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync -
Configure the plugin
Basic Player Setup:
import { IvsPlayer } from '@capgo/ivs-player';// Create a playerconst { playerId } = await IvsPlayer.create({url: 'https://your-ivs-playback-url.m3u8',autoplay: true});// Start playbackawait IvsPlayer.play({ playerId });Player Controls:
// Pause playbackawait IvsPlayer.pause({ playerId });// Set volumeawait IvsPlayer.setVolume({playerId,volume: 0.5 // 0.0 to 1.0});// Seek to positionawait IvsPlayer.seekTo({playerId,position: 30 // seconds});Add to your
Info.plist
:<key>NSAppTransportSecurity</key><dict><key>NSAllowsArbitraryLoads</key><true/></dict>Add to your
AndroidManifest.xml
:<uses-permission android:name="android.permission.INTERNET" /> -
Event handling
import { IvsPlayer } from '@capgo/ivs-player';// Listen for player eventsIvsPlayer.addListener('onState', (event) => {console.log('Player state:', event.state);// States: idle, buffering, ready, playing, ended});IvsPlayer.addListener('onError', (event) => {console.error('Player error:', event.error);});IvsPlayer.addListener('onDuration', (event) => {console.log('Video duration:', event.duration);});IvsPlayer.addListener('onProgress', (event) => {console.log('Current position:', event.position);});// Listen for timed metadataIvsPlayer.addListener('onMetadata', (event) => {console.log('Timed metadata:', event.metadata);}); -
Advanced usage
import { IvsPlayer } from '@capgo/ivs-player';export class StreamPlayer {private playerId: string | null = null;private listeners: any[] = [];async initialize(streamUrl: string) {// Create player with custom configurationconst result = await IvsPlayer.create({url: streamUrl,autoplay: false,muted: true, // Start muted for autoplay policiescontrols: true});this.playerId = result.playerId;this.setupEventListeners();}private setupEventListeners() {// State changesconst stateListener = IvsPlayer.addListener('onState', (event) => {this.handleStateChange(event.state);});this.listeners.push(stateListener);// Quality changesconst qualityListener = IvsPlayer.addListener('onQuality', (event) => {console.log(`Quality changed to: ${event.quality.name}`);});this.listeners.push(qualityListener);// Buffer updatesconst bufferListener = IvsPlayer.addListener('onBuffer', (event) => {console.log(`Buffer: ${event.percentage}%`);});this.listeners.push(bufferListener);}private handleStateChange(state: string) {switch (state) {case 'playing':console.log('Stream is playing');break;case 'buffering':console.log('Stream is buffering');break;case 'ended':console.log('Stream ended');break;}}async play() {if (this.playerId) {await IvsPlayer.play({ playerId: this.playerId });}}async pause() {if (this.playerId) {await IvsPlayer.pause({ playerId: this.playerId });}}async setQuality(qualityName: string) {if (this.playerId) {await IvsPlayer.setQuality({playerId: this.playerId,quality: qualityName});}}async destroy() {// Remove listenersthis.listeners.forEach(listener => listener.remove());// Destroy playerif (this.playerId) {await IvsPlayer.destroy({ playerId: this.playerId });}}}
API Reference
Methods
create(options: CreateOptions)
Create a new IVS player instance.
Parameters:
options
: Player configurationurl
: string - IVS playback URLautoplay
: boolean - Auto-start playbackmuted
: boolean - Start mutedcontrols
: boolean - Show native controls
Returns: Promise<{ playerId: string }>
play(options: PlayerOptions)
Start playback.
pause(options: PlayerOptions)
Pause playback.
stop(options: PlayerOptions)
Stop playback and reset position.
seekTo(options: SeekOptions)
Seek to a specific position.
setVolume(options: VolumeOptions)
Set player volume (0.0 to 1.0).
setQuality(options: QualityOptions)
Set playback quality.
destroy(options: PlayerOptions)
Destroy the player instance.
Events
onState
: Player state changesonError
: Playback errorsonDuration
: Video duration availableonProgress
: Playback progress updatesonQuality
: Quality changesonMetadata
: Timed metadata eventsonBuffer
: Buffer status updates
Interfaces
interface CreateOptions { url: string; autoplay?: boolean; muted?: boolean; controls?: boolean;}
interface PlayerOptions { playerId: string;}
interface SeekOptions extends PlayerOptions { position: number; // seconds}
interface VolumeOptions extends PlayerOptions { volume: number; // 0.0 to 1.0}
interface QualityOptions extends PlayerOptions { quality: string; // quality name}
Platform Notes
iOS
- Requires iOS 12.0 or later
- Uses native AVPlayer with IVS extensions
- Supports Picture-in-Picture on iPads
Android
- Requires Android 5.0 (API level 21) or later
- Uses Amazon IVS Player SDK
- Hardware acceleration recommended
Common Use Cases
- Live Streaming: Real-time events, sports, gaming
- Interactive Streaming: Viewer polls, Q&A sessions
- E-commerce: Live shopping with product highlights
- Education: Live classes with timed content
- Entertainment: Concerts, shows with audience interaction
Best Practices
-
Network Handling: Monitor connection quality
IvsPlayer.addListener('onError', (event) => {if (event.error.includes('network')) {// Handle network errorsshowRetryButton();}}); -
Resource Management: Always destroy players when done
-
Autoplay Policies: Start muted for reliable autoplay
-
Quality Selection: Let IVS handle automatic quality switching
Troubleshooting
Stream not playing:
- Verify the IVS playback URL is valid
- Check network permissions
- Ensure the stream is live
High latency:
- IVS is optimized for low latency, check your encoder settings
- Verify you’re using the IVS-specific playback URL
Quality issues:
- Allow automatic quality switching
- Check network bandwidth requirements