Audio not working correctly

My audio file gets loaded like this in the main section of my program:

hitSound = audio.loadStream("audio/hit\_target.mp3")

The sound should get played when a target gets hit, so my Runtime onEnterEvent fuction looks like this:

local function onEnterFrame(event)
    if(targetsHit > 0) then
        local hitSoundChannel = audio.play(hitSound)
        print(hitSoundChannel)
    end
end

The print statement was added for debugging. When multiple targets get hit in short succession, I would expect multiple audio channels to be opened, i.e. the sounds should overlap. However, the output of the print statement shows that only ever one channel is opened (#1) and unless that channel has finished playing the sound, audio.play returns 0 as an error code:

23:28:36.395  1
23:28:36.457  0
23:28:36.552  0
23:28:36.661  0
23:28:36.802  0
23:28:37.786  1
23:28:37.848  0
23:28:37.895  0
23:28:37.942  0
23:28:37.973  0
23:28:38.020  0
23:28:38.255  1
23:28:38.380  0
23:28:38.426  0

Why wouldn’t it allocate the next available channel as per the documentation instead?

Perhaps thiswill help

Also, I’d be careful about staring and sounds in an enterFrame listener the way you are doing.  

That code looks like it will start a new sound playing every frame that targetHit is > 0, without limit or safety.

Better to play a sound as a result of a game event like a collision or death/damage of a player.

Thank you for your replies.

@roaminggamer: that’s good advice, I have lots more logic in the listener to take care of that, I just didn’t want to post unnecessary code on the forum

@JonPM: I wasn’t aware of that, so I added it to my code. Something very unexpected happened then, instead of the 1 0 0 0 1 0 0 0 … I would now get 2 1 1 1 2 1 1 1 2 1… so no more error return value, but still not the desired result.

I played some more around with it and finally resolved it by changing from audio.loadStream to audio.loadSound. Not sure why that made a difference, but now it works perfectly fine.

Perhaps thiswill help

Also, I’d be careful about staring and sounds in an enterFrame listener the way you are doing.  

That code looks like it will start a new sound playing every frame that targetHit is > 0, without limit or safety.

Better to play a sound as a result of a game event like a collision or death/damage of a player.

Thank you for your replies.

@roaminggamer: that’s good advice, I have lots more logic in the listener to take care of that, I just didn’t want to post unnecessary code on the forum

@JonPM: I wasn’t aware of that, so I added it to my code. Something very unexpected happened then, instead of the 1 0 0 0 1 0 0 0 … I would now get 2 1 1 1 2 1 1 1 2 1… so no more error return value, but still not the desired result.

I played some more around with it and finally resolved it by changing from audio.loadStream to audio.loadSound. Not sure why that made a difference, but now it works perfectly fine.