メディアプレーヤーのプレイリストの再生
このトピックでは、onEndedプロパティのJavaScriptロジックを実装して、最初のビデオの再生が完了したら2番目のビデオの再生を自動的に開始する例を紹介します。
前提条件
WC3メディアプレーヤーを使用するようにアプリを設定します。詳細については、メディアプレーヤーのセットアップを参照してください。
プレイリストの統合
App.tsxを開き、コード例に示されているように変更します。このコードでは、コンテンツをコンテンツの配列に変換し、別のメディアアイテムのエントリを追加しています。その後、onEndedプロパティを登録し、現在のコンテンツの読み込みを解除して、onEndedイベントがトリガーされたら次のコンテンツの再生を開始しています。以下で強調表示されているステップは、コンテンツプレイリストの再生に関連するものです。
コード例
以下のコード例では、非アダプティブコンテンツを再生する2つの方法を示します。
VideoPlayerAPIを使用する方法Videoコンポーネントを使用する方法
VideoPlayer API
src/App.tsxファイルを開き、その内容を以下のコードに置き換えます。
Vega SDKを使用してアプリをビルドし、デバイスまたはシミュレーターで実行してログを収集します。アプリを作成してシミュレーターで実行する方法の詳細については、Vegaアプリの作成およびVega仮想デバイスを参照してください。
/*
* Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All rights reserved.
*
* 専有/機密情報。 使用にあたってはライセンス条項が適用されます。
*/
import * as React from 'react';
import {useRef, useState, useEffect} from 'react';
import {
Platform,
useWindowDimensions,
View,
StyleSheet,
TouchableOpacity,
Text,
} from 'react-native';
import {
VideoPlayer,
VegaVideoSurfaceView,
VegaCaptionsView,
} from '@amazon-devices/react-native-w3cmedia';
import {ShakaPlayer, ShakaPlayerSettings} from './shakaplayer/ShakaPlayer';
// アプリからビデオの再生APIを手動で呼び出す場合は、falseに設定します。
const AUTOPLAY = true;
const DEFAULT_ABR_WIDTH: number = Platform.isTV ? 3840 : 1919;
const DEFAULT_ABR_HEIGHT: number = Platform.isTV ? 2160 : 1079;
const content = [
{
secure: 'false', // true: セキュアなビデオバッファーを使用します。false: 非セキュアなビデオバッファーを使用します。
uri: 'https://storage.googleapis.com/shaka-demo-assets/angel-one/dash.mpd',
drm_scheme: '', // com.microsoft.playready, com.widevine.alpha
drm_license_uri: '', // DRMライセンス取得サーバーのURL(コンテンツがDRMで保護されている場合にのみ必要)
},
];
export const App = () => {
const currShakaPlayerSettings = useRef<ShakaPlayerSettings>({
secure: false, // 再生をセキュアモードまたは非セキュアモードで実行
abrEnabled: true, // アダプティブビットレート(ABR)の切り替えを有効化
abrMaxWidth: DEFAULT_ABR_WIDTH, // ABRで許容される最大幅
abrMaxHeight: DEFAULT_ABR_HEIGHT, // ABRで許容される最大高さ
});
const player = useRef<any>(null);
const videoPlayer = useRef<VideoPlayer | null>(null);
const timeoutHandler = useRef<ReturnType<typeof setTimeout> | null>(null);
const [playerSettings, setPlayerSettings] = useState<ShakaPlayerSettings>(
currShakaPlayerSettings.current,
);
const [buttonPress, setButtonPress] = useState(false);
const [nextContent, setNextContent] = useState({index: 0}); // { index: number }
// 再レンダリングのためにnextContentの状態を追跡します。
const nextContentRef = useRef<number>(0);
// 全画面解像度でレンダリングします。
const {width: deviceWidth, height: deviceHeight} = useWindowDimensions();
useEffect(() => {
if (nextContent.index !== nextContentRef.current) {
nextContentRef.current = nextContent.index;
// <Video>コンポーネントを強制的に再レンダリングします。
initializeVideoPlayer();
setNextContent((prev) => {
return {...prev};
});
}
}, [nextContent]);
useEffect(() => {
console.log('app:AppPreBuffering v13.0を開始します');
initializeVideoPlayer();
}, []);
const onEnded = async () => {
console.log('app:onEndedを受け取りました');
player.current.unload();
player.current = null;
await videoPlayer.current?.deinitialize();
removeEventListeners();
onVideoUnMounted();
setNextContent({index: (nextContent.index + 1) % content.length});
};
const onError = () => {
console.log(`app: AppPreBuffering:エラーイベントリスナーが呼び出されました`);
};
const setUpEventListeners = (): void => {
console.log('app:イベントリスナーをセットアップします');
videoPlayer.current?.addEventListener('ended', onEnded);
videoPlayer.current?.addEventListener('error', onError);
};
const removeEventListeners = (): void => {
console.log('app:イベントリスナーを削除します');
videoPlayer.current?.removeEventListener('ended', onEnded);
videoPlayer.current?.removeEventListener('error', onError);
};
const initializeVideoPlayer = async () => {
console.log('app:initializeVideoPlayerが呼び出されました');
videoPlayer.current = new VideoPlayer();
// @ts-ignore
global.gmedia = videoPlayer.current;
await videoPlayer.current.initialize();
setUpEventListeners();
videoPlayer.current!.autoplay = false;
initializeShaka();
};
const onSurfaceViewCreated = (surfaceHandle: string): void => {
console.log('app:サーフェスが作成されました');
videoPlayer.current?.setSurfaceHandle(surfaceHandle);
videoPlayer.current?.play();
};
const onSurfaceViewDestroyed = (surfaceHandle: string): void => {
videoPlayer.current?.clearSurfaceHandle(surfaceHandle);
};
const onCaptionViewCreated = (captionsHandle: string): void => {
console.log('app:キャプションビューが作成されました');
videoPlayer.current?.setCaptionViewHandle(captionsHandle);
};
const initializeShaka = () => {
console.log('app: in initializePlayer() index = ', nextContent.index);
if (videoPlayer.current !== null) {
player.current = new ShakaPlayer(videoPlayer.current, playerSettings);
}
if (player.current !== null) {
player.current.load(content[nextContent.index], AUTOPLAY);
}
};
const onVideoUnMounted = (): void => {
console.log('app: in onVideoUnMounted');
// @ts-ignore
global.gmedia = null;
videoPlayer.current = null;
};
if (!buttonPress) {
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.button}
onPress={() => {
setButtonPress(true);
}}
hasTVPreferredFocus={true}
activeOpacity={1}>
<Text style={styles.buttonLabel}>ここを押してビデオを再生</Text>
</TouchableOpacity>
</View>
);
} else {
return nextContent.index === nextContentRef.current ? (
<View style={styles.videoContainer}>
<VegaVideoSurfaceView
style={styles.surfaceView}
onSurfaceViewCreated={onSurfaceViewCreated}
onSurfaceViewDestroyed={onSurfaceViewDestroyed}
/>
<VegaCaptionsView
onCaptionViewCreated={onCaptionViewCreated}
style={styles.captionView}
/>
</View>
) : (
<View style={styles.videoContainer}></View>
);
}
};
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: '#283593',
justifyContent: 'center',
alignItems: 'center',
},
button: {
alignItems: 'center',
backgroundColor: '#303030',
borderColor: 'navy',
borderRadius: 10,
borderWidth: 1,
paddingVertical: 12,
paddingHorizontal: 32,
},
buttonLabel: {
color: 'white',
fontSize: 22,
fontFamily: 'Amazon Ember',
},
videoContainer: {
backgroundColor: 'white',
alignItems: 'stretch',
},
surfaceView: {
zIndex: 0,
},
captionView: {
width: '100%',
height: '100%',
top: 0,
left: 0,
position: 'absolute',
backgroundColor: 'transparent',
flexDirection: 'column',
alignItems: 'center',
zIndex: 2,
}
});
Videoコンポーネント
src/App.tsxファイルを開き、その内容を以下のコードに置き換えます。
Vega SDKを使用してアプリをビルドし、デバイスまたはシミュレーターで実行してログを収集します。アプリを作成してシミュレーターで実行する方法の詳細については、Vegaアプリの作成およびVega仮想デバイスを参照してください。
/*
* Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All rights reserved.
*
* 専有/機密情報。 使用にあたってはライセンス条項が適用されます。
*/
import * as React from "react";
import { useRef } from "react";
import { useWindowDimensions, View, StyleSheet } from "react-native";
import { Video } from "@amazon-devices/react-native-w3cmedia";
// アプリからビデオの再生APIを手動で呼び出す場合は、falseに設定します。
const AUTOPLAY = true;
const content = {
uri: "https://storage.googleapis.com/shaka-demo-assets/angel-one/dash.mpd",
};
export const App = () => {
const video = useRef<Video | null>(null);
// 全画面解像度でレンダリングします。
const { width: deviceWidth, height: deviceHeight } = useWindowDimensions();
console.log("AppNonAdaptiveVidoe v1.0");
return (
<View style={styles.videoContainer}>
<Video
autoplay={AUTOPLAY}
src={content.uri}
width={deviceWidth}
height={deviceHeight}
controls={true}
ref={(ref) => {
video.current = ref;
}}
/>
</View>
);
};
const styles = StyleSheet.create({
videoContainer: {
backgroundColor: "white",
alignItems: "stretch",
},
});
関連リソース
Last updated: 2025年9月30日

