# Switch audio playing on face distance from cam

**URL:** https://discourse.processing.org/t/switch-audio-playing-on-face-distance-from-cam/35027
**Category:** Libraries
**Created:** [February 7, 2022, 6:03pm UTC](https://discourse.processing.org/t/switch-audio-playing-on-face-distance-from-cam/35027 "2022-02-07T18:03:58Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![LooSiuni](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@LooSiuni](https://discourse.processing.org/u/LooSiuni)
#### Post date: [February 7, 2022, 6:03pm UTC](https://discourse.processing.org/t/switch-audio-playing-on-face-distance-from-cam/35027/1 "2022-02-07T18:03:58Z")

</div>

So I have a code I am writing where I use OpenCV for face detection and I am switching between 2 scenes depending on the distance the face is from the camera. But I have to put one audio file to play for each of these, and playing on draw() just bugs the audio.

I am using switch() for playing the 2 diferent graphics. Do you guys have any suggestions on how to make the audio switch as well?

(The code is long, but I can share if needed)

---

<div class="post-metadata">

### Author: ![Tinkerer](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tinkerer](https://discourse.processing.org/u/Tinkerer)
#### Post date: [February 7, 2022, 6:47pm UTC](https://discourse.processing.org/t/switch-audio-playing-on-face-distance-from-cam/35027/2 "2022-02-07T18:47:55Z")

</div>

Hiya

Have a look at Example 4 here - just switch the keyboard trigger for your face detection.

> **[Sound](https://processing.org/tutorials/sound/#example-3-4-sample-playback)**
>
> Learn how to play, analyze, and synthesize sound with the Sound Library.

```auto
Processing Sound Library, Example 4

This example shows how to make a simple keyboard-triggered sampler with the Sound
library. In this sketch 5 different short samples are loaded and played back at
different speeds, which also changes their perceived pitch by one or two octaves.
```

---

<div class="post-metadata">

### Author: ![LooSiuni](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@LooSiuni](https://discourse.processing.org/u/LooSiuni)
#### Post date: [February 7, 2022, 7:47pm UTC](https://discourse.processing.org/t/switch-audio-playing-on-face-distance-from-cam/35027/3 "2022-02-07T19:47:15Z")

</div>

I tried that approach, but I think the audio keeps playing again and again, because the trigger is inside the draw.

I am kind of a begginer, so I tried making a new void for the face detection but it stop working ☹

---

<div class="post-metadata">

### Author: ![Tinkerer](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tinkerer](https://discourse.processing.org/u/Tinkerer)
#### Post date: [February 8, 2022, 6:47am UTC](https://discourse.processing.org/t/switch-audio-playing-on-face-distance-from-cam/35027/4 "2022-02-08T06:47:15Z")

</div>

Hiya

you could use isPlaying to check if a sound is already playing

> **[Reference](https://processing.org/reference/libraries/sound/SoundFile_isPlaying_.html)**
>
> Check whether this soundfile is currently playing.

and stop for when you switch

> **[Reference](https://processing.org/reference/libraries/sound/SoundFile_stop_.html)**
>
> Stops the playback.

then perhaps for an approach that might work - look at scene switching as used by games (also called game state)

eg in your draw loop have these steps

detect face

if face is near

- face is near code
- is face-is-near sound playing? if so do nothing, otherwise play face-is-near sound
- is face-is-far sound playing? if so stop it, otherwise do nothing

if face is far

- face is far code
- is face-is-far sound playing? if so do nothing, otherwise play face-is-far sound
- is face-is-near sound playing? if so stop it, otherwise do nothing

its fine having the face detection in a new void - you then call it at the right point in your draw loop

🙂

---

<div class="post-metadata">

### Author: ![LooSiuni](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@LooSiuni](https://discourse.processing.org/u/LooSiuni)
#### Post date: [February 8, 2022, 9:48am UTC](https://discourse.processing.org/t/switch-audio-playing-on-face-distance-from-cam/35027/5 "2022-02-08T09:48:33Z")

</div>

Thank you so much!!! I was not aware of isPlaying and it worked perfectly!
