Page 1 of 1

sound files fail to play

Posted: Mon Jun 20, 2011 6:45 pm
by cvbnmme4th
I am trying to add background music to my game however, despite checking the wiki and making sure that my code was correct, when I run my .love file, no sound plays. My code is:

Code: Select all

function love.play ()
	music = love.audio.newSource("castle.mp3", "stream")
	love.audio.play("music")
	love.audio.setVolume(1.0)
end
I have tried this with a .ogg of the file and it still doesnt work. I am having the same problem with images simply failing to load. I am very new to Love and am probably making some moronic mistake here. I have made sure that the sound file is in the top level of the .zip along with main.lua, is that wrong?

Re: sound files fail to play

Posted: Mon Jun 20, 2011 7:27 pm
by Kadoba
Try changing "love.play ()" to "love.load()"

*edit*
Also don't put "music" in quotes when giving it to love.audio.play. That will just turn it into a string.

Re: sound files fail to play

Posted: Mon Jun 20, 2011 8:06 pm
by cvbnmme4th
Ah, that's worked. Thank you very much. I'm very new to this.

Re: sound files fail to play

Posted: Mon Jun 20, 2011 9:02 pm
by Robin
cvbnmme4th wrote:I'm very new to this.
Are you just new to LÖVE, or new to Lua or even programming altogether?

It's not a problem either way, but it might help us help you in the future. :)

Re: sound files fail to play

Posted: Tue Jun 21, 2011 6:55 am
by T-Bone
Putting a bit of text inside quotes (" "), for example "apple", in almost any programming language means that you're referring to the word apple, as in a sort of list of letters ('a','p','p','l' and 'e') (this is called a string), while writing just apple without quotes means you're referring to apple as a symbol. Since a computer doesn't know what an apple is from the beginning, you would have to define it first for your computer to get what you mean. Look at your example:

Code: Select all

function love.play ()
   music = love.audio.newSource("castle.mp3", "stream")
   love.audio.play(music)
   love.audio.setVolume(1.0)
end
First, you define what music is. From now on, the word music (without quotes) is well defined, as a song in this case. When you define it using love.audio.newSource(), note that both "castle.mp3" and "stream" are in quotes. You are telling your computer to go look for a file called excactly "castle.mp3", to do this it needs the word "castle.mp3", not some symbol.

Note that you could have done this too:

Code: Select all

function love.play ()
   source = "castle.mp3"
   soundtype = "stream"
   music = love.audio.newSource(source, soundtype)
   love.audio.play(music)
   love.audio.setVolume(1.0)
end
In this case, source and soundtype are symbols referring to the strings "castle.mp3" and "stream" respectively. If you would have written "source" inside newSource here, it would have looked for a file called source, instead of looking at what source symbolizes, which in this case is "castle.mp3".

Hopefully this could demonstrate the difference between writing something in quotes and not doing so :P