# The sound begins to make noise after repeated use

**URL:** <https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205>\
**Category:** Processing for Android\
**Created:** [June 28, 2020, 3:51am UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205 "2020-06-28T03:51:01Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 3:51am UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/1 "2020-06-28T03:51:01Z")

</div>

\<import processing.sound.\*;  
SoundFile file;  
int i;

void setup() {  
size(640, 360);  
background(255);  
file = new SoundFile(this, “test.mp3”);  
}

void draw() {  
background(60);  
if(mousePressed){  
file.play(1, 0.5);  
mousePressed = false;  
}

```
if (file.isPlaying()) {
  i++;
  println("File play " + i + "i-seconds");
} else {
  i = 0;
}

if(i>10){
  file.stop();
}

```

}

> 

Above left an example code. If you start (by pressing) the audio, then everything works, but if you repeat it many times, the sound starts to make noise very stylish.

The example shows that I tried to stop it forcibly. This did not work.

For the test, any files are suitable for you, since for me this happens even with examples from the library.

Android. I am writing on APDE.

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 3:52am UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/2 "2020-06-28T03:52:05Z")

</div>

Sorry, I rarely talk in forums, I can’t enter the code correctly. do not scold 🙂

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 28, 2020, 5:23am UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/3 "2020-06-28T05:23:01Z")

</div>

Sounds like you are having an audio overlapping issue.

