Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
sphyrth
Party member
Posts: 108
Joined: Mon Jul 07, 2014 11:04 am
Contact:

### Re: A question about music

I think I was able to reproduce the error. You put the important section of code into love.load() instead of love.update().

This is the barebones of what the guys are trying to explain:
Yes.png (14.15 KiB) Viewed 3175 times
As a programming preference, I don't like getting a "newSource" everytime the music stops. I'd rather have all the music stored in an array instead.

CrimsonGuy
Prole
Posts: 35
Joined: Thu Apr 04, 2019 3:32 pm

### Re: A question about music

^In general loading any type of resource (music or images) on every tick of the update will lead to a big performance hit.

pgimeno
Party member
Posts: 1738
Joined: Sun Oct 18, 2015 2:58 pm

### Re: A question about music

Well, music is not stopped at every tick, so it should not do that. But loading the next song on the fly could lead to pauses in the middle of the game, which is a good point by sphyrth.

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

### Re: A question about music

So in the end, a baseline could be something like this:

Code: Select all

local music = {}
local music_volume = 0.5
local current_music = 0
local count_music = 11

for i=0,count_music-1 do
music[i] = love.audio.newSource("music/mus_".. i ..".mp3", "stream" )
music[i]:setVolume(music_volume)
end

music[0]:play()

end

function love.update(dt)
if not music[current_music]:isPlaying() then
current_music = (current_music + 1) % count_music -- from 0 to 10
-- current_music = love.math.random(0,count_music-1) -- if you want to shuffle
music[current_music]:play()
end
end

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.

### Who is online

Users browsing this forum: Bing [Bot], Google [Bot], zorg and 10 guests