Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 乒乓球比赛。当球击到任何一个桨时,如何播放音频?_Java - Fatal编程技术网

Java 乒乓球比赛。当球击到任何一个桨时,如何播放音频?

Java 乒乓球比赛。当球击到任何一个桨时,如何播放音频?,java,Java,主课 package ponggame; import java.applet.Applet; import java.applet.AudioClip; public class Sound { public static final Sound bgMusic = new Sound("/pong.wav"); public static final Sound hitPaddle = new Sound("/bounce.wav"); private Au

主课

package ponggame;

import java.applet.Applet;

import java.applet.AudioClip;

public class Sound {

    public static final Sound bgMusic = new Sound("/pong.wav");
    public static final Sound hitPaddle = new Sound("/bounce.wav");

    private AudioClip clip;

    public Sound(String fileName) {
        try {
            // gets the sound file that is passed in the constructor
            clip = Applet.newAudioClip(Sound.class.getResource(fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // plays the music, obviously
    public void play() {
        try {
            new Thread() { //multi-tasking stuff
                public void run(){
                    //clip.play();
                    clip.loop();

                }
            }.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //plays the audio in a loop
    public void loop(){
        play();
    }

}
请尝试以下链接:

我无法测试它,但它看起来不错


您必须插入
AudioPlayer.player.start(音频流)在检查冲突方法中

请减少问题中的代码量-尝试使用一个。可以同时播放多个剪辑。当球反弹时,您可以直接剪辑。播放反弹剪辑。音响系统应将它们混合在一起。
package ponggame;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Rectangle;

import java.awt.event.KeyEvent;

public class Paddle implements Runnable {

    int x, y, yDirection, id;

    Rectangle paddle;

    public Paddle(int x, int y, int id){
        this.x = x;
        this.y = y;
        this.id = id;
        paddle = new Rectangle(x, y, 10, 50);
    }

    public void keyPressed(KeyEvent e){
        switch(id){
            default:
                System.out.println("Please enter a valid ID in Paddle constructor");
                break;
            case 1:
                if(e.getKeyCode() == e.VK_W){
                    setYDirection(-1);
                }
                if(e.getKeyCode() == e.VK_S){
                    setYDirection(1);
                }
                break;
            case 2:
                if(e.getKeyCode() == e.VK_UP){
                    setYDirection(-1);
                }
                if(e.getKeyCode() == e.VK_DOWN){
                    setYDirection(+1);
                }
                break;            
        }
    }
    public void keyReleased(KeyEvent e){
        switch(id){
            default:
                System.out.println("Please enter a valid ID in Paddle constructor");
                break;
            case 1:
                if(e.getKeyCode() == e.VK_W){
                    setYDirection(0);
                }
                if(e.getKeyCode() == e.VK_S){
                    setYDirection(0);
                }
                break;
            case 2:
                if(e.getKeyCode() == e.VK_UP){
                    setYDirection(0);
                }
                if(e.getKeyCode() == e.VK_DOWN){
                    setYDirection(0);
                }
                break;            
        }
    }

    public void setYDirection(int ydir){
        yDirection = ydir;
    }

    public void move(){
        paddle.y += yDirection;
        if(paddle.y <= 15)
            paddle.y = 15;
        if(paddle.y >= 250)
            paddle.y = 250;
    }

    public void draw(Graphics g){
        switch(id){
            default:
                System.out.println("Please enter a valid ID in Paddle constructor");
                break;
            case 1:
                g.setColor(Color.CYAN);
                g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
                break;
            case 2:
                g.setColor(Color.PINK);
                g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
                break;
        }
    }

    @Override
    public void run() {
        try{
            while(true){
                move();
                Thread.sleep(8);
            } `enter code here`enter code here`
        }catch(Exception e){System.err.println(e.getMessage());}
    }

}
package ponggame;

import java.applet.Applet;

import java.applet.AudioClip;

public class Sound {

    public static final Sound bgMusic = new Sound("/pong.wav");
    public static final Sound hitPaddle = new Sound("/bounce.wav");

    private AudioClip clip;

    public Sound(String fileName) {
        try {
            // gets the sound file that is passed in the constructor
            clip = Applet.newAudioClip(Sound.class.getResource(fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // plays the music, obviously
    public void play() {
        try {
            new Thread() { //multi-tasking stuff
                public void run(){
                    //clip.play();
                    clip.loop();

                }
            }.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //plays the audio in a loop
    public void loop(){
        play();
    }

}
package ponggame;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import javax.swing.JFrame;



public class Main extends JFrame {

    // Double buffering
    Image dbImage;
    Graphics dbg;

    // sound relative
    private Sound sound;

    // Ball objects
    static Ball b = new Ball(193, 143);

    // Variables for screen size
    int GWIDTH = 400, GHEIGHT = 300;
    // Dimension of GWIDTH*GHEIGHT
    Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);

    // Create constructor to spawn window
    public Main() {
        this.setTitle("Pong Game - RC");
        this.setSize(screenSize);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBackground(Color.DARK_GRAY);
        this.addKeyListener(new AL());

        sound.bgMusic.play();

    }

    public static void main(String[] args) {
        Main m = new Main();
        // Create and start threads
        Thread ball = new Thread(b);
        ball.start();
        Thread p1 = new Thread(b.p1);
        Thread p2 = new Thread(b.p2);
        p1.start();
        p2.start();

    }

    // handles all the music stuff
    public static void music() {

    }

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

    public void draw(Graphics g) {
        b.draw(g);
        b.p1.draw(g);
        b.p2.d(g);

        g.setColor(Color.WHITE);
        g.drawString("" + b.p1Score, 15, 50);
        g.drawString("" + b.p2Score, 370, 50);

        repaint();
    }

    // //////EVENT LISTENER CLASS/////////
    public class AL extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            b.p1.keyPressed(e);
            b.p2.keyPressed(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            b.p1.keyReleased(e);
            b.p2.keyReleased(e);
        }
    }

}
// /////END EVENT LISTENER CLASS/////