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.
sphyrth
Citizen
Posts: 87
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: A question about music

Post by sphyrth » Thu Jun 20, 2019 1:01 pm

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
Yes.png (14.15 KiB) Viewed 2028 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: 28
Joined: Thu Apr 04, 2019 3:32 pm

Re: A question about music

Post by CrimsonGuy » Thu Jun 20, 2019 7:08 pm

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

User avatar
pgimeno
Party member
Posts: 1691
Joined: Sun Oct 18, 2015 2:58 pm

Re: A question about music

Post by pgimeno » Fri Jun 21, 2019 10:05 am

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.

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

Re: A question about music

Post by zorg » Fri Jun 21, 2019 3:46 pm

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

function love.load()
	
	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 :3True 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests