-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I use realtime currentTime of YouTube Video? #9
Comments
@wooogler Im super happy that this library is of help to you :)
Unfortunately, the link you provided isn't working for me. What format do you want the duration in? I will give you a quick example for it. rg - |
Here's a simple example import React, {useState, useRef, useEffect} from 'react';
import {Text, View} from 'react-native';
import YoutubePlayer from 'react-native-youtube-iframe';
const App = () => {
const [elapsed, setElapsed] = useState(0);
const playerRef = useRef();
useEffect(() => {
const interval = setInterval(async () => {
const elapsed_sec = await playerRef.current.getCurrentTime(); // this is a promise. dont forget to await
// calculations
const elapsed_ms = Math.floor(elapsed_sec * 1000);
const ms = elapsed_ms % 1000;
const min = Math.floor(elapsed_ms / 60000);
const seconds = Math.floor((elapsed_ms - min * 60000) / 1000);
setElapsed(
min.toString().padStart(2, '0') +
':' +
seconds.toString().padStart(2, '0') +
':' +
ms.toString().padStart(3, '0'),
);
}, 100); // 100 ms refresh. increase it if you don't require millisecond precision
return () => {
clearInterval(interval);
};
}, []);
return (
<>
<YoutubePlayer
height={250}
ref={playerRef}
videoId={'DC471a9qrU4'}
/>
<View>
<View style={{flexDirection: 'row'}}>
<Text style={{flex: 1}}>{'elapsed time'}</Text>
<Text style={{flex: 1, color: 'green'}}>{elapsed}</Text>
</View>
</>
);
}; screenshot - |
It's very kind of you!! Thank you for your quick response! |
no problem :) Although
|
I understand. I try to use setInterval. Thank you! |
Hi, LonelyCpp.
I'm actually trying to use your component.
It's more stable and easier than react-native-youtube. It's great!
I need to synchronize the state with YouTube Video currentTime.
But, I cannot find how to do that.
I tried to use setInterval and it worked, but currentTime was a float number.
So, I use toFixed, and Math.floor, then I can see the short change from float to integer number.
I need the function like elapsed time in this demo https://cookpete.com/react-player/.
May I ask you the solution for this problem?
The text was updated successfully, but these errors were encountered: