background 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.
Post Reply
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

background music

Post by jjmafiae »

im making a game and i need to have some looping background music but i cant figure out the code can someone help me? ;)
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: background music

Post by Boolsheet »

Welcome to LÖVE!

To get some music playing you have to create a Source with love.sound.newSource. This will load the encoded audio file and prepare it for playback. Set it to looping with Source:setLooping and then call Source:play to start playback. Use Source:setVolume to control its volume so it really is background music. ;)

Putting it all together:

Code: Select all

function love.load()
	-- Loading the audio file.
	Music = love.audio.newSource("rsar.ogg")

	-- Letting it loop forever.
	Music:setLooping(true)

	-- Making it a bit quieter.
	Music:setVolume(0.5)

	-- Starting playback.
	Music:play()
end

function love.keypressed(key)
	if key == "escape" then
		-- Exit on escape.
		love.event.quit()
	elseif key == " " then
		-- Play or stop on spacebar.
		if Music:isStopped() then
			Music:play()
		else
			Music:stop()
		end
	end
end
looping_source.love
(269 KiB) Downloaded 124 times
We recommend you use the Vorbis audio format. WAVE and mp3 may show some issues.
Shallow indentations.
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: background music

Post by jjmafiae »

thanks mate :D you have helped me alot
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 56 guests