Game Development Community

Alx sounds on Update Callback.

by Alex Ubilava · in iTorque 2D · 05/01/2012 (5:51 am) · 7 replies

Hi friends,

I've got an issue with sound management. I try to play steps sound when my player is moving and stop it when the player stands still. I've used some info in the tdn's docs, and this is what I get for the moment:


In my player's OnUpdate Callback I put:


if (player.getLinearVelocityY()==0 && player.getLinearVelocityX()==0)
{
alxStop($stepsSound);
}


And it works fine, the sound is set off (for the test I start playing it in other place of code). But I also need to play that StepsAudio when the player is moving. Taking into account that the simple incorporation of alxPlay() in the OnUpdate Callback will bring me a ton of sound instances (and, as a result, I'll have nothing but horrible noise), I believe that I should use some sort of "if" statement that will know if the sound is finished to play it again or not. Something like:

if (player.getLinearVelocityY()!=0 && player.getLinearVelocityX()!=0)
{
if (alxFinishedPlaying(StepsAudio)) // I don't know what exactly must I put here...
alxPlay(StepsAudio);
}


Please, anyone can help?

#1
05/02/2012 (3:26 am)
Something like this should suffice...

$handle = alxPlay($stepsSound);
if (alxIsPlaying($handle)) {
  echo("Source is playing");
}

#2
05/02/2012 (7:54 am)
Thanx, I'll try this one:

if (player.getLinearVelocityY()!=0 && player.getLinearVelocityX()!=0)
{
$handle = alxPlay($stepsSound);
if (alxIsPlaying($handle))
echo("Source is playing");
else
alxPlay(StepsAudio);
}



And is it absolutely necessary to use handles with Alx? Is it possible to put, for example alxIsPlaying(alxPlay($stepsSound)) ?
#3
05/03/2012 (12:31 am)
yes you must use is playing cause alxPlay will always return a new handle.
Also you must not assign the handle there but only in the ELSE block as you want to get the handle of the last recently playing step sound, you don't want to kick off a new one each round otherwise the if check would be useless ;)
#4
05/03/2012 (1:37 am)
@Marc Thanks a lot, so the alxPlay value changes dynamically...

So I should use this, right?:


if (player.getLinearVelocityY()!=0 && player.getLinearVelocityX()!=0)
{
// Is the alxPlay is still playing last steps sound iteration?
if (alxIsPlaying($handle))
echo("Source is playing");

// If not, play a new steps sound iteration and make a new handle for it
else
{
alxPlay(StepsAudio);
$handle = alxPlay(StepsAudio);
}

}



P.S. For making this piece of code work the StepsAudio should be non-looping, shouldn't it?
#5
05/03/2012 (2:30 am)
Nearly but you need to remove the first alxPlay

if (player.getLinearVelocityY()!=0 && player.getLinearVelocityX()!=0)
{ 
// Is the alxPlay is still playing last steps sound iteration? 
if (alxIsPlaying($handle))
echo("Source is playing");

// If not, play a new steps sound iteration and make a new handle for it
else
{
$handle = alxPlay(StepsAudio);
}

}


To explain what we do there and why: alxPlay gives you a targetobject that is the currently running audio track. This target object is required to find out if this audio track is still running but also to pause and stop it. If you don't know what audio track you want to check, you can not interact with it.

Your original code didn't care about the track already running and simply started a new one each time which on its own already kills the iphone pretty surely ;)


And if the track were looping then isPlaying simply always would be true but you can not make the track looping anyway, there is no mp3 / aac track attribute that forces the playback layer to loop it. Looping is an attribute you set when starting a track when you setup the audio playback layer. (alxPlayback completely ignores any datablock, just in case that lead to the question on looping)
#6
05/03/2012 (4:43 am)
Thank you very much, Marc and sorry for any inconvenience =)). I imagine how it is difficult to explain every step to the noob in simple words=))

I'll try it as soon as I'm back home. So the $handle = alxPlay(StepsAudio) by itself will start StepsAudio...
#7
05/03/2012 (11:36 am)
@Marc, many thanks once more! I've just tried it and it works perfectly!


P.S. This ALX is really a weird thing, indeed. I thought it was just an ordinary function but it seems that I need to dig deeper to understand its logic...