• Print

Author Topic: Attempt to try using Sound.PlayURL  (Read 16921 times)

0 Members and 1 Guest are viewing this topic.

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Attempt to try using Sound.PlayURL
« on: October 24, 2015, 07:11:28 am »
Recently I have given up the hunt to find a radio addon so I tried to custom make one using ULX features and sound.PlayURL. The command works fine and runs but does not play audio. I get this error:
rcon ulx ytaudio https://www.youtube.com/watch?v=bbDY5bRcZrw
autokick is disabled for WispySkies
L 10/24/2015 - 09:08:25: Lua Error:
[ERROR] addons/ulx/lua/ulx/modules/sh/yttosound.lua:5: attempt to call field 'PlayURL' (a nil value)
  1. call - addons/ulx/lua/ulx/modules/sh/yttosound.lua:5
   2. __fn - addons/ulib/lua/ulib/shared/commands.lua:942
    3. unknown - addons/ulib/lua/ulib/shared/commands.lua:1295
     4. unknown - lua/includes/modules/concommand.lua:54



[ERROR] addons/ulx/lua/ulx/modules/sh/yttosound.lua:5: attempt to call field 'PlayURL' (a nil value)
  1. call - addons/ulx/lua/ulx/modules/sh/yttosound.lua:5
   2. __fn - addons/ulib/lua/ulib/shared/commands.lua:942
    3. unknown - addons/ulib/lua/ulib/shared/commands.lua:1295
     4. unknown - lua/includes/modules/concommand.lua:54

Here is the code I have for the script:
Code: Lua
  1. local CATEGORY_NAME = "Wispy's Fun House"
  2.  
  3. function ulx.ytaudio( command )
  4.        
  5.         sound.PlayURL ( "URL", "3d", function( station )
  6.         if ( IsValid( station ) ) then
  7.  
  8.                 station:SetPos( LocalPlayer():GetPos() )
  9.  
  10.                 station:Play()
  11.  
  12.         else
  13.  
  14.                 LocalPlayer():ChatPrint( "URL is not valid." )
  15.  
  16.         end
  17.         end )
  18. end
  19. local ytaudio = ulx.command( CATEGORY_NAME, "ulx ytaudio", ulx.ytaudio, "!ytaudio" )
  20. ytaudio:defaultAccess( ULib.ACCESS_SUPERADMIN )
  21. ytaudio:help( "Plays a valid link." )

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Attempt to try using Sound.PlayURL
« Reply #1 on: October 24, 2015, 07:36:19 am »
You're trying to run sound.PlayURL() on the server-side, but that function only exists on the client-side.

I suggest to have the ULX command trigger some client-side code that plays the audio. You could use the Net library for that.

Just a heads-up - sound.PlayURL requires a direct link (.mp3, .ogg, .pls) to an audio file. Links to a YouTube video won't work.
« Last Edit: July 11, 2018, 12:19:54 am by Timmy »

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Attempt to try using Sound.PlayURL
« Reply #2 on: October 24, 2015, 08:59:10 am »
Thanks for the clarification. I like to experiment with Lua as I barely know it. Is there any way so if I typed !ytaudio LINK it would actually play the Audio from the YouTube link to everyone.

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Attempt to try using Sound.PlayURL
« Reply #3 on: October 24, 2015, 11:03:15 am »
You can try to play the video in an invisible browser.

That won't work on OS X. We don't have Adobe Flash or support for the HTML5 video element in Awesomium (the build-in browser). ;_;

I believe that Flash is supported if you're on Windows though. If that's correct, most people will be able to play the video just fine.

Assuming you're on Windows: You can test if videos play properly for you by setting the ULX MOTD to a video URL, then opening it.

