Snippets Collections
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;

public class SoundPlayer {

    private static SoundPlayer instance;

    private Clip mClip;

    public static SoundPlayer get() {
        if (instance == null) instance = new SoundPlayer();
        return instance;
    }

    private SoundPlayer() {
        final File soundFile = new File("./res/my_sound.wav");

        try {
            mClip = AudioSystem.getClip();
            mClip.open(AudioSystem.getAudioInputStream(soundFile));

        } catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
            throw new RuntimeException(e);
        }
    }

    public void playMySound() {
        if (mClip != null) {
            mClip.setMicrosecondPosition(0);     // Reset to the start of the file
            mClip.start();
        }
    }
}
public void playMySound() {
    if (mClip != null) {
        mClip.setMicrosecondPosition(0);     // Reset to the start of the file
        mClip.start();
    }
}
final File soundFile = new File("./res/my_sound.wav");
  
try {
  mClip = AudioSystem.getClip();
  mClip.open(AudioSystem.getAudioInputStream(soundFile));

} catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
  throw new RuntimeException(e);
}
star

Thu Jun 30 2022 12:49:31 GMT+0000 (Coordinated Universal Time)

#java #sound
star

Thu Jun 30 2022 12:47:57 GMT+0000 (Coordinated Universal Time)

#java #sound
star

Thu Jun 30 2022 12:40:06 GMT+0000 (Coordinated Universal Time)

#java #sound

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension