Hey all, I'm working on my my first love2d game and I've run into a small problem with love.audio. I'm trying to implement an enenyOnHit noise and so in my love.load I initialized the sound effect:
The problem I ran into at first was that this would only successfully play once every couple of seconds or so. I noticed on the wiki there was a love.audio.rewind(). Since I'm streaming the sound files I assumed that I may have to rewind the source to place the "stream pointer" back at the front manually faster than Love seemed to do it on its own. So I did:
Think of sources as... Well, sources. A trumpet, for example. A trumpet can only make one sound at a time. You can't have one trumpeteer (I may just have made that word up) play two notes at the same time. The same goes for sound sources. Once you've played a sound source, you can't play it again until it has finished playing the first time.
What you do instead is to initialize your sound file as a sounddata object, and every time it's supposed to be played, you create a new source from that sounddata. Once the source has finished playing, you remove it.
One way to handle this is by writing a soundmanager that keeps track of all your sources and removes the ones that aren't needed anymore. You can view an example here: http://github.com/LOVE-Party/Love-LD18/ ... anager.lua
Think of sources as... Well, sources. A trumpet, for example. A trumpet can only make one sound at a time. You can't have one trumpeteer (I may just have made that word up) play two notes at the same time. The same goes for sound sources. Once you've played a sound source, you can't play it again until it has finished playing the first time.
What you do instead is to initialize your sound file as a sounddata object, and every time it's supposed to be played, you create a new source from that sounddata. Once the source has finished playing, you remove it.
One way to handle this is by writing a soundmanager that keeps track of all your sources and removes the ones that aren't needed anymore. You can view an example here: http://github.com/LOVE-Party/Love-LD18/ ... anager.lua
function playSFX(file)
local sfx = love.audio.newSource(file)
love.audio.play(sfx)
end
Note: I only use this for one-off sound effects, not for the background music.
Bad, bad idea! What will happen once you've played 1000 sounds?
EDIT: Hmm, or maybe the garbage collector will take care of it. I'm not entirely sure.
GC grabbing it was my assumption as well, but after playtesting and having it crashing on me after putting this in where it's never crashed before, I'm curious wtf happened, haha. I'll sort it out
Not that I know what that piece of code does, but I suspect it turns off the garbage collector - which certainly wouldn't be a solution.
The reason the game crashed now, I suspect, is because your sound source is being cleaned up by the garbage collector after your function has been run - before it has stopped playing. To fix it, set up a sound queue, add every sound to that, remove the sounds once they've been played. Pretty simple.