If the video plays, you could probably get away with opening an invisible browser on the client when that command runs.
« Last Edit: October 24, 2015, 11:05:19 am by Timmy »

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Attempt to try using Sound.PlayURL
« Reply #4 on: October 24, 2015, 11:12:48 am »
*claps* everyone is on Mac .-. So there is no way that it's possible to have it play audio to everyone by typing !ytaudio LINK. Sorry if I'm being persistent, I'm really curious how stuff like this works.

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Attempt to try using Sound.PlayURL
« Reply #5 on: October 24, 2015, 11:25:11 am »
Aww, that sucks! I can't think of a convenient, hack-free solution that would do what you want. :c

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Attempt to try using Sound.PlayURL
« Reply #6 on: October 24, 2015, 11:26:47 am »
Yeah .-. Although thanks for trying to help! One day I'll figure it out.

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Attempt to try using Sound.PlayURL
« Reply #7 on: October 25, 2015, 05:27:14 am »
*hopes that there is a solution and someone who sees this would know*

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Attempt to try using Sound.PlayURL
« Reply #8 on: October 27, 2015, 10:54:29 am »
The one thing that seems impossible yet so persistent that even the worst feel the same as those who know what happens to a man. Some quote. Back on topic, anyone?

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Attempt to try using Sound.PlayURL
« Reply #9 on: October 27, 2015, 06:05:56 pm »
Bumping this thread constantly isn't going to get you anywhere. If the in-game HTML can't display a youtube video on Macs, then you can't play the audio from it. Yes it's unfortunate, but there isn't any way to get around it.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Attempt to try using Sound.PlayURL
« Reply #10 on: October 27, 2015, 07:41:56 pm »
Interesting discussion -
https://github.com/Facepunch/garrysmod-requests/issues/398
After reading most of it, I can't tell if it's Awesomium that doesn't support Mac/Video tag, or if it's just Gmod not running latest supported.
Either way, nothing can be done _here_ on this forum code wise, so, yeah, don't keep bumping until you find proof Youtube will work in Gmod on Mac.

EDIT - I KNOW, write a Lua VM that runs Windows and HTML5 more properly!
*ducks*
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Attempt to try using Sound.PlayURL
« Reply #11 on: October 27, 2015, 09:43:09 pm »
I got something. Relies on the API provided by youtubeinmp3.com. It's not ideal, but, I tried a few songs and it seems to work fine. :D

Disclaimer: Code below should be used for educational purposes only.
Code: Lua
  1. if SERVER then
  2.     -- Pool network strings
  3.     util.AddNetworkString("PlayYouTubeAudio")
  4. end
  5.  
  6. local function APIRequest(videoID, callback)
  7.     -- Build API URL
  8.     local url = {}
  9.     url[1] = "https://youtubeinmp3.com/fetch/?format=JSON&video="
  10.     url[2] = "https://www.youtube.com/watch?v="
  11.     url[3] = videoID
  12.     url = table.concat(url)
  13.  
  14.     http.Fetch(url, function(body, len, headers, code)
  15.         if code ~= 200 then
  16.             return callback(false, "API request failed")
  17.         end
  18.  
  19.         local json = util.JSONToTable(body)
  20.  
  21.         if not json then
  22.             local shortenerUrl = "http://tny.im/yourls-api.php"
  23.             local shortenerParams = {
  24.                 action = "shorturl",
  25.                 url = url,
  26.                 format = "json"
  27.             }
  28.             http.Post(shortenerUrl, shortenerParams, function(body, len, headers, code)
  29.                 -- Beg that the URL shortener doesn't fail!
  30.                 return callback(false, "Go to " .. util.JSONToTable(body).shorturl .. ", click \"Convert & Download MP3\" then try requesting this song again.")
  31.             end)
  32.         elseif json.error then
  33.             return callback(false, json.error)
  34.         else
  35.             return callback(true, json)
  36.         end
  37.     end)
  38. end
  39.  
  40. if CLIENT then
  41.     -- This function plays audio from URL
  42.     local station
  43.     local function play(url)
  44.         if IsValid(station) then station:Stop() end
  45.         sound.PlayURL(url, "", function(_station)
  46.             station = _station
  47.             if IsValid(station) then
  48.                 station:Play()
  49.             end
  50.         end)
  51.     end
  52.  
  53.     -- Net hook
  54.     net.Receive("PlayYouTubeAudio", function()
  55.         APIRequest(net.ReadString(), function(ok, data)
  56.             if not ok then
  57.                 print(data)
  58.                 return false
  59.             end
  60.             play(data.link)
  61.         end)
  62.     end)
  63. end
  64.  
  65. -- Create ULX command
  66. local CATEGORY_NAME = "YouTube"
  67.  
  68. function ulx.youtube(calling_ply, videoID)
  69.     if string.len(videoID) ~= 11 then
  70.         ULib.tsayError(calling_ply, "Invalid video ID. Example: !youtube Y6ljFaKRTrI")
  71.         return false
  72.     end
  73.  
  74.     APIRequest(videoID, function(ok, data)
  75.         if not ok then
  76.             ULib.tsayError(calling_ply, data)
  77.             return false
  78.         end
  79.  
  80.         net.Start("PlayYouTubeAudio")
  81.         net.WriteString(videoID)
  82.         net.Broadcast()
  83.  
  84.         ulx.fancyLogAdmin(calling_ply, "#A played song: #s", data.title)
  85.     end)
  86. end
  87. local youtube = ulx.command(CATEGORY_NAME, "ulx youtube", ulx.youtube, "!youtube")
  88. youtube:addParam{type=ULib.cmds.StringArg, hint="YouTube video ID", ULib.cmds.takeRestOfLine}
  89. youtube:defaultAccess(ULib.ACCESS_ADMIN)
  90. youtube:help("Plays audio from a YouTube video.")
