2015-09-07 64 views
0

我想让我的高级项目在Java中的冠军选择像英雄联盟。我认为迄今为止我做得很好,但我陷入了一些困境。我想尝试打开一个JOptionPane播放声音文件。我整天都在做一些研究,但没有看到很多人在尝试。我通过JOptionPane看到了一些有关嘟嘟声的事情,但这就是它。有任何想法吗?JOptionPane中的音频文件

private class ButtonListener implements ActionListener 
    { 
     @Override 

     public void actionPerformed(ActionEvent e) 
     { 
     String actionCommand = e.getActionCommand(); 

     if (actionCommand.equals("Aatrox")) 
     { 
       playPopupMessageSound(); 
       final ImageIcon icon = new ImageIcon("C:\\Users\\Owner\\Documents\\NetBeansProjects\\EventObjectWindow\\src\\eventobjectwindow\\Resources\\Aatrox.png"); 
       JOptionPane.showMessageDialog(null, "You have selected Aatrox."+System.lineSeparator()+ "Aatrox is a legendary warrior, one of only five" + 
         "that remain of an ancient race known as the Darkin."+System.lineSeparator()+"He wields his massive blade with grace and poise, "+"" 
         + "slicing through legions in a style that is hypnotic to behold."+System.lineSeparator()+"With each foe felled, Aatrox's seemingly" + 
         "living blade drinks in their blood, empowering him and fueling his brutal, elegant campaign of slaughter." +System.lineSeparator()+ 
         "Base stats:" +System.lineSeparator()+ "Health: 537.8 (+85 per level)        Health Regen: 6.59 (+0.5 per level)" 
         +System.lineSeparator()+ "Attack Damage: 60.376 (+3.2 per level)   Armor:24.384 (+3.8 per level)" +System.lineSeparator()+ 
         "Attack Speed: 0.651 (+3% per level)    Magic Resist: 32.1 (+1.25 per level)" +System.lineSeparator()+ 
         "Movement Speed: 345", "Aatrox", JOptionPane.PLAIN_MESSAGE, icon);    
     } 
     else if (actionCommand.equals("Ahri")) 
     { 
      JOptionPane.showMessageDialog(null, "You have selected Ahri"); 
     } 
     else if (actionCommand.equals("Akali")) 
     { 
      JOptionPane.showMessageDialog(null, "You have selected Akali"); 
     } 
     else if (actionCommand.equals("Alistar")) 
     { 
      JOptionPane.showMessageDialog(null, "You have selected Alistar"); 
     } 
     else if (actionCommand.equals("Amumu")) 
     { 
      JOptionPane.showMessageDialog(null, "You have selected Amumu"); 
     } 
     else if (actionCommand.equals("Anivia")) 
     { 
      JOptionPane.showMessageDialog(null, "You have selected Anivia"); 
     } 
     else if (actionCommand.equals("Annie")) 
     { 
      JOptionPane.showMessageDialog(null, "You have selected Annie"); 
     }} 
    } 




public static void main(String[] args) 
    { 
     EventObjectWindow em = new EventObjectWindow(); 
    } 
    protected static void playPopupMessageSound() { 

     try (InputStream is = EventObjectWindow.class.getResourceAsStream("C:\\Users\\Owner\\Documents\\NetBeansProjects\\SrProjectLeague\\src\\srprojectleague\\Sounds\\Aatrox.wav")) { 
      try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is)) { 
       Clip clip = AudioSystem.getClip(); 
       clip.open(audioInputStream); 
       clip.start(); 
      } catch (UnsupportedAudioFileException | LineUnavailableException ex) { 
       ex.printStackTrace(); 
      } 
     } catch (IOException exp) { 
      exp.printStackTrace(); 
     }} 
     public static class PlayPopupMessageSound implements Runnable { 

     @Override 
     public void run() { 
      playPopupMessageSound(); 
     } 
}} 
+1

写一个包装类更是把你需要一个'JOptionPane'的最常用参数,自己构造它并在显示之前播放声音 – MadProgrammer

+0

我对Java很缺乏经验,所以你可能会详细阐述一下,对不起。 – blazelflack

+0

我做过了,全部都在回答中 – MadProgrammer

回答

0

有关如何使用音频API的更多信息,请参阅Sound Tutorial

基本的想法是在显示对话框之前尝试播放声音,但这可能意味着声音在对话框实际上可见之前就已经完成了(如很长一段时间)。

我们可以使用Thread来尝试延迟音频的播放,但我只是使用了SwingUtilities.invokeLater,这似乎工作得很好。我的机器负载很重,因此您正在测试的机器可能会找到不同的结果并需要进行一些额外的调整。音频文件的大小也可能起作用,所以要小心。

接下来,我创建了一个简单的说服方法,它会播放使用弹出相关的音频和显示对话框,让生活更简单,例如...

import java.awt.Component; 
import java.awt.EventQueue; 
import java.io.IOException; 
import java.io.InputStream; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.UnsupportedAudioFileException; 
import javax.swing.JOptionPane; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestMessage { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       showMessage(null, "Be happy", "You are now happy"); 
      } 
     }); 
    } 

    public static void showMessage(Component parent, String title, String message) { 

     SwingUtilities.invokeLater(new PlayPopupMessageSound()); 
     JOptionPane.showMessageDialog(parent, message, title, JOptionPane.PLAIN_MESSAGE); 

    } 

    protected static void playPopupMessageSound() { 

     try (InputStream is = TestMessage.class.getResourceAsStream("/audio/TaDa.wav")) { 
      try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is)) { 
       Clip clip = AudioSystem.getClip(); 
       clip.open(audioInputStream); 
       clip.start(); 
      } catch (UnsupportedAudioFileException | LineUnavailableException ex) { 
       ex.printStackTrace(); 
      } 
     } catch (IOException exp) { 
      exp.printStackTrace(); 
     } 

    } 

    public static class PlayPopupMessageSound implements Runnable { 

     @Override 
     public void run() { 
      playPopupMessageSound(); 
     } 

    } 

} 
+0

如果我通过电子邮件发送给我的代码,您可以看一下吗?我不确定我在搞什么。 – blazelflack

+0

用你使用的代码更新你的问题 – MadProgrammer

+0

我刚刚做了,它可能真的很愚蠢,我以前从未使用过声音文件。 – blazelflack