Page 1 of 2

A question about music

Posted: Mon Jun 17, 2019 1:47 pm
by Keisser
Greetings everyone.
I have music files in a folder and I want them to constantly play at background, so, when one ends, another one starts playing. Unfortunately, I don't see any possibility in Love2d to detect the end of sound file. Is it even possible?

Re: A question about music

Posted: Tue Jun 18, 2019 7:48 pm
by raidho36
If it's not looped, it stops playing at the end. As in, the playsate changes to "stopped". You can detect this and play the next track.

Re: A question about music

Posted: Wed Jun 19, 2019 6:17 am
by TheHUG

Re: A question about music

Posted: Wed Jun 19, 2019 5:02 pm
by Keisser
Well I created this code and it does not seem to work properly.

Code: Select all

----

local music_volume = 0.5
--local sound_volume = 1

----
music = love.audio.newSource("music/mus_"..math.random(11)..".mp3", "stream" )
----

if music:isPlaying( ) == false then
	math.randomseed( os.time() )
	math.random(); math.random(); math.random(); math.random(); math.random()
	music = love.audio.newSource("music/mus_"..math.random(11)..".mp3", "stream" )
	love.audio.play( music )
	music:setVolume(music_volume)
end
Music starts at the beginning of the game but as soon as it ends, nothing happens.

Re: A question about music

Posted: Wed Jun 19, 2019 5:25 pm
by Andlac028
Simply put

Code: Select all

if music:isPlaying( ) == false then
	math.randomseed( os.time() )
	math.random(); math.random(); math.random(); math.random(); math.random()
	music = love.audio.newSource("music/mus_"..math.random(11)..".mp3", "stream" )
	love.audio.play( music )
	music:setVolume(music_volume)
end
into love.update()

Re: A question about music

Posted: Wed Jun 19, 2019 6:15 pm
by zorg
People, people, no need to reinvent the wheel if you only want a track to loop from start to end, there's a method for that: Source:setLooping

Example:

Code: Select all

music = love.audio.newSource("music/mus_"..math.random(11)..".mp3", "stream" )
music:setLooping(true)

-- Then call music:play() somewhere in your code that's not the update function.


Edit: I seem to have misread the actual issue; this only loops one track. I will give an edited version of Andlac'syour code since it's bad to use the lua random generator when löve provides a better one.

Code: Select all

if not music:isPlaying( ) then
	music = love.audio.newSource("music/mus_"..love.math.random(11)..".mp3", "stream" )
	-- ^ or love.math.random(1,11) if you lack a mus_0.mp3
	music:setVolume(music_volume)
	love.audio.play( music )
end

Re: A question about music

Posted: Wed Jun 19, 2019 6:30 pm
by Keisser
I put the code into love_update() and nothing is playing now. This neither works when I have it in a separate .lua file nor in main.lua.

Re: A question about music

Posted: Wed Jun 19, 2019 6:48 pm
by grump
Keisser wrote: Wed Jun 19, 2019 5:02 pm

Code: Select all

	math.randomseed( os.time() )
	math.random(); math.random(); math.random(); math.random(); math.random()
lmao
I put the code into love_update() and nothing is playing now. This neither works when I have it in a separate .lua file nor in main.lua.
This would be the perfect time to post the exact code that shows the issue that can be run with a minimum amount of effort for anyone else trying to reproduce it.

Re: A question about music

Posted: Thu Jun 20, 2019 5:50 am
by raidho36
Yeesh, that guy's a grump allright.

The proper spelling is "love.update". The "update" function is in the table "love". Make sure you never define another "love.update" function, it'll overwrite the last one.

Re: A question about music

Posted: Thu Jun 20, 2019 12:48 pm
by grump
raidho36 wrote: Thu Jun 20, 2019 5:50 am Yeesh, that guy's a grump allright.
Wut? Nah. A grumpy response would have been a link to How To Ask Questions The Smart Way without further comment. Also, please don't assume my gender.