Depending on what you want to accomplish you have a couple options, both utilizing `isPlaying()`  
[https://processing.org/reference/libraries/sound/SoundFile\_isPlaying\_.html](https://processing.org/reference/libraries/sound/SoundFile_isPlaying_.html)

If you want to avoid the overlap when you press the mouse then you should check if the audio is not already playing first, and if it is not then play the audio.

```auto
if (mousePressed) {
    if(!file.isPlaying()) { 
    file.play();
    }
  }

```

If you are wanting to restart the audio when you press the mouse then you should check if the audio is playing, and if it is then `stop()` the audio followed by `play(), else `play()`.

```auto
if (mousePressed) {
    if (file.isPlaying()) {
      file.stop();
      file.play();
    } else {
      file.play();
    }
  }

```

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 3:38pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/4 "2020-06-28T15:38:01Z")

</div>

when I saw your answer, I was delighted, thinking that the problem would be solved. because by all laws of logic the answer is correct

But after adding your solution to the code, nothing changed.  
After many clicks and starting the sound, the sound began to make noise again. And with each press, the noise becomes stronger.

I can’t even guess what it is.

Moreover, there are lags when compiling the code, and if the code is made by the application.

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 28, 2020, 3:56pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/5 "2020-06-28T15:56:54Z")

</div>

> [@NoName](#):
>
> But after adding your solution to the code, nothing changed.

This would be a good time for you to share the code you are working with so we can further investigate and debug your issue.

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 5:10pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/6 "2020-06-28T17:10:09Z")

</div>

the code is higher.

The reason is not in the code, since the problem occurs even in the examples of the sound library (without changing the code).

Your help is really needed, how can I help you (so that you help me) besides the code? After all, I did not use sound in the project, but just opened an example from the library.

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 28, 2020, 5:24pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/7 "2020-06-28T17:24:22Z")

</div>

> [@NoName](#):
>
> The reason is not in the code, since the problem occurs even in the examples of the sound library (without changing the code).

Could be your soundcard or some other hardware related issue, maybe. ¯\_(ツ)\_/¯  
I don’t see a sketch in the examples that match the code you shared above …

> [@NoName](#):
>
> But after adding your solution to the code, nothing changed.

Can you share the updated code? Simply saying you added the code and there is no change doe not really help, maybe you put it outside of a function, have syntax errors, ect …

> [@NoName](#):
>
> If you start (by pressing) the audio, then everything works, but if you repeat it many times, the sound starts to make noise very stylish.

Maybe the issue I think you are having is not the issue at all. To me it sounds like you are describing the audio overlapping, meaning each time you press the mouse it plays a new instance of the audio, while the other instance(s) are still playing.

Is this the issue?

If so, then my previous answer is the solution, if not then can you please share your updated code with a more detailed description of what the issue is?

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 5:34pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/8 "2020-06-28T17:34:52Z")

</div>

If this is really that important, then here is the code I’m using  
At the same time, I added a counter to understand when the sound begins to break itself.  
At 95-100 presses, the sound becomes incorrect. And with each subsequent press, this effect increases.

import processing.sound.\*;  
SoundFile file;  
int i = 0;

void setup() {  
size(640, 360);  
background(255);  
file = new SoundFile(this, “test.mp3”);  
}

void draw() {  
background(60);  
fill(200);  
textSize(width/20);  
text(i, width/2, height/2);  
if (mousePressed) {  
i++;  
if (file.isPlaying()) {  
file.stop();  
file.play();  
} else {  
file.play();  
}  
mousePressed = false;  
}  
}

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 6:10pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/9 "2020-06-28T18:10:51Z")

</div>

TESTED AT THE SECOND TSSTROY

Problem still exists. It’s a pity.

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 28, 2020, 6:11pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/10 "2020-06-28T18:11:37Z")

</div>

Sorry, I was distracted, the issue is using `mousePressed` …

If you use the `mouseClicked()` you can set a boolean to true, then check if that boolean is true in your logic and stop/play the sound then set the boolean to false.

```auto
void draw() {
  background(60);
  if (playSnd) {
    if (file.isPlaying()) {
      file.stop();
      file.play();
      playSnd = false;
    } else {
      file.play();
    }
  }
}

void mouseClicked() {
  playSnd = true;
}

```

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 6:20pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/11 "2020-06-28T18:20:20Z")

</div>

Mouse click does not solve the problem.  
There is a suggestion that this is a regular problem APDE. Faced with it when APDE did not capture the pressing of the keyboard of Russian letters.

Can you test with yourself? Or is everything OK in your APDЕ?

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 28, 2020, 6:24pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/12 "2020-06-28T18:24:11Z")

</div>

Works as expected on my end.

I am not familiar with that issue since I do not type in Russian, but I am also confused how that has anything to do with the mouse click/press.

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 6:28pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/13 "2020-06-28T18:28:58Z")

</div>

Clicking does not solve the problem.  
Why do you suggest that the problem is in pressing? Is there any other similar problem?

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 6:32pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/14 "2020-06-28T18:32:41Z")

</div>

New news.

If the sound begins to distort, it does not matter what further sound we turn on, it will still sound crooked.

I specify:  
Many times clicked on the first sound. There were lags. And even if now we click on the second sound, it will also lag.

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 6:51pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/15 "2020-06-28T18:51:46Z")

</div>

OH MY GOD.

I just discovered (on a long audio file) that what we wrote above about WHY SOMETIMES does not stop the previous sample.

I hear one on one overlapping.

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 28, 2020, 6:53pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/16 "2020-06-28T18:53:09Z")

</div>

> [@NoName](#):
>
> Why do you suggest that the problem is in pressing?

Because `mousePressed` is true while it is pressed, causing the audio to stop and play each frame during the press. Although you press and release quickly, it still fires repeatedly for several frames.

`mouseClicked()` is fired only once.

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 6:58pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/17 "2020-06-28T18:58:40Z")

</div>

so I entered at the end of the code mousePressed = false, which eliminates the possibility of a second press.

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 7:07pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/18 "2020-06-28T19:07:36Z")

</div>

more news.

If you click on the Play Sound a hundred times, and when the lags start … You can write soundFile=null and then set the sound file again.

Spoiler: this does not solve the problem. but it doesn’t give any errors, which means that at the time of zeroing the sound, it does not play anywhere.

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 28, 2020, 7:20pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/19 "2020-06-28T19:20:00Z")

</div>

Sorry, I don’t know what else to try … it works on my end.

Starting to sound a bit hackey, like you said already, may have something to do with Russian keyboard input.

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [June 28, 2020, 7:24pm UTC](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205/20 "2020-06-28T19:24:03Z")

</div>

Anyway, thanks for trying to help.

I will hope that someone on this forum knows how to fix the error))))

[Next page](https://discourse.processing.org/t/the-sound-begins-to-make-noise-after-repeated-use/22205.md?page=2)
