Why can’t I play a sound effect in this java game

I’m currently trying to make a game in java and decided to try and add sound effects so I’m starting with adding a sound effect when the player jumps. there seems to be no problems but no sound is played when I jump. I even had the game print out “heee” every time spacebar is pressed so I know it should work. This code is from a much bigger project so I’m sorry if anything is missing but generally everything works besides the sound. (right now I’m using a death sfx as a placeholder sfx.)

I have 2 files:
File for playing sounds

import java.net.URL;

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

public class MakeSounds {
    Clip clip;
    URL soundurl[]=new URL[30];
    public MakeSounds() {
        soundurl[0]= getClass().getResource("/sounds/Deathsound.wav");
//This sound effect is put in a seperate package named "sounds"
    }
    public void setFile(int i) {
        try{
            AudioInputStream ais= AudioSystem.getAudioInputStream(soundurl[i]);
            clip = AudioSystem.getClip();
            clip.open(ais);
        }catch(Exception e) {
            
        }
    }
    public void play() {
        clip.start();
    }
    public void loop() {
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    public void stop() {
        clip.stop();
    }
}

main controller file

interface IStrategy {
    
    public void controller(Bird bird, KeyEvent kevent);
    public void controllerReleased(Bird bird, KeyEvent kevent);
}

// Controller class is used to control the movement of the bird
class Controller implements IStrategy {
    
    public void controller(Bird bird, KeyEvent kevent) {}
    public void controllerReleased(Bird bird, KeyEvent kevent) {
        MakeSounds sound=new MakeSounds();
        sound.setFile(0);
        if(kevent.getKeyCode() == KeyEvent.VK_SPACE) { // The player moves
        if(bird.dy>0) {bird.jump();sound.setFile(0);sound.play();System.out.print("heee");}
        else {bird.Down();}
        }
    }
    }

Output: heee

  • 2

    Never catch excpetion without hanlde it. Atleast log them

    – 

  • 2

    Strip off the gaming part and learn how to play audio in Java. Then go back to your game.

    – 

  • What @Queeg says: divide the problem into small bits and solve each small bit, one at a time — divide and conquer.

    – 

  • 1

    I agree with Jens: NEVER write an empty catch block. How are you supposed to know what’s going wrong if you ignore exceptions? The stack trace is a critical piece of information for debugging. Code must always make the stack trace available, either by calling printStackTrace() on the exception, properly logging the exception, or throwing a new exception whose cause is the caught exception.

    – 

  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example.

    – 
    Bot

Leave a Comment