1

好的,所以我正在开发我的程序,一旦单击按钮就会播放一首歌曲,我有代码,当单击按钮时我的音频正在播放,但现在唯一的问题是我的程序完全疯了,只是单击按钮时播放音频!!!!该程序只是冻结音频继续播放,但程序不会执行它的其他功能!我究竟做错了什么?我该如何解决?

这是动作侦听器按钮的代码

在 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

 }

sound.java 我有 playAudio()

import java.io.File;
import javax.sound.sampled.*;



public class sound {

public void playAudio(File sf){
    AudioFormat audioFormat;
    AudioInputStream audioInputStream;
    SourceDataLine sourceDataLine;

    try
    {
        audioInputStream = AudioSystem.getAudioInputStream(sf);
        audioFormat = audioInputStream.getFormat();
        System.out.println(audioFormat);

        DataLine.Info dataLineInfo = new  
  DataLine.Info(SourceDataLine.class,audioFormat);

        sourceDataLine =(SourceDataLine)AudioSystem.getLine(dataLineInfo);

        byte tempBuffer[]=new byte[100000];
        int cnt;
        sourceDataLine.open(audioFormat);
        sourceDataLine.start();
        while((cnt=audioInputStream.read
                     (tempBuffer,0,tempBuffer.length))!=-1){
            if(cnt>0){
                sourceDataLine.write(tempBuffer,0,cnt);
            }

        }

    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.exit(0);
        }
    }

}
4

2 回答 2

1

You need to play the audio in a separate thread. The program becomes unresponsive because it is playing your audio on the event dispatch thread instead of drawing the UI and responding to user interaction.

instead of:

sound.playAudio(pokemonBattle);

do:

Thread t = new Thread( new SoundPlayer( sound, pokemonBattle );
t.start();

and also:

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 );
    }
}
于 2012-07-10T02:03:19.660 回答
0

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

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

        }

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

于 2012-07-10T02:09:34.113 回答