Playing sound while "Moving" in Touch

I have a rectangle on the screen and I would like audio to only play when finger moves into the rectangle and stops playing when finger is outside the circle.

This means that if finger touches the outside of the rectangle and is “moved” into the rectangle then audio starts up, and vice versa.

Audio stops also when finger is lifted.

Please help. Thanks.

Hi @scottshaw3d,

What kind of audio will be playing? A very short but repeating sound effect? Or a longer “music” track?

You’ll probably want to use a touch listener on the rectangle and sense the “moved” phase, however, you don’t want to play the sound effect on every moved phase, as that would almost immediately fill and overload the audio channels and possibly crash the app (and it wouldn’t sound good either!). Most likely, you should sense the first “moved” phase (or the “began” phase), then set focus on the object (http://docs.coronalabs.com/api/type/StageObject/setFocus.html). At the same time, start the audio playing. Then when focus is lost, stop the audio.

Hope this helps,

Brent

Hi @scottshaw3d,

What kind of audio will be playing? A very short but repeating sound effect? Or a longer “music” track?

You’ll probably want to use a touch listener on the rectangle and sense the “moved” phase, however, you don’t want to play the sound effect on every moved phase, as that would almost immediately fill and overload the audio channels and possibly crash the app (and it wouldn’t sound good either!). Most likely, you should sense the first “moved” phase (or the “began” phase), then set focus on the object (http://docs.coronalabs.com/api/type/StageObject/setFocus.html). At the same time, start the audio playing. Then when focus is lost, stop the audio.

Hope this helps,

Brent