PlaySound

Posted by Dzbanek on Wed 16 Jul 2014 04:59 AM — 7 posts, 24,954 views.

#0
Hello

How can I play a number of sounds one after another? I would like to hear them one after one.
Something like:

function test()
PlayMySound("1.wav")
PlayMySound("2.wav")
PlayMySound("3.wav")
end

Of course when I just use PlaySound I will hear all of them at the same time.

I thought about using timers and checking if sound is playing in for example buffer 1 to play next one and setting the name of it in the variable but it won't work for more than 2 sounds. Forgive me my poor knowledge.
Australia Forum Administrator #1
You should be able to use GetSoundStatus to see if a sound has finished playing and then move onto playing another one. I don't see why that would not work for more than two sounds.
#2
function test()
PlayMySound("1.wav")
PlayMySound("2.wav")
PlayMySound("3.wav")
end

function PlayMySound(filename)
while GetSoundStatus(1) > 0 then
end
PlaySound (1, filename, false, 0, 0)
end

In this example test plays 3 files but in real example different function can play a sound. I'm not sure it that while loop won't hang my whole plugin?
Australia Forum Administrator #3
This works, I just tested it:


require "wait"

function PlayOneSound (name)
  while GetSoundStatus (1) > 0 do
    wait.time (0.1)
  end -- while
  PlaySound (1, name)
end -- PlayOneSound 

function soundTest ()

  wait.make ( function ()

    PlayOneSound ("1.wav")
    PlayOneSound ("2.wav")
    PlayOneSound ("3.wav")

  end )  -- end of wait.make

end -- soundTest

soundTest ()


Using the "wait" module like this stops the client from hanging while it plays the sounds, potentially for quite a few seconds.

See: http://gammon.com.au/modules

In particular:

http://www.gammon.com.au/forum/?id=4956
Amended on Wed 16 Jul 2014 06:07 AM by Nick Gammon
#4
Umm right. But I see I need to pass PlayOneSound to the wait. Not sure how I can do that in the following example:

function a()
PlayOneSound ("1.wav")
end

function b()
PlayOneSound ("2.wav")
end

function c()
PlayOneSound ("3.wav")
end

So, basically there are different functions playing different sounds. Any further help, please? Forgive me my lack of knowledge, trying to learn:)
Australia Forum Administrator #5
Just move the wait inside PlayOneSound like this:


require "wait"

function PlayOneSound (name)
  wait.make ( function ()
    while GetSoundStatus (1) > 0 do
      wait.time (0.1)
    end -- while
    PlaySound (1, name)
  end )  -- end of wait.make

end -- PlayOneSound 

PlayOneSound ("1.wav")
PlayOneSound ("2.wav")
PlayOneSound ("3.wav")


Now you can call PlayOneSound whenever you feel like it.
#6
Thank you very much! It works great:)