How to play video

Hello, i have question i wanna make play youtube video in game (no in loading) how to make function on lua to play video from htmll ? i using this function in html to play video in loading <iframe width="100%" height="100%" src="https://www.youtube.com/embed/_HCR4w0ABVg?autoplay=1&%3Fvq=hd720;controls=0;&amp;showinfo=0" frameborder="0" allow="autoplay" allowfullscreen=""></iframe> (Video is example)

Send a nui message then detect using js in nui if you got a video then play the video

2 Likes

@TheIndra can you show me example please?..

On phone, someone else on the forums might be able to give you an example :smile:

Uh; I would look into some documentation on JS about doing autoplay videos… I know for real web based applications, you can no-longer autoplay a youtube video with sound without giving user controls, but there are some hacky workouts.

https://www.w3schools.com/html/html5_video.asp

The whole w3schools site is an incredibly valuable resource for anything HTML/JS/CSS related.

can you give me example ? i am lostedd…

i need execute video from lua function…

1 Like

i dont know how to play it wia function example function playyoutubevideo

Make a function which sends a nui message for example {action:“play”}

Then if you receive that in NUI then use YouTube JavaScript api or something to play/display the video

1 Like

@TheIndra can you help me with that on discord ? please ?.. i need only client function to play it… and nui…

1 Like

Let’s keep the discussion on the forums, can’t really provide any examples as I’m on vacation

Searching the docs and Google should provide you with enough information

i searching it and i didnt found nothing

@TheIndra can you help me with that ? after you when are you on computer? I still haven’t found anything. I’d be very grateful to you

Typed from phone

In Lua you make a function

function PlayYoutube(videoid)
   SendNuiMessage({action = "play", video = videoid})
end

Then in nui have a div or something which is display:none

Then as js you would have a event listener

window.addEventListener("message", function(event) {
    if (event.data.action == "play") {
        // unhide div and play video using YouTube is api or load an iframe
    }
})

This should help you a little

@TheIndra like this ? window.addEventListener("message", function(event) { if (event.data.action == "play") { <iframe width="100%" height="100%" src="https://www.youtube.com/embed/_HCR4w0ABVg?autoplay=1&%3Fvq=hd720;controls=0;&amp;showinfo=0" frameborder="0" allow="autoplay" allowfullscreen=""></iframe> } })

You can’t just use html in js

You would have to set the iframe src using js

document.getElementById("iframediv").src = "https://www.youtube.com/embed/" + event.data.video + "?autoplay=1"

Something like that

@TheIndra like this ?

window.addEventListener("message", function(event) {
    if (event.data.action == "play") {
       document.getElementById("iframediv").src = "https://www.youtube.com/embed/" + event.data.video + "?autoplay=1"
    }
})

If you have your html right probably, not sure if I did the .src right as I’m on phone you’d have to google but I gave you a good example

@TheIndra still dont worked…when will you be on pc? I’d be very grateful to you if you help me…

<div id="hide" style="display:none;">
    <iframe id="frame" width="100%" height="100%" frameborder="0" allow="autoplay" allowfullscreen=""></iframe>
</div>

html will be something like this

And your js

document.getElementById("hide").style.display = "block";
document.getElementById("frame").src =  "https://www.youtube.com/embed/" + event.data.video + "?autoplay=1"

In my head this should work, but not sure as I’m on phone