Can't Get YouTube Video Duration

I’m using the video player for YouTube videos. I inputted my link, and the page loads fine. I created a button to get the duration on-click, but it’s not working. Can someone please explain?

result = 0.

Here’s the button code:

$w("#button110").onClick(event => {
    let duration = $w("#videoPlayer1").duration.toString();
    $w("#text487").text = duration;

I’ve attached a screenshot so you can see the YT video “is” loaded in the player, that the button is there and working, and that the result when I click the button, which is “0”…

Duration is an option for this player, so I’m lost.

Any ideas?

Hello David,
There might be a couple of potential issues that could prevent the duration from being displayed correctly.

Timing: When accessing the duration property of the video player, it’s crucial to ensure that the video has loaded completely. Otherwise, the duration might not be available. You can handle this by waiting for the onReady event of the video player. Here’s an example of how you can modify your code to include the onReady event:

$w("#videoPlayer1").onReady(() => {
    let duration = $w("#videoPlayer1").duration.toString();
    $w("#text487").text = duration;

By wrapping the duration retrieval code inside the onReady event handler, you ensure that the duration is accessed only after the video player is ready.

Element IDs: Double-check that the IDs used in your code (#button110, #videoPlayer1, #text487) match the actual IDs of the elements on your Wix page. If the IDs don’t match, the code won’t be able to find the correct elements and won’t update the text accordingly.
Make sure that the button, video player, and text element IDs are correct and that they exist on your Wix page.

By addressing these potential issues and ensuring that the video player has loaded before accessing the duration, you should be able to retrieve and display the correct duration of the YouTube video when the button is clicked.