Hi Siva Prakash,
I’ve tried setting it up and running it, but the result still isn’t working — every time I open the video, it only shows a black screen and doesn’t play. Could you please help me check what might be wrong? Thank you so much.
Here is my code
//PlayerScreen.tsx
import React, {useRef, useEffect, useState} from 'react';
import {StyleSheet, View, Text, TouchableOpacity, Platform} from 'react-native';
import {
VideoPlayer,
KeplerVideoSurfaceView,
KeplerCaptionsView,
} from '@amazon-devices/react-native-w3cmedia';
import {
ShakaPlayer,
ShakaPlayerSettings,
} from '../../w3cmedia/shakaplayer/ShakaPlayer';
// ⚙️ Video sample (DASH)
const content = {
secure: false,
uri: 'https://cdn.pixabay.com/video/2019/04/06/22634-328940142_large.mp4',
drm_scheme: '',
drm_license_uri: '',
};
const DEFAULT_ABR_WIDTH = Platform.isTV ? 3840 : 1919;
const DEFAULT_ABR_HEIGHT = Platform.isTV ? 2160 : 1079;
export default function PlayerScreen() {
const currShakaPlayerSettings = useRef<ShakaPlayerSettings>({
secure: false,
abrEnabled: true,
abrMaxWidth: DEFAULT_ABR_WIDTH,
abrMaxHeight: DEFAULT_ABR_HEIGHT,
});
const videoPlayer = useRef<VideoPlayer | null>(null);
const shakaPlayer = useRef<any>(null);
const [isReady, setIsReady] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [playerSettings] = useState<ShakaPlayerSettings>(
currShakaPlayerSettings.current,
);
useEffect(() => {
let isMounted = true;
const setup = async () => {
await initVideoPlayer();
};
setup();
return () => {
if (isMounted) {
cleanup();
}
isMounted = false;
};
}, []);
const initVideoPlayer = async () => {
try {
setIsLoading(true);
videoPlayer.current = new VideoPlayer();
await videoPlayer.current.initialize();
videoPlayer.current.autoplay = false;
shakaPlayer.current = new ShakaPlayer(
videoPlayer.current,
playerSettings,
);
console.log('✅ Shaka Player created');
await shakaPlayer.current.load(content, true);
console.log('✅ Shaka content loaded');
setIsReady(true);
} catch (err) {
console.error('❌ Error initializing video player:', err);
} finally {
setIsLoading(false);
}
};
// 🧹 Cleanup player
const cleanup = async () => {
console.log('🧹 Cleaning up player...');
try {
await videoPlayer.current?.deinitialize();
} catch (err) {
console.warn('Error cleaning up:', err);
} finally {
videoPlayer.current = null;
shakaPlayer.current = null;
}
};
// Surface & Caption handles
const onSurfaceCreated = (surfaceHandle: string) => {
console.log('Surface created:', surfaceHandle);
videoPlayer.current?.setSurfaceHandle(surfaceHandle);
};
const onSurfaceDestroyed = (surfaceHandle: string) => {
console.log('Surface destroyed:', surfaceHandle);
videoPlayer.current?.clearSurfaceHandle(surfaceHandle);
};
const onCaptionCreated = (captionHandle: string) => {
console.log('Caption created:', captionHandle);
videoPlayer.current?.setCaptionViewHandle(captionHandle);
};
return (
<View style={styles.container}>
{isReady ? (
<>
<KeplerVideoSurfaceView
style={styles.surface}
onSurfaceViewCreated={onSurfaceCreated}
onSurfaceViewDestroyed={onSurfaceDestroyed}
/>
<KeplerCaptionsView
onCaptionViewCreated={onCaptionCreated}
style={styles.captionView}
/>
</>
) : (
<TouchableOpacity
style={styles.playButton}
onPress={initVideoPlayer}
hasTVPreferredFocus={true}>
<Text style={styles.playLabel}>Start video</Text>
</TouchableOpacity>
)}
{isLoading && (
<View style={styles.overlay}>
<Text style={{color: 'white'}}>Loading video...</Text>
</View>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {flex: 1, backgroundColor: 'black', justifyContent: 'center'},
surface: {width: '100%', height: '100%'},
captionView: {
width: '100%',
height: '100%',
top: 0,
left: 0,
position: 'absolute',
backgroundColor: 'transparent',
flexDirection: 'column',
alignItems: 'center',
zIndex: 2,
},
overlay: {
...StyleSheet.absoluteFillObject,
justifyContent: 'center',
alignItems: 'center',
zIndex: 10,
backgroundColor: 'rgba(0,0,0,0.3)',
},
playButton: {
backgroundColor: '#303030',
padding: 20,
borderRadius: 10,
alignSelf: 'center',
},
playLabel: {color: '#fff', fontSize: 20},
});
//tsconfig.json
{
"extends": "@tsconfig/react-native/tsconfig.json",
"compilerOptions": {
"types": ["jest", "@amazon-devices/react-native-kepler"],
"typeRoots": [
"src/w3cmedia/shakaplayer",
"node_modules/@types"
]
},
"exclude": [
"src/shakaplayer/dist"
]
}
Regards,
Hoang Dang