Jump to content

Making a song playlist


ShayF2

Recommended Posts

I need some help with a sound system I am working on. I want to make a simple command based song playlist. Eventually I'll add pause features, stop and play and all that. Maybe even a gui, but for now I gotta get this working. Someone please help me get this working.

Server.lua -

local root = getRootElement()

function addPublic(player,cmd,sound)
	if sound then
		if string.find(tostring(sound), "http://") == nil and string.find(tostring(sound), "https://") == nil then
			sound = string.format("http://%s",tostring(sound))
		end
		triggerClientEvent("addSong",root,player,sound)
		outputChatBox(getPlayerName(player).." has just added "..sound.." to the playlist!",root,0,255,255)
	end
end
addCommandHandler("addsong",addPublic)

Client.lua -

local root = getRootElement()
local localPlayer = getLocalPlayer()
local playlist = {}
local publicSound = playSound("http://test.mp3/",false)
currentIndex = 0

addEvent("addSong",true)
addEventHandler("addSong",root,function(player,theSound)
	table.insert(playlist,theSound)
end)

setTimer(function()
	if isSoundPaused(publicSound) then
		for i,sounds in pairs(playlist) do
			if i == currentIndex then
				destroyElement(publicSound)
				publicSound = playSound(sounds,false)
				setSoundVolume(publicSound,1)
				currentIndex = currentIndex + 1
				outputChatBox("Now Playing!: "..sounds,0,255,255)
			end
		end
	end
end,1000,0)

addCommandHandler("vol",function(cmd,vol)
	local v = vol / 100
	setSoundVolume(publicSound,v)
	outputChatBox("Public sound volume set to: "..tostring(vol).."%",0,255,255)
end)

 

Link to comment

If you like it to be synced with the server then you're gonna want to get the length of every song before adding them to a server-side playlist/table. Then use those lengths to set a timer to loop through it on the server and trigger the client. If you don't need it synced then you can probably just make use of these events https://wiki.multitheftauto.com/wiki/Client_Scripting_Events#Sound_events. onClientSoundStream, onClientSoundStopped etc. 

for example onClienSoundStream you can check if something is playing if not you add it to the table, then make another check whether to play it. And on stop you can remove the table entry and play the next one.

Link to comment
1 hour ago, Tails said:

If you like it to be synced with the server then you're gonna want to get the length of every song before adding them to a server-side playlist/table. Then use those lengths to set a timer to loop through it on the server and trigger the client. If you don't need it synced then you can probably just make use of these events https://wiki.multitheftauto.com/wiki/Client_Scripting_Events#Sound_events. onClientSoundStream, onClientSoundStopped etc. 

for example onClienSoundStream you can check if something is playing if not you add it to the table, then make another check whether to play it. And on stop you can remove the table entry and play the next one.

I've never done this before, could you like write out an example or something to help?

Link to comment
3 hours ago, Tails said:

If you like it to be synced with the server then you're gonna want to get the length of every song before adding them to a server-side playlist/table. Then use those lengths to set a timer to loop through it on the server and trigger the client. If you don't need it synced then you can probably just make use of these events https://wiki.multitheftauto.com/wiki/Client_Scripting_Events#Sound_events. onClientSoundStream, onClientSoundStopped etc. 

for example onClienSoundStream you can check if something is playing if not you add it to the table, then make another check whether to play it. And on stop you can remove the table entry and play the next one.

I got the script to work. I debugged it (the hard way aka adding messages after every function) When some messages didn't show I knew what was wrong. It was not matching the index. I thought that the first index in a table was 0, but it's 1, I changed the default index as 1 and it started working. Thank you for helping. However I have a new problem. The next song wont start.

Server.lua

function addPublic(player,cmd,sound)
	if sound then
		triggerClientEvent("addSong",root,sound,player)
		outputChatBox(getPlayerName(player).." has just added "..sound.." to the playlist!",root,0,255,255)
	end
end
addCommandHandler("addsong",addPublic)

Client.lua

playlist = {}
currentIndex = 1
enabled = false

addEvent("addSong",true)
addEventHandler("addSong",root,function(sound,player)
	if string.find(tostring(sound), "http://") == nil and string.find(tostring(sound), "https://") == nil then
			sound = string.format("http://%s",tostring(sound))
	end
	table.insert(playlist,{song = sound,by = player})
	if enabled == false then
		enabled = true
		playIt()
	end
end)

function playIt()
	for i,sounds in ipairs(playlist) do
		if i == currentIndex then
			if publicSound then
				destroyElement(publicSound)
			end
			publicSound = playSound(sounds.song,false)
			setSoundVolume(publicSound,1)
			outputChatBox("Now Playing: "..sounds.song.." added by: "..getPlayerName(sounds.by),0,255,255)
			timer1 = getSoundLength(publicSound)
			setTimer(playIt,timer1+1000,1)
			currentIndex = currentIndex+1
		end
	end
end



addCommandHandler("vol",function(cmd,vol)
	local v = vol / 100
	setSoundVolume(publicSound,v)
	outputChatBox("Public sound volume set to: "..tostring(vol).."%",0,255,255)
end)

 

Edited by shay103
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...