Jump to content

[Question]Using getSoundLength for timer


kieran

Recommended Posts

  • Moderators

I still can't understand why you need a timer for it.

On 09/10/2017 at 03:05, kieran said:

Hi, I am trying to make a script that will play a random sound from a table and then continue to play sounds, but I have a problem with the timer, all sounds play at once instead of one after the other...

Can you explain better, please? Because reading your quote, I assume you want the next song to play after the previous one finished, and the code I wrote works that way.

Anyway, If you want to access the timer variable globally, just do as I did with 'playingSound' variable in my code above.

Link to comment
34 minutes ago, DNL291 said:

I still can't understand why you need a timer for it.

Can you explain better, please? Because reading your quote, I assume you want the next song to play after the previous one finished, and the code I wrote works that way.

Anyway, If you want to access the timer variable globally, just do as I did with 'playingSound' variable in my code above.

Worked perfect, and yes, I had a problem first with milliseconds, but then I realized I set it to *100 and not *1000 of sounds length xD

But after that I had a problem making my timer global as it was local inside another function, tried your code and works perfect!  Thanks :)

Did make one edit though, a useful tip I found out is...  Always remove a onClientResourceStart handler if it's just when the player logs on or joins, prevents it restarting :)

Edited by kieran
Link to comment
  • 2 weeks later...
songs = {
	'sounds/1.mp3',
	'sounds/2.mp3',
	'sounds/3.mp3',
	'sounds/4.mp3',
	'sounds/5.mp3',
	'sounds/6.mp3',
	'sounds/7.mp3',
	'sounds/8.mp3'
}

local previous
function randomIndex()
	local number = math.random(1,#songs)
	if not previous == number then
		previous = number
		return number
	end
end

function nextSong()
	if isSoundFinished(sound) then
		playSong()
	else
		setTimer(nextSong,2000,1)
	end
end

local sound
function playSong()
  	local i = randomIndex()
	sound = playSound(songs[i],true)
	if isElement(sound) then
		setSoundVolume(sound,0.8)
  		local length = getSoundLength(sound)
  		if length > 1 then length = length+2000 end
  		setTimer(nextSong,length,1)
	end
end
addEventHandler('onClientResourceStart',resourceRoot,playSong)

function stopLoginSound()
  	if isElement(sound) then
		destroyElement(sound)
   	end
end
addEvent('stoploginsound',true)
addEventHandler('stoploginsound',root,stopLoginSound)

This is simply proper code formatting, I've got no idea if it works. If it does then it does. It's suppose to lol. Good luck.

Just remember getSoundLength returns in milliseconds and a timer also uses milliseconds... 1000 milliseconds = 1 second.

Edited by ShayF
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...