はじめに
-
パッケージをインストール
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 -
ネイティブプロジェクトと同期
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync -
プラグインを設定
基本的なプレーヤーセットアップ:
import { IvsPlayer } from '@capgo/ivs-player';// プレーヤーを作成const { playerId } = await IvsPlayer.create({url: 'https://your-ivs-playback-url.m3u8',autoplay: true});// 再生を開始await IvsPlayer.play({ playerId });プレーヤーコントロール:
// 再生を一時停止await IvsPlayer.pause({ playerId });// ボリュームを設定await IvsPlayer.setVolume({playerId,volume: 0.5 // 0.0 から 1.0});// 位置にシークawait IvsPlayer.seekTo({playerId,position: 30 // 秒});Info.plistに追加:<key>NSAppTransportSecurity</key><dict><key>NSAllowsArbitraryLoads</key><true/></dict>AndroidManifest.xmlに追加:<uses-permission android:name="android.permission.INTERNET" /> -
イベント処理
import { IvsPlayer } from '@capgo/ivs-player';// プレーヤーイベントをリッスンIvsPlayer.addListener('onState', (event) => {console.log('プレーヤーの状態:', event.state);// 状態: idle, buffering, ready, playing, ended});IvsPlayer.addListener('onError', (event) => {console.error('プレーヤーエラー:', event.error);});IvsPlayer.addListener('onDuration', (event) => {console.log('ビデオの長さ:', event.duration);});IvsPlayer.addListener('onProgress', (event) => {console.log('現在位置:', event.position);});// タイムドメタデータをリッスンIvsPlayer.addListener('onMetadata', (event) => {console.log('タイムドメタデータ:', event.metadata);}); -
高度な使用方法
import { IvsPlayer } from '@capgo/ivs-player';export class StreamPlayer {private playerId: string | null = null;private listeners: any[] = [];async initialize(streamUrl: string) {// カスタム設定でプレーヤーを作成const result = await IvsPlayer.create({url: streamUrl,autoplay: false,muted: true, // 自動再生ポリシーのためミュートで開始controls: true});this.playerId = result.playerId;this.setupEventListeners();}private setupEventListeners() {// 状態変更const stateListener = IvsPlayer.addListener('onState', (event) => {this.handleStateChange(event.state);});this.listeners.push(stateListener);// 品質変更const qualityListener = IvsPlayer.addListener('onQuality', (event) => {console.log(`品質が次に変更されました: ${event.quality.name}`);});this.listeners.push(qualityListener);// バッファ更新const bufferListener = IvsPlayer.addListener('onBuffer', (event) => {console.log(`バッファ: ${event.percentage}%`);});this.listeners.push(bufferListener);}private handleStateChange(state: string) {switch (state) {case 'playing':console.log('ストリームが再生中');break;case 'buffering':console.log('ストリームがバッファリング中');break;case 'ended':console.log('ストリームが終了');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() {// リスナーを削除this.listeners.forEach(listener => listener.remove());// プレーヤーを破棄if (this.playerId) {await IvsPlayer.destroy({ playerId: this.playerId });}}}
API リファレンス
Section titled “API リファレンス”create(options: CreateOptions)
Section titled “create(options: CreateOptions)”新しい IVS プレーヤーインスタンスを作成します。
パラメータ:
options: プレーヤー設定url: string - IVS 再生 URLautoplay: boolean - 自動再生を開始muted: boolean - ミュートで開始controls: boolean - ネイティブコントロールを表示
戻り値: Promise<{ playerId: string }>
play(options: PlayerOptions)
Section titled “play(options: PlayerOptions)”再生を開始します。
pause(options: PlayerOptions)
Section titled “pause(options: PlayerOptions)”再生を一時停止します。
stop(options: PlayerOptions)
Section titled “stop(options: PlayerOptions)”再生を停止して位置をリセットします。
seekTo(options: SeekOptions)
Section titled “seekTo(options: SeekOptions)”特定の位置にシークします。
setVolume(options: VolumeOptions)
Section titled “setVolume(options: VolumeOptions)”プレーヤーのボリュームを設定します(0.0 から 1.0)。
setQuality(options: QualityOptions)
Section titled “setQuality(options: QualityOptions)”再生品質を設定します。
destroy(options: PlayerOptions)
Section titled “destroy(options: PlayerOptions)”プレーヤーインスタンスを破棄します。
onState: プレーヤーの状態変更onError: 再生エラーonDuration: ビデオの長さが利用可能onProgress: 再生進行状況の更新onQuality: 品質変更onMetadata: タイムドメタデータイベントonBuffer: バッファステータスの更新
インターフェース
Section titled “インターフェース”interface CreateOptions { url: string; autoplay?: boolean; muted?: boolean; controls?: boolean;}
interface PlayerOptions { playerId: string;}
interface SeekOptions extends PlayerOptions { position: number; // 秒}
interface VolumeOptions extends PlayerOptions { volume: number; // 0.0 から 1.0}
interface QualityOptions extends PlayerOptions { quality: string; // 品質名}プラットフォームに関する注記
Section titled “プラットフォームに関する注記”- iOS 12.0 以降が必要
- IVS 拡張機能を備えたネイティブ AVPlayer を使用
- iPad でピクチャーインピクチャーをサポート
Android
Section titled “Android”- Android 5.0(API レベル 21)以降が必要
- Amazon IVS Player SDK を使用
- ハードウェアアクセラレーションを推奨
一般的なユースケース
Section titled “一般的なユースケース”- ライブストリーミング: リアルタイムイベント、スポーツ、ゲーム
- インタラクティブストリーミング: 視聴者投票、Q&A セッション
- E コマース: 製品ハイライト付きライブショッピング
- 教育: タイムドコンテンツ付きライブクラス
- エンターテインメント: 観客との対話付きコンサート、ショー
ベストプラクティス
Section titled “ベストプラクティス”-
ネットワーク処理: 接続品質を監視
IvsPlayer.addListener('onError', (event) => {if (event.error.includes('network')) {// ネットワークエラーを処理showRetryButton();}}); -
リソース管理: 使用後は必ずプレーヤーを破棄
-
自動再生ポリシー: 信頼性の高い自動再生のためミュートで開始
-
品質選択: IVS に自動品質切り替えを処理させる
トラブルシューティング
Section titled “トラブルシューティング”ストリームが再生されない:
- IVS 再生 URL が有効であることを確認
- ネットワーク権限を確認
- ストリームがライブであることを確認
高遅延:
- IVS は低遅延用に最適化されているため、エンコーダー設定を確認
- IVS 固有の再生 URL を使用していることを確認
品質の問題:
- 自動品質切り替えを許可
- ネットワーク帯域幅の要件を確認