Desc: Plays the audio from a YouTube video for all connected clients.
Usage: !youtube videoID (I didn't want to deal with extracting the id for all possible YouTube video urls)
Example: !youtube Y6ljFaKRTrI
Caveats: Occasionally you'll get this
You can tell people to type "stopsound" in their client console if they don't want to hear the song


Might also be worth monitoring this GitHub issue for updates: Adobe Flash isn't loaded on Mac OS X

Edit: Fix small mistake in the code
« Last Edit: April 19, 2016, 09:07:03 am by Timmy »

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Attempt to try using Sound.PlayURL
« Reply #12 on: October 28, 2015, 04:23:16 am »
Bumping this thread constantly isn't going to get you anywhere. If the in-game HTML can't display a youtube video on Macs, then you can't play the audio from it. Yes it's unfortunate, but there isn't any way to get around it.
;) yeah
Interesting discussion -
https://github.com/Facepunch/garrysmod-requests/issues/398
After reading most of it, I can't tell if it's Awesomium that doesn't support Mac/Video tag, or if it's just Gmod not running latest supported.
Either way, nothing can be done _here_ on this forum code wise, so, yeah, don't keep bumping until you find proof Youtube will work in Gmod on Mac.

EDIT - I KNOW, write a Lua VM that runs Windows and HTML5 more properly!
*ducks*

