2015-09-07 31 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