• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Trying to make an audio file play once

Pkirkby

Member
Hi everyone, this one's fairly simple,for someone experienced anyway. I have a simple audio file that plays when my street lights come on, but it wants to loop even with the loop turned to false. It just plays over and over, I've tried setting variables to shut it off, but not sure what direction to take it to make this work. Here's my simple code right now:

if (!audio_is_playing(sfxLightBuzz))
{
audio_play_sound_at(sfxLightBuzz, oFLoatingLampOn.x, oFLoatingLampOn.y, 0, 0, 100, 300, 1, true)
audio_sound_gain(sfxLightBuzz,0.3,500)
}

Any ideas? This seems like there should be a simple option to have it play once.
 

rIKmAN

Member
Hi everyone, this one's fairly simple,for someone experienced anyway. I have a simple audio file that plays when my street lights come on, but it wants to loop even with the loop turned to false. It just plays over and over, I've tried setting variables to shut it off, but not sure what direction to take it to make this work. Here's my simple code right now:

if (!audio_is_playing(sfxLightBuzz))
{
audio_play_sound_at(sfxLightBuzz, oFLoatingLampOn.x, oFLoatingLampOn.y, 0, 0, 100, 300, 1, true)
audio_sound_gain(sfxLightBuzz,0.3,500)
}

Any ideas? This seems like there should be a simple option to have it play once.
You have the the "loop" argument in audio_play_sound_at set to 1 which will evaluate to true and so set it to loop.
Change the 1 to either a 0 or as is shown in the manual to
false.
 

rIKmAN

Member
Oops, I pasted the old code, I have changed it already to false and it still replays. Good eye though. Not sure why it'd still loop.
In that case it'll be because of the way you have the if check setup.

Every time the audio is not playing that code block will run and play the sound again, over and over which makes it appear to be looping.
Toggle a flag variable when it has been played so you can check against that to see if it's already been played.
 

Pkirkby

Member
In that case it'll be because of the way you have the if check setup.

Every time the audio is not playing that code block will run and play the sound again, over and over which makes it appear to be looping.
Toggle a flag variable when it has been played so you can check against that to see if it's already been played.
Yeah, that's kind of where I'm at. I had it turn a variable to false, but then because there is no delay between playing the audio, it shut it off right away, and we dont hear it at all. I thought of doing an alarm, but I refuse to do it that way unless its the only option. I haven't seen an option to say "audio_done_play" or something like that, how would I go about it? Sorry if that's a noob question, and thanks for the replies.
 

rIKmAN

Member
Yeah, that's kind of where I'm at. I had it turn a variable to false, but then because there is no delay between playing the audio, it shut it off right away, and we dont hear it at all. I thought of doing an alarm, but I refuse to do it that way unless its the only option. I haven't seen an option to say "audio_done_play" or something like that, how would I go about it? Sorry if that's a noob question, and thanks for the replies.
I'm not sure what events you are using the code in, but something like this should work to play it only once if it needs to be done in an event that runs every frame

Create Event
Code:
buzzHasPlayed = false;
Step Event
Code:
if (!buzzHasPlayed)
{
    audio_play_sound_at(sfxLightBuzz, oFLoatingLampOn.x, oFLoatingLampOn.y, 0, 0, 100, 300, false, true)
    audio_sound_gain(sfxLightBuzz,0.3,500)
 
    // set the flag to true so it doesn't play again
    buzzHasPlayed = true;
}
However I'm almost certain this is not the best way to achieve what you are trying to do.

You should be able to play this sound as a one off in the same place that you turn your lights on/off as presumably they don't keep flipping on and off each frame, so you must already have some kind of logic to make sure that only happens once (or it's in an event that only fires once / intermittently) so you should be able to play it there without any issues or having it playing repeatedly.

Without knowing how you have things setup it's hard to say - but that's the general logic above if you are forced to do it that way for whatever reason.
 

Pkirkby

Member
I'm not sure what events you are using the code in, but something like this should work to play it only once if it needs to be done in an event that runs every frame

Create Event
Code:
buzzHasPlayed = false;
Step Event
Code:
if (!buzzHasPlayed)
{
    audio_play_sound_at(sfxLightBuzz, oFLoatingLampOn.x, oFLoatingLampOn.y, 0, 0, 100, 300, false, true)
    audio_sound_gain(sfxLightBuzz,0.3,500)
 
    // set the flag to true so it doesn't play again
    buzzHasPlayed = true;
}
However I'm almost certain this is not the best way to achieve what you are trying to do.

You should be able to play this sound as a one off in the same place that you turn your lights on/off as presumably they don't keep flipping on and off each frame, so you must already have some kind of logic to make sure that only happens once (or it's in an event that only fires once / intermittently) so you should be able to play it there without any issues or having it playing repeatedly.

Without knowing how you have things setup it's hard to say - but that's the general logic above if you are forced to do it that way for whatever reason.
That did it, I was over complicating some things. Thanks alot for your help.
 
Top