0

I tried to play some sounds using the javax.sound.sampled package, when I encountered a strange residual beeping sound right after the clip ends. I am playing back four different audio files and the other three are working fine. I can not find a reason, why it is making this sound. Just to clarify, I checked the audio file in a editor and the beeping noise is not showing up and therefore has to be caused by Java. The following code is used to play the sounds (I am having trouble with case 1):

import java.io.IOException;
import java.net.URL;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class SoundHandler implements SoundConstants{
    URL url;
    public SoundHandler() {
        url = this.getClass().getResource("/sounds3.wav");
    }
    public void playSound(int sound) {
        try {
            Clip clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(url));
            clip.setFramePosition(0);
            clip.start();
        } catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
    }
}
mousekip
  • 117
  • 10
  • 1
    _I am having trouble with case 1_ to make your code more of an [mre] remove other cases and make the relevant audio file available. – c0der Dec 22 '21 at 06:04
  • 1
    Is the URL for a publicly accessible resource? I'm curious to give it a try. Another interesting experiment might be to play back the files using `SourceDataLine` (example of how to do so is at the javasound tag's info area: click on the tag, then on "Learn more..."). Am curious if the sound gives the same behavior. Also good: confirm/check that each sound file has the same audio format. Dropping the use of MASTER_GAIN would also be a good troubleshooting test. Is it possible the site hosting the URL is adding this artifact? Or is the URL local/under your control? – Phil Freihofner Dec 22 '21 at 08:29
  • 1
    @PhilFreihofner No the URL is on my computer. I stored the URLs of the sounds to make my code more efficient, because these sounds are sometimes being played multiple times per second. I do not know how to share audio files via Stack Overflow, so unless you tell me how I am afraid you can not try it out. To your next point, removing MASTER_GAIN unfortunately does nothing, but I will give SourceDataLine a try. – mousekip Dec 22 '21 at 19:18
  • 1
    Often if a sound is truncated (stops abruptly while playing), the discontinuity will be heard as a loud click. But even if your mystery sound file has a chopped off ending, it wouldn't sound like residual beeps, but like a transient click. So that should not be the cause. I don't recall there being a way to link sound assets using stackoverflow. The only way I've exchanged audio files is by using a third party tool or by posting the asset on my website. Will await result of `SourceDataLine` playback before putting further thought into this. – Phil Freihofner Dec 23 '21 at 00:57
  • @PhilFreihofner The OP could try to replicate the problem using [sounds (tones) generated in memory](https://stackoverflow.com/a/7782749/418556). – Andrew Thompson Dec 23 '21 at 02:13
  • @PhilFreihofner I have just solved the problem, I am just still unsure how. All I did was copying the audio file from my project folder into a video/audio editing tool and then exporting it again. Maybe the first time, there was a compression error or something like that. Anyways thank you for your responses – mousekip Dec 23 '21 at 15:32
  • 1
    Glad to hear you solved it! I wonder why the editor you checked the file with didn't reveal this error. I would think a tool like Audacity probably would have exposed the issue. In any event, the next step, can you write up your "solution" as an answer and mark this as solved? – Phil Freihofner Dec 23 '21 at 18:30

0 Answers0