• Javascript/Youtube API not working in garrysmod
    8 replies, posted
hi iv recently noticed that if you have a panel and set its HTML, it doesnt call functions, nor can you use :RunJavascript("myfunc()") (it does nothing) i noticed this with the youtube api. the code works perfectly in a web browser but doesnt call functions in garrysmod. i might be doing it wrong or maybe it just doesnt call functions? im not sure. any insight to this would be helpful. code for anyone wondering: [code] <script type="text/javascript" src="http://118.88.20.34/~gambit/swfobject/src/swfobject.js"></script> <div id="ytapiplayer"> You need Flash player 8+ and JavaScript enabled to view this video. </div> <script type="text/javascript"> var ytplayer; var params = { allowScriptAccess: "always" }; var atts = { id: "ytplayer" }; swfobject.embedSWF("http://www.youtube.com/v/HExOCl3YypM?autoplay=1&iv_load_policy=3&enablejsapi=1&version=3&modestbranding=1&playerapiid=ytplayer&hl=en_US&rel=0&start=0", "ytapiplayer", "100%", "100%", "8", null, null, params, atts); function onYouTubePlayerReady(playerId){ ytplayer = document.getElementById("ytplayer"); ytplayer.setVolume(0); document.body.innerHTML = ":D"; } function changeVolume(iVolume) { if (ytplayer) ytplayer.setVolume(iVolume); } </script> [/code] it plays the video but you cant change the volume, and none of the functions are called (even if you call it through RunJavascript)
-bump-
While this sounds stupid... have you tried both [code] :RunJavascript("myfunc()") [/code] and [code] .RunJavascript("myfunc()") [/code] The old wiki says .Run and the new one says :Run
Javascript stuff works ofr me.
do you mean that the code i supplied works?
-bump again-
i updated the code to: [code] <head> <style type = 'text/css'> body{ margin: 0; padding: 0; border: 0; background: #000000; overflow: 'hidden'; } </style> </head> <body> <script type="text/javascript" src="http://118.88.20.34/~gambit/swfobject/src/swfobject.js"></script> <div id="ytapiplayer"> You need Flash player 8+ and JavaScript enabled to view this video. </div> <script type="text/javascript"> var ytplayer; var params = { allowScriptAccess: "always" }; var atts = { id: "ytplayer" }; swfobject.embedSWF("http://www.youtube.com/v/HExOCl3YypM?autoplay=1&iv_load_policy=3&enablejsapi=1&version=3&modestbranding=1&playerapiid=ytplayer&hl=en_US&rel=0&start=0", "ytapiplayer", "100%", "100%", "8", null, null, params, atts); function onYouTubePlayerReady(playerId){ ytplayer = document.getElementById("ytplayer"); console.log("ready"); } function changeVolume(iVolume) { console.log("volume changed"); if (ytplayer) ytplayer.setVolume(iVolume); } console.log("loaded!"); </script> </body> [/code] the result of loading a video is: [code] [HTML] loaded ] lua_run_cl theater = Entity(1):GetEyeTrace().Entity ] ] lua_run_cl print(theater.Browser) ] Panel: [name:Awesomium][class:CAwsHTML][0,0,1024,512] ] lua_run_cl theater.Browser:RunJavascript("changeVolume(10);") ] [HTML] volume changed ] lua_run_cl theater.Browser:RunJavascript("onYouTubePlayerReady();") ] [HTML] ready ] lua_run_cl theater.Browser:RunJavascript("changeVolume(10);") ] [HTML] volume changed ] lua_run_cl theater.Browser:RunJavascript("changeVolume(0);") ] [HTML] volume changed [/code] in short: Yes, RunJavascript works. No, callbacks are not internally called (onYouTubePlayerReady should be automatically called - but i had to manually call it) No, changing the volume still did not work in the end (still no idea why)
I don't have any issues using the YouTube API in Cinema. You can [URL="https://github.com/pixeltailgames/pixeltailgames.github.com/blob/master/cinema/js/theater.js#L226"]review my code[/URL] and see if it helps.
thanks, but i would really like to know why a function that - in a browser gets automatically - but i have to call manually ingame, and even when the object is setup, the volume wont change.
Sorry, you need to Log In to post a reply to this thread.