Trying to make an audio file play once

Discussion in 'Programming' started by Pkirkby, Sep 13, 2019.

Tags:
  1. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    115
    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.
     
  2. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,453
    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.
     
  3. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    115
    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.
     
  4. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,453
    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.
     
  5. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    115
    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.
     
  6. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,453
    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.
     
  7. Pkirkby

    Pkirkby Member

    Joined:
    Mar 25, 2019
    Posts:
    115
    That did it, I was over complicating some things. Thanks alot for your help.
     
    rIKmAN likes this.
  8. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,453
    No problem, glad you sorted it.
     
    Pkirkby likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice