Sto creando un gioco e quando fai game over il programma dovrà riprodurre un file audio, l' unica cosa è che lo riproduce non quando c' è la schermata game over, ma quando ritorna automaticamente dopo il game over alla schermata home. Ecco il codice, spero mi possiate aiutare.


codice:
package com.game;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Sound {

    public Clip clip;
    
    public Sound(String path){
        try {
            AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResource(path));
            AudioFormat baseFormat = ais.getFormat();
            AudioFormat decodeFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 
                    16, baseFormat.getChannels(), baseFormat.getChannels()*2, baseFormat.getSampleRate(), false);
            AudioInputStream dais = AudioSystem.getAudioInputStream(decodeFormat, ais);
                clip = AudioSystem.getClip();
                clip.open(dais);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void play(){
        if(clip == null) return;
        stop();
        clip.setFramePosition(0);
        clip.start();
    }
    
    public void close(){
        stop();
        clip.close();
    }
    
    public void stop(){
        if(clip.isRunning()) clip.stop();
    }
}
codice:
Sound gameOverSound = new Sound("/gameOverSound.wav");
if(gameOver) {
gameOverSound.play();
}