gpt4 book ai didi

java - JOptionPane中的音频文件

转载 作者:行者123 更新时间:2023-12-03 01:59:57 25 4
gpt4 key购买 nike

我正在尝试为Java的高级项目选择像英雄联盟这样的冠军。我认为到目前为止,我的表现不错,但是我仍然坚持某些东西。我想尝试打开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();
}
}}

最佳答案

首先查看Sound Tutorial,以获取有关如何使用音频API的更多信息。

基本思想是在显示对话框之前尝试播放声音,但这可能意味着声音在对话框实际可见之前就已结束(例如,很长一段时间)。

我们可以使用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();
}

}

}

关于java - JOptionPane中的音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32429929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com