Java sound, only plays once

When the ball intersects the paddle, it plays the sound.But when the ball intersects the paddle again, the sound won’t play.

Here are some of my source codes.

Sound


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.*;
import sun.audio.AudioData;
import sun.audio.AudioDataStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;

public class Sound {

    public AudioPlayer MGP = AudioPlayer.player;
    public AudioStream BGM;
    public AudioData MD;
    public ContinuousAudioDataStream loop = null;
    public AudioDataStream play = null;
    public File file;
    public URL url;

    public Sound(String dir) {
        url = this.getClass().getResource(dir);
        try {
            file = new File(url.toURI());
        } catch (URISyntaxException e) {
            file = new File(url.getPath());
        }
        try {
            System.out.println("the url is " + file.getAbsolutePath());
            BGM = new AudioStream(new FileInputStream(file));
            MD = BGM.getData();
            loop = new ContinuousAudioDataStream(MD);
            play = new AudioDataStream(MD);
        } catch (IOException e) {
        }
    }

    public void play() {
        MGP.start(play);
    }

    public void playLoop() {
        play = loop;
        MGP.start(play);
    }

    public void stop() {
        MGP.stop(play);
    }
}

Ball


import java.awt.Rectangle;
import java.util.Random;
import java.awt.Image;
import javax.swing.ImageIcon;

public class Ball {

    public int x;
    public int y;
    public int dx = 0;
    public int dy = 0;
    public int diameter;
    public Image ballImg;
    public Player player = new Player();
    boolean bouncedX = false;
    boolean bouncedY = false;
    //////////Sounds//////////////
    public Sound hit = new Sound("/ultrapong/sounds/paddle/hit.wav");

    public Ball() {
        x = 250;
        y = 250;
        dx = 3;
        dy = 3;
        diameter = 5;
        ballImg = new ImageIcon(this.getClass().getResource("/ultrapong/Ball.png")).getImage();
        ranBouncerX = new Random();
        ranBouncerY = new Random();
    }
    public Random random = new Random();

    public void update() {
        if (y < 0 || y + diameter > 460) {
            for (int i = 250; i < 300 && i > 200; i++) {
                y = random.nextInt(i);
            }
            for (int i = 250; i < 300 && i > 200; i++) {
                x = random.nextInt(i);
            }
            dx = 2;
            dy = 2;
        } else if (x + diameter + (diameter / 2) > 500 || x < 0) {
            dx = bounceX(dx);
        }
        x = x + dx;
        y = y + dy;

    }

    public void updateBounceX(Rectangle p, Rectangle b) {
        if (p.intersects(b)) {
            dy = bounceY(dy);
            hit.play();
        }
    }

    public void updateBounceTileX(Rectangle p, Rectangle b) {
        int leftBorder = p.x;
        int rightBorder = p.x + p.width;
        int middleBorderX = p.x + (p.width / 2);
        if (p.intersects(b) && b.x <= rightBorder && b.x + b.width > middleBorderX) {
            if (!bouncedX) {
                dx = bounceX(dx);
                bouncedX = true;
            }
            if (bouncedX) {
                dx = dx * 1;
            }
        } else if (p.intersects(b) && b.x + b.width >= leftBorder && b.x + b.width < middleBorderX) {
            if (!bouncedX) {
                dx = bounceX(dx);
                bouncedX = true;
            }
            if (bouncedX) {
                dx = dx * 1;
            }
        } else if (!p.intersects(b)) {
            bouncedX = false;
        }
    }

    public void updateBounceTileY(Rectangle p, Rectangle b) {
        int topBorder = p.y;
        int bottomBorder = p.y + p.height;
        int middleBorderY = p.y + (p.height / 2);
        if (p.intersects(b) && b.y <= bottomBorder && b.y + b.height > middleBorderY) {
            if (!bouncedY) {
                dy = bounceY(dy);
                bouncedX = true;
            }
            if (bouncedY) {
                dy = dy * 1;
            }
        } else if (p.intersects(b) && b.y + b.height >= topBorder && b.y + b.height < middleBorderY) {
            if (!bouncedY) {
                dy = bounceY(dy);
                bouncedY = true;
            }
            if (bouncedY) {
                dy = dy * 1;
            }
        } else if (!p.intersects(b)) {
            bouncedY = false;
        }
    }

    public Rectangle ball() {
        return new Rectangle(getX(), getY(), diameter(), diameter());
    }

