The sound begins to make noise after repeated use

<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.

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

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

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.

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()`.

if (mousePressed) {
    if (file.isPlaying()) {
      file.stop();
      file.play();
    } else {
      file.play();
    }
  }
1 Like

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.

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.

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.

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 …

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 …

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?

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;
}
}

TESTED AT THE SECOND TSSTROY

Problem still exists. It’s a pity.

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.

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

void mouseClicked() {
  playSnd = true;
}

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Е?

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.

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

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.

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.

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.

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

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.

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.

1 Like

Anyway, thanks for trying to help.

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

1 Like