## A question about music

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Keisser
Prole
Posts: 3
Joined: Mon Jun 17, 2019 1:42 pm

### A question about music

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?

raidho36
Party member
Posts: 1797
Joined: Mon Jun 17, 2013 12:00 pm

### Re: A question about music

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.

TheHUG
Citizen
Posts: 50
Joined: Sun Apr 01, 2018 4:21 pm

### Re: A question about music

Keisser
Prole
Posts: 3
Joined: Mon Jun 17, 2019 1:42 pm

### Re: A question about music

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.

Andlac028
Prole
Posts: 5
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

### Re: A question about music

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()

zorg
Party member
Posts: 2619
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: A question about music

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
Last edited by zorg on Wed Jun 19, 2019 10:38 pm, edited 1 time in total.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Keisser
Prole
Posts: 3
Joined: Mon Jun 17, 2019 1:42 pm

### Re: A question about music

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.

grump
Party member
Posts: 576
Joined: Sat Jul 22, 2017 7:43 pm

### Re: A question about music

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.

raidho36
Party member
Posts: 1797
Joined: Mon Jun 17, 2013 12:00 pm

### Re: A question about music

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.

grump
Party member
Posts: 576
Joined: Sat Jul 22, 2017 7:43 pm

### Re: A question about music

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.

### Who is online

Users browsing this forum: No registered users and 8 guests