sound files fail to play

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
cvbnmme4th
Prole
Posts: 2
Joined: Mon Jun 20, 2011 6:32 pm

sound files fail to play

Post 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?
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: sound files fail to play

Post 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.
cvbnmme4th
Prole
Posts: 2
Joined: Mon Jun 20, 2011 6:32 pm

Re: sound files fail to play

Post by cvbnmme4th »

Ah, that's worked. Thank you very much. I'm very new to this.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: sound files fail to play

Post 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. :)
Help us help you: attach a .love.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: sound files fail to play

Post 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
Post Reply

Who is online

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