    public Image getImage() {
        return ballImg;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int diameter() {
        return diameter;
    }

    public void velocityX(int vx) {
        this.dx = vx;
    }

    public void velocityY(int vy) {
        this.dy = vy;
    }
    public Random ranBouncerX;

    public int bounceX(int d) {
        int dd = (d < 0) ? -1 : 1;
        int n = ranBouncerX.nextInt();
        if (n <= 0) {
            n = 1 - n;
        }
        return ((n % 6) + 2) * -dd;

    }
    public Random ranBouncerY;

    public int bounceY(int d) {
        int dd = (d < 0) ? -1 : 1;
        int n = ranBouncerY.nextInt();
        if (n <= 0) {
            n = 1 - n;
        }
        return ((n % 6) + 2) * -dd;
    }

    public int getDx() {
        return dx;
    }

    public int getDy() {
        return dy;
    }
}

Here are all of my source codes.

http://www.mediafire.com/?4eubm7v6u8cyakq

First of all, it is not recommended to use classes in the Sun package.
What’s wrong with using JavaSound in javax.sound.sampled?

You might try calling stop before you call start. I seem to recall that being the solution to similar behavior some time ago (although I’ve never actually used these particular classes before)

I’ve got the answer to my problem.

Since, Im going to make a downloadable and an Applet version of my game, I used AudioClip instead. So, If I want to make this downloadable version an Applet, I can just change the MainConfig constructor into an init, and remove the class, UltraPong.

Also, the sound will play, everytime the ball intersects the paddle.

Sound


import java.applet.Applet;
import java.applet.AudioClip;
import java.net.*;

    public class Sound { // Holds one audio file

        private AudioClip sound; // Sound player
        private URL url; // Sound path

        Sound(String filename) {
            try {
                url = this.getClass().getResource(filename); // Get the Sound URL
                sound = Applet.newAudioClip(url); // Load the Sound                
            } catch (Exception e) {
            } // Satisfy the catch
        }

        public void playLoop() {
            sound.loop(); // Play 
        }

        public void stop() {
            sound.stop(); // Play 
        }

        public void play() {
            sound.play(); // Play only once
        }
    }

MainConfig


import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.*;
import ultrapong.levels.*;
import ultrapong.levels.Config.*;
import ultrapong.startscreen.Menu;

public class MainConfig extends Applet implements Runnable {

    public int setWidth, setHeight;
    public Player player;
    public Thread mainLoop;
    public Level currentLevel;
    public int counter;
    public AI computer = new AI();
    public Ball ball = new Ball();
    public BoxLevel bx = new BoxLevel();
    public Menu m = new Menu();
    ///////////DoubleBufferingStuff/////////
    public Graphics dbg;
    public Image dbImage;

    public MainConfig() {
        player = new Player();
        addKeyListener(new KL());
        addMouseMotionListener(new MML());
        addMouseListener(new ML());
        setFocusable(true);
        setWidth = 506;
        setHeight = 498;
        currentLevel = m;
    }

    public void addNotify() {
        super.addNotify();
        mainLoop = new Thread(this);
        mainLoop.start();
    }

    @Override
    public void update(Graphics g) {
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paint(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        if (currentLevel == m) {
            m.drawLevelElements(g2d);
            if (m.startPressed) {
                currentLevel = bx;
            }
        } else if (currentLevel == bx) {
            bx.drawBackground(g2d);
            bx.drawLevel(g2d);
            bx.drawLevelObjects(g2d);
        }
    }

    public class MML extends MouseMotionAdapter {

        @Override
        public void mouseMoved(MouseEvent e) {
            m.mouseMoved(e);
        }
    }

    public class ML extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            m.mousePressed(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            m.mouseReleased(e);
        }
    }

    public class KL extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            if (currentLevel == bx) {
                bx.player.keyPressed(e);
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (currentLevel == bx) {
                bx.player.keyReleased(e);
            }
        }
    }

    public void updateLevels() {
        if (currentLevel == bx) {
            bx.update(ball, player, computer, currentLevel);
        }
    }

    public void run() {

        float startingTime = System.currentTimeMillis();
        float cumTime = System.currentTimeMillis() - startingTime;
        while (true) {
            float timePassed = System.currentTimeMillis() - cumTime;
            cumTime += timePassed;
            updateLevels();
            repaint();
            try {
                Thread.sleep(20);
            } catch (Exception e) {
                System.err.toString();
            }
        }
    }
}