Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.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,好的,我正在制作一个程序,一旦点击一个按钮,它就会播放一首歌。我把代码记下来,当点击按钮时,我的音频就会播放。但是现在唯一的问题是,我的程序完全疯了,当点击按钮时,我只会播放音频!!!!该程序只是冻结音频继续播放,但该程序不会做它的其他功能!我做错了什么?我该如何修复它 下面是操作侦听器按钮的代码 在myFrame.java中 sound sound = new sound(); private File pokemonBattle = new File(".\\audio\\"+"sound.

好的,我正在制作一个程序,一旦点击一个按钮,它就会播放一首歌。我把代码记下来,当点击按钮时,我的音频就会播放。但是现在唯一的问题是,我的程序完全疯了,当点击按钮时,我只会播放音频!!!!该程序只是冻结音频继续播放,但该程序不会做它的其他功能!我做错了什么?我该如何修复它

下面是操作侦听器按钮的代码

在myFrame.java中

sound sound = new sound();
private File pokemonBattle = new File(".\\audio\\"+"sound.wav");


private void dualButtonActionPerformed(java.awt.event.ActionEvent evt)       
    {                                           
    // TODO add your handling code here:
    for (int i = 0; i<1; i++){
    c = deck.getNextCard();
    p1hand[i] = c;
    p1.setCard(c);                   //ignore all this
    c = deck.getNextCard();
    p2hand[i] = c;
    p2.setCard(c);
     }

    pokemon1.setEnabled(true);
    pokemon2.setEnabled(true);
    pokemon1.setIcon(p1hand[0].getImage()); //ignore all this as well
    pokemon2.setIcon(p2hand[0].getImage());
    textArea.setText("Pokemon 1:"+p1hand[0].toString()+"\nPokemon 2:   
 "+p2hand[0].toString());
    p1ResultLabel.setText("");
    p2ResultLabel.setText("");

      //this is where im calling my audio
     sound.playAudio(pokemonBattle);//this is where im calling my play audio method

 }

您需要在单独的线程中播放音频。该程序变得无响应,因为它在上播放您的音频,而不是绘制UI并响应用户交互

而不是:

sound.playAudio(pokemonBattle);
做:

而且:

public class SoundPlayer implements Runnable
{
    private final sound sound;
    private final File soundFile;

    public SoundPlayer( sound sound, File soundFile )
    {
        this.sound = sound;
        this.soundFile = soundFile;
    }

    public void run()
    {
       sound.playAudio( soundFile );
    }
}

您需要在单独的线程中播放音频。该程序变得无响应,因为它在上播放您的音频,而不是绘制UI并响应用户交互

而不是:

sound.playAudio(pokemonBattle);
做:

而且:

public class SoundPlayer implements Runnable
{
    private final sound sound;
    private final File soundFile;

    public SoundPlayer( sound sound, File soundFile )
    {
        this.sound = sound;
        this.soundFile = soundFile;
    }

    public void run()
    {
       sound.playAudio( soundFile );
    }
}

约瑟夫是对的。但在这种情况下,问题是

 while((cnt=audioInputStream.read
                     (tempBuffer,0,tempBuffer.length))!=-1){
            if(cnt>0){
                sourceDataLine.write(tempBuffer,0,cnt);
            }

        }

您的while循环将无限地运行,从而冻结您的应用程序。由于您同时读取和写入整个缓冲区,因此不需要while循环。在循环之外这样做,你会没事的。

约瑟夫是对的。但在这种情况下,问题是

 while((cnt=audioInputStream.read
                     (tempBuffer,0,tempBuffer.length))!=-1){
            if(cnt>0){
                sourceDataLine.write(tempBuffer,0,cnt);
            }

        }

您的while循环将无限地运行,从而冻结您的应用程序。由于您同时读取和写入整个缓冲区,因此不需要while循环。在循环之外执行此操作,您就会没事。

很抱歉,我完全不知道如何在单独的线程中播放音频?很抱歉,我完全不知道如何在单独的线程中播放音频?