• Print

Author Topic: sound.PlayURL Help  (Read 5961 times)

0 Members and 1 Guest are viewing this topic.

Offline Bloody

  • Newbie
  • *
  • Posts: 1
  • Karma: -1
sound.PlayURL Help
« on: April 05, 2018, 09:36:56 pm »
I am kind of new to deving  and I really do Not know how to make this work or what is wrong with this .

local TextEntry = vgui.Create( "DTextEntry", qframe)
            TextEntry:SetPos( 25, 80 )
            TextEntry:SetSize( 300, 30 )
            TextEntry:SetText( "Paste Url" )
            TextEntry.OnEnter =  function()
            RunConsoleCommand("qmusic")
            local url = TextEntry:GetValue() -- So basicaly when you hit enter it sets the varible url and runs console command. But this is not the issue
   end
   
   end
end
concommand.Add( "qmusic", function (station) sound.PlayURL ( url, "3d"  --- this function is the issue, it will just not work.
   if ( IsValid( station ) ) then

      station:SetPos( LocalPlayer():GetPos() )

      station:Play()

   else

      LocalPlayer():ChatPrint( "Invalid URL!" )

   end
end ))

Please help,  thx.
« Last Edit: April 06, 2018, 06:56:29 am by Bloody »

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: sound.PlayURL Help
« Reply #1 on: April 05, 2018, 10:28:42 pm »
Please make sure your code is formatted before posting. Also use [code=lua] tags to insert your code. Also be sure to define what you mean by "it will just not work". Are there any errors? Is there any output/function?

Your concommand isn't receiving the url. In the last line of your TextEntry.OnEnter() you do
Code: Lua
  1. local url = TextEntry:GetValue()
. This value is never used, so your concommand isn't receiving it. I'd recommend making a separate function for playing the URL:

Code: Lua
  1. function play( url )
  2.         local ply = LocalPlayer()
  3.         sound.PlayURL( url, "3d", function( station )
  4.                 if ( IsValid( station ) ) then
  5.                         station:SetPos( ply:GetPos() )
  6.                         station:Play()
  7.                 else
  8.                         ply:ChatPrint( "Invalid URL" )
  9.                 end
  10.         end )
  11. end
  12.  

and make your OnEntry() call this function with the specified url:
Code: Lua
  1. TextEntry.OnEnter = function()
  2.         local url = TextEntry:GetValue()
  3.         play( url )
  4. end
  5.  

You could also make a concommand that uses this by recreating the TextEntry and then calling this function
Code: Lua
  1. concommand.Add( "qmusic", function( ply )
  2.         -- Create the TextEntry again (along with the frame and everything you did)
  3.         TextEntry.OnEnter = function()
  4.                 local url = TextEntry:GetValue()
  5.                 play( url )
  6.         end
  7. end )
  8.  
« Last Edit: April 05, 2018, 10:33:11 pm by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Vaporouscreeper

  • Newbie
  • *
  • Posts: 22
  • Karma: 0
    • TheBox steam group
Re: sound.PlayURL Help
« Reply #2 on: April 06, 2018, 03:08:29 am »
You're trying to call a Non existing function

try something like this
Code: Lua
  1. function name()
  2.    sound.PlayURL(URL as a string, string flags, function(audio)
  3.       --code when it plays
  4.       audio:SetPos( LocalPlayer():GetPos() )
  5.       audio:Play()
  6.    end)
  7. end
  8. concommand.Add(command name,name())
  9.  
more info of sound.PlayURL
http://wiki.garrysmod.com/page/sound/PlayURL
« Last Edit: April 06, 2018, 03:17:07 am by Vaporouscreeper »
I'm a Moderator on a server called TheBox. and i'm mostly active on it

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: sound.PlayURL Help
« Reply #3 on: April 06, 2018, 03:31:05 am »
Merged two threads on the same topic -- please only post in one place.
Experiencing God's grace one day at a time.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: sound.PlayURL Help
« Reply #4 on: April 06, 2018, 10:15:28 am »
Taken from my PMs:
Quote from: Bloody on April 06, 2018, 07:03:01 am
Hello thank you for responding  to my post,  but It still will not work. So what is happening is there is no lua errors at all. The thing that will not work is when I put the link in it still says invalidurl. I do not understand why this will not work.


Code: Lua
  1.         local TextEntry = vgui.Create( "DTextEntry", qframe)
  2.                 TextEntry:SetPos( 25, 80 )
  3.                 TextEntry:SetSize( 300, 30 )
  4.                 TextEntry:SetText( "Paste Url" )
  5.                 TextEntry.OnEnter =  function()
  6.                         local url = TextEntry:GetValue()
  7.                         play( url )
  8.                 end
  9.         end
  10. end
  11. function play( url )
  12.         local ply = LocalPlayer()
  13.         sound.PlayURL( url , "3d", function( station )
  14.                 if ( IsValid( station ) ) then
  15.                         station:SetPos( ply:GetPos() )
  16.                         station:Play()
  17.                 else
  18.                         ply:ChatPrint( "Invalid URL" )
  19.                 end
  20.         end )
  21. end

Replace the play function with this and tell me the output:
Code: Lua
  1. function play( url )
  2.         local ply = LocalPlayer()
  3.         sound.PlayURL( url , "3d", function( station, id, name )
  4.                 if ( IsValid( station ) ) then
  5.                         station:SetPos( ply:GetPos() )
  6.                         station:Play()
  7.                 else
  8.                         ply:ChatPrint( "Invalid URL: " .. id .. " " .. name )
  9.                 end
  10.         end )
  11. end
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print