I thought about a VM, but still wasn't sure. I also considered having a file that downloads the audio to a server, and puts it in FTP but that requires another server and a restart .-.
I got something. Relies on the API provided by youtubeinmp3.com. It's not ideal, but, I tried a few songs and it seems to work fine. :D
Code: Lua
  1. if SERVER then
  2.     -- Pool network strings
  3.     util.AddNetworkString("PlayYouTubeAudio")
  4. end
  5.  
  6. local function APIRequest(videoID, callback)
  7.     -- Build API URL
  8.     local url = {}
  9.     url[1] = "https://youtubeinmp3.com/fetch/?format=JSON&video="
  10.     url[2] = "https://www.youtube.com/watch?v="
  11.     url[3] = videoID
  12.     url = table.concat(url)
  13.  
  14.     http.Fetch(url, function(body, len, headers, code)
  15.         if code ~= 200 then
  16.             return callback(false, "API request failed")
  17.         end
  18.  
  19.         local json = util.JSONToTable(body)
  20.  
  21.         if not json then
  22.             local shortenerUrl = "http://tny.im/yourls-api.php"
  23.             local shortenerParams = {
  24.                 action = "shorturl",
  25.                 url = url,
  26.                 format = "json"
  27.             }
  28.             http.Post(shortenerUrl, shortenerParams, function(body, len, headers, code)
  29.                 -- Beg that the URL shortener doesn't fail!
  30.                 return callback(false, "Go to " .. util.JSONToTable(body).shorturl .. ", click \"Convert & Download MP3\" then try requesting this song again.")
  31.             end)
  32.         elseif json.error then
  33.             return callback(false, json.error)
  34.         else
  35.             return callback(true, json)
  36.         end
  37.     end)
  38. end
  39.  
  40. if CLIENT then
  41.     -- This function plays audio from URL
  42.     local station
  43.     local function play(url)
  44.         if IsValid(station) then station:Stop() end
  45.         sound.PlayURL(url, "", function(_station)
  46.             station = _station
  47.             if IsValid(station) then
  48.                 station:Play()
  49.             end
  50.         end)
  51.     end
  52.  
  53.     -- Net hook
  54.     net.Receive("PlayYouTubeAudio", function()
  55.         APIRequest(net.ReadString(), function(ok, data)
  56.             if not ok then
  57.                 print(data)
  58.                 return false
  59.             end
  60.             play(data.link)
  61.         end)
  62.     end)
  63. end
  64.  
  65. -- Create ULX command
  66. local CATEGORY_NAME = "YouTube"
  67.  
  68. function ulx.youtube(calling_ply, videoID)
  69.     if string.len(videoID) ~= 11 then
  70.         ULib.tsayError(calling_ply, "Invalid video ID. Example: !youtube Y6ljFaKRTrI")
  71.         return false
  72.     end
  73.  
  74.     APIRequest(videoID, function(ok, data)
  75.         if not ok then
  76.             ULib.tsayError(calling_ply, data)
  77.             return false
  78.         end
  79.  
  80.         net.Start("PlayYouTubeAudio")
  81.         net.WriteString(videoID)
  82.         net.Broadcast()
  83.  
  84.         ulx.fancyLogAdmin(calling_ply, "#A played song: #s", data.title)
  85.     end)
  86. end
  87. local youtube = ulx.command(CATEGORY_NAME, "ulx youtube", ulx.youtube, "!youtube")
  88. youtube:addParam{type=ULib.cmds.StringArg, hint="YouTube video ID", ULib.cmds.takeRestOfLine}
  89. youtube:defaultAccess(ULib.ACCESS_ADMIN)
  90. youtube:help("Plays audio from a YouTube video.")

Desc: Plays the audio from a YouTube video for all connected clients.
Usage: !youtube videoID (I didn't want to deal with extracting the id for all possible YouTube video urls)
Example: !youtube Y6ljFaKRTrI
Caveats: Occasionally you'll get this
You can tell people to type "stopsound" in their client console if they don't want to hear the song


Might also be worth monitoring this GitHub issue for updates: Adobe Flash isn't loaded on Mac OS X

Edit: Fix small mistake in the code
Looks alright, I'll try it! Hahaha persistent prevails *chicken cough cough* I must be sick.
« Last Edit: October 28, 2015, 04:26:55 am by WispySkies »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Attempt to try using Sound.PlayURL
« Reply #13 on: October 28, 2015, 03:13:44 pm »
Be warned, using any 3rd party site that grabs Youtube content is 1) against Youtube TOS and 2) may be like playing whack-a-mole - those sites frequently get changed/taken down/blocked by Youtube.
They keep track of referrals, if too many referrals come from a 'download our content so you can do what you want with it' site, they take action.
You're also not supporting your local "pirate" site because you can't see their ads while downloading content not intended by Youtube. :P
You're killing children and puppies this way!
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Attempt to try using Sound.PlayURL
« Reply #14 on: October 28, 2015, 04:59:35 pm »
You're killing children and puppies this way!
Puppies noooooooooooooo!

  • Print