java动画声音无法正常播放

发布于 2025-01-06 11:51:01 字数 2578 浏览 4 评论 0原文

一个球无限循环地上下移动。每个动作都应该播放声音。但每5-6个动作就会播放一次。为什么会发生这种情况?声音持续时间小于1秒。在代码中,循环的每次迭代都会休眠 3 秒。然而,将睡眠时间改为6秒,就达到了所需的效果。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import sun.audio.*;
import java.io.*;

public class dabble {

private boolean z=false;
private int x=10;
private int y=10;
private JFrame frame;
private JLabel label;
private mypanel panel;
private JButton b1;
private JButton b2;

public static void main (String[] args) throws Exception
{
    dabble dab = new dabble();
    dab.start();
}

void start()
{
    frame = new JFrame();
    label = new JLabel();
    panel = new mypanel();
    b1= new JButton("Start");
    b2= new JButton("Stop");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    b1.addActionListener(new al1());
    b2.addActionListener(new al2());

    frame.getContentPane().add(BorderLayout.NORTH,b1);
    frame.getContentPane().add(BorderLayout.SOUTH,b2);
    frame.getContentPane().add(BorderLayout.CENTER,panel);
    frame.getContentPane().add(BorderLayout.EAST,label);
    frame.setSize(600,600);
    frame.setVisible(true);
}

class al1 implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if (z==false)
        {
            class myrun implements Runnable{
                public void run(){
                    z=true;
                    while(z==true)
                    {
                        y=510-y;
                        panel.repaint();
                        try
                        {
                            InputStream in=new FileInputStream("hit.wav");
                            AudioStream as=new AudioStream(in);
                            AudioPlayer.player.start(as);
                            Thread.sleep(3000);
                        }
                        catch(Exception Ex){}
                    }
                }
            }

            Runnable myjob = new myrun();
            Thread mythread = new Thread(myjob);
            mythread.start();

        }
    }
}

class al2 implements ActionListener{
    public void actionPerformed(ActionEvent event){
        z=false;
    }
}

class mypanel extends JPanel {
    public void paintComponent ( Graphics g){
        g.setColor(Color.white);
        g.fillRect(0,0,this.getWidth(),this.getHeight());
        int red = (int) (Math.random()*255);
        int green = (int) (Math.random()*255);
        int blue = (int) (Math.random()*255);
        Color c1 = new Color(red,green,blue);
        g.setColor(c1);
        g.fillOval(x,y,20,20);
    }
}
}

A ball is moving up and down in an infinite loop. Sound is supposed to play for each movement. But it is being played for every 5-6 movements. Why is that happening? Sound duration is less than 1 second. In the code there is a sleep for 3 seconds in each iteration of the loop. However, changing the sleep duration to 6 seconds, achieves the required effect.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import sun.audio.*;
import java.io.*;

public class dabble {

private boolean z=false;
private int x=10;
private int y=10;
private JFrame frame;
private JLabel label;
private mypanel panel;
private JButton b1;
private JButton b2;

public static void main (String[] args) throws Exception
{
    dabble dab = new dabble();
    dab.start();
}

void start()
{
    frame = new JFrame();
    label = new JLabel();
    panel = new mypanel();
    b1= new JButton("Start");
    b2= new JButton("Stop");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    b1.addActionListener(new al1());
    b2.addActionListener(new al2());

    frame.getContentPane().add(BorderLayout.NORTH,b1);
    frame.getContentPane().add(BorderLayout.SOUTH,b2);
    frame.getContentPane().add(BorderLayout.CENTER,panel);
    frame.getContentPane().add(BorderLayout.EAST,label);
    frame.setSize(600,600);
    frame.setVisible(true);
}

class al1 implements ActionListener{
    public void actionPerformed(ActionEvent event){
        if (z==false)
        {
            class myrun implements Runnable{
                public void run(){
                    z=true;
                    while(z==true)
                    {
                        y=510-y;
                        panel.repaint();
                        try
                        {
                            InputStream in=new FileInputStream("hit.wav");
                            AudioStream as=new AudioStream(in);
                            AudioPlayer.player.start(as);
                            Thread.sleep(3000);
                        }
                        catch(Exception Ex){}
                    }
                }
            }

            Runnable myjob = new myrun();
            Thread mythread = new Thread(myjob);
            mythread.start();

        }
    }
}

class al2 implements ActionListener{
    public void actionPerformed(ActionEvent event){
        z=false;
    }
}

class mypanel extends JPanel {
    public void paintComponent ( Graphics g){
        g.setColor(Color.white);
        g.fillRect(0,0,this.getWidth(),this.getHeight());
        int red = (int) (Math.random()*255);
        int green = (int) (Math.random()*255);
        int blue = (int) (Math.random()*255);
        Color c1 = new Color(red,green,blue);
        g.setColor(c1);
        g.fillOval(x,y,20,20);
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

開玄 2025-01-13 11:51:01
  1. Clip< 中加载声音/code>JavaSound 信息 页面所示。
  2. Clip 提供了 < code>loop(int)方法来指定播放的次数。
  1. Load the sound in a Clip as shown on the JavaSound info. page.
  2. Clip offers a loop(int) method to specify the number of time to play.
风启觞 2025-01-13 11:51:01

我要改变的一件事是在 while 循环中加载音频文件。这可能是一个冗长的操作,会打乱时间。

AudioStream as 添加为 al1 类的私有成员变量,并在构造函数中对其进行初始化。然后,每次循环迭代时,您就已经准备好流并且可以播放它了。

One thing I would change is the loading of the audio file in the while loop. This could be a lengthy operation that is messing up the timing.

Add the AudioStream as as a private member variable of the al1 class and initialize it in the constructor. Then, each time the loop iterates, you already have the stream ready to go and you can just play it.

迷爱 2025-01-13 11:51:01

当我将 sleep 命令移到 try 块之外,但仍在 while 循环内时,循环所需的时间仍然存在一些不均匀性,但至少球和声音保持同步。

不过确实很奇怪。我不明白为什么睡眠安排很重要。就好像在 try 块中的睡眠直到新的重绘发生之后才执行,从而阻止下一个播放?但怎么可能呢?

When I move the sleep command to outside the try block, but still within the while loop, there is still some unevenness in how long the loop takes, but at least the ball and sound stay in sync.

Really weird, though. I can't figure out why the sleep placement matters. It is as if the sleep, when in the try block, doesn't get executed until after the new repaint has occurred, and thus holds up the next play? But how could that be?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文