useDuration()
useDuration():
number
Returns the media duration in milliseconds.
This hook provides a reactive way to observe and respond to changes in the total playback duration — applicable only for Video on Demand (VOD) content.
Behavior
- VOD: Returns the full duration of the asset in milliseconds, from
0toduration. - LIVE: Not applicable. Live content does not have a fixed duration; use useSeekableRange instead to determine the playable time window.
⚠️ Must be used within a PlayerProvider or AVPlayerViewControllerProvider component.
Returns
number
number The total duration of the media in milliseconds.
Example
import React from 'react';
import { View, Text } from 'react-native';
import { useDuration, useLive } from '@castlabs/react-native-prestoplay';
export default function DurationInfo() {
const duration = useDuration();
const isLive = useLive();
if (isLive) {
return <Text>Duration not available for live streams</Text>;
}
return (
<View>
<Text>Total Duration: {Math.round(duration / 1000)} seconds</Text>
</View>
);
}