我能够播放.wav文件,但是当它播放时,它会暂停程序(取决于它的位置),然后让程序恢复。我想知道我是否能够阻止这一点,并在程序的其余部分执行时让它发挥作用。有谁知道我必须尝试使这个工作的任何具体方法?我不是要求你们为我写的;我只需要知道是否有更有效的方法(尝试谷歌,但我没有找到任何东西)。任何帮助表示赞赏! :)
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
import java.io.*;
import javax.imageio.*;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioSystem;
public class CharacterCreator extends JOptionPane
{
public static void main (String[] args)
{
File Sound = new File("badapple.wav");
String name, ageStr, gender, finalName, femaleAge, maleAge, femaleGender, maleGender, fileCreated, error;
int age, again;
do
{
//Initiates .wav file
PlaySound(Sound);
name = JOptionPane.showInputDialog("What is your character's name?");
gender = JOptionPane.showInputDialog("Is your character female or male?");
ageStr = JOptionPane.showInputDialog("How old is your character?");
age = Integer.parseInt(ageStr);
finalName = "Your character's name is " + name.substring(0, 1).toUpperCase() + name.substring(1);
if (gender.matches("female|Female|FEMALE"))
{
femaleGender = "Your character is a female!";
femaleAge = "She is " + age + " year(s) old!";
JOptionPane.showMessageDialog(null, finalName);
JOptionPane.showMessageDialog(null, femaleGender);
JOptionPane.showMessageDialog(null, femaleAge);
}
else if (gender.matches("male|Male|MALE"))
{
maleGender = "Your character is a male!";
maleAge = "He is " + age + " year(s) old!";
JOptionPane.showMessageDialog(null, finalName);
JOptionPane.showMessageDialog(null, maleGender);
JOptionPane.showMessageDialog(null, maleAge);
}
fileCreated = name.substring(0, 1).toUpperCase() + name.substring(1) + "'s file has been created!";
PrintWriter out;
try
{
out = new PrintWriter(name + ".txt");
out.println("******************************************");
out.println("** **");
out.println("** WELCOME TO YOUR CHARACTER'S PROFILE! **");
out.println("** **");
out.println("******************************************");
out.println("");
out.println("Name: " + name.substring(0, 1).toUpperCase() + name.substring(1));
out.println("Gender: " + gender.substring(0, 1).toUpperCase() + gender.substring(1));
out.println("Age: " + age);
out.close();
}
catch (FileNotFoundException e)
{
System.err.println("File doesn't exist");
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, fileCreated);
again = JOptionPane.showConfirmDialog(null, "Do Another?");
}
while (again == JOptionPane.YES_OPTION);
}
static void PlaySound (File Sound)
{
try
{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(Sound));
clip.start();
Thread.sleep(clip.getMicrosecondLength()/1000);
}
catch (Exception e)
{
System.out.println("Cannot find or play sound!");
}
}
}
答案 0 :(得分:2)
删除此行。
Thread.sleep(clip.getMicrosecondLength()/1000);
如果要从磁盘读取文件,您可能还需要考虑多线程。
答案 1 :(得分:0)
赞
JOptionPane jop = new JOptionPane(null);