java - 如何正确添加声音?

标签 java

好吧,我正在开发我的程序,一旦单击按钮就会播放歌曲,我已经写下了代码,并且单击按钮时我的音频正在播放,但现在唯一的问题是我的程序完全疯了,只是在单击按钮时播放音频!!!!该程序只是卡住音频继续播放,但该程序不会执行其他功能!我究竟做错了什么?我该如何解决它?

这是操作监听器按钮的代码

在 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);
        }
    }

}

最佳答案

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

而不是:

sound.playAudio(pokemonBattle);

做:

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

还有:

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 );
    }
}

关于java - 如何正确添加声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11405611/

相关文章:

java - 为什么我的表情给出了错误的答案?

java - 如何创建依赖于 Spring Bean 的自定义 Spring PropertySource

java - 如何在我自己的API中实现Dicom属性字典

java - 尝试构建 32 位 JD2XX DLL 时找不到 -lftd2xx

java - 使用 Java 对 LPT 端口进行编程。真的吗?

java - Libgdx GestureListener 处理按住 TouchDown

java - hibernatetemplate 清除ehcache?

java - if (str1 == null) 当抛出 NullPointerException 时

java - 比较两个二叉树,看看它们是否具有相同的结构

java - start() 方法不存在