sound.PlayURL Youtube

So I’m just trying to play sounds from youtube by URL, I’m using an API referenced here.

I’m pretty sure what I’m doing is really amateur and stupid but I’m probably not going to figure it out.

Anyhow here is my code,
[lua]
http.Fetch(“www.youtubeinmp3.com/fetch/?format=JSON&video=”…data, function(body, len, headers, code)
local Table = util.JSONToTable(body);

sound.PlayURL(Table[1]["link"], "mono", function(sound)
	sound:Play();
end);

end);
[/lua]

What are you having trouble with?

Regardless, I suggest checking if the sound is valid, as a broken url will not work, therefore your code is likely going to error.

If you’re having issues with the table structure, use PrintTable to visualize it. I made this a while back, that recursively prints the table but as a Lua table.

Number #1


hook.Add( "PlayerSay", "Youtube Sound", function( ply, txt, bTeam )
	
	if ( string.sub( txt, 1, 8 ) == "/youtube" ) then
		local clip = string.sub( txt, 9, 100 )
	    sound.PlayURL(clip, "mono", function(sound)
                sound:SetPos( LocalPlayer():GetPos() )
		sound:Play();
		
		return ""
    end
	
end )

You could try to see if the sound would work via commands in chat…
example: /youtube www.youtubeinmp3.com/fetch/?format=JSON&video=CODEWORKS <-- Random name…

and you seem to be missing…


sound:SetPos( LocalPlayer():GetPos() )

clearly, we are playing a sound… at nil to nil. So we need to set it to the player… maybe thats ur error?
Example:


http.Fetch("www.youtubeinmp3.com/fetch/?format=JSON&video="..data, function(body, len, headers, code)
	local Table = util.JSONToTable(body);

	sound.PlayURL(Table[1]["link"], "mono", function(sound)
                sound:SetPos( LocalPlayer():GetPos() ) -- Before the SOUND:PLAY() or it will play the sound.. then set it.. basically not work...
		sound:Play();
	end);
end);

So hope this helps…