即时通讯尝试在Java程序中播放声音,但我不知道最新的错误。当我尝试使用此代码时,声音正在播放,但其他程序中没有其他事情发生。 这里是我到目前为止已经完成:(在程序的中间重要的代码)不能在Java中播放声音
//IMPORTS
import java.io.*;
import java.net.URL;
//imports for sound
import javax.sound.sampled.*;
import javax.swing.*;
//the program is an elevator but i dont think this is important
public class Aristi_Makri_ansanswer extends JFrame {
public static void main(String[] args){
try{
int orofos=0; //arxikopoiisi metavlitwn
int epilogi=0;
String koumpi="";
//here i read the floor choice
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
Aristi_Makri_ansanswer name = new Aristi_Makri_ansanswer();
System.out.println(" To open the door press ^"); //ekswteriko koumpi
//press a key to open the door
koumpi=input.readLine();
if (koumpi=="^") { //anoigei i porta me to patima tou^
name.anoigma_portas();
}
System.out.println(" I porta anoikse ");
name.anoigma_fws(); //kalei tin sinartisi gia to anoigma portas
System.out.println(" to fws anoikse ");
int atoma =(int)(Math.random()*10)+1; //random pou vgazei mexri 10 atoma
//people getting into the elevator
System.out.println("mpikan "+atoma+" atoma mesa sto ansanswer"); // minima posa atoma mpikan
//no more than 6 people are allowed in the elevator
if(atoma>6){ //an ta atoma ine panw apo 6 tha vgazei minima oti xwrei mono 6 atoma k yparxei alli random pou tha vgazei arithmous mexri to 6
System.out.println("to ansanswer xwrei mexri 6 atoma.Parakalw na meinete mexri 6 atoma");
atoma =(int)(Math.random()*6)+1;
System.out.println("paremeinan "+atoma+" atoma mesa sto ansanswer");
}
//// floor choices
System.out.println(" 0 "); // menu gia epilogi orofou
System.out.println(" 1 ");
System.out.println(" 2 ");
System.out.println(" 3 ");
System.out.println("Choose a floor:");
epilogi = Integer.parseInt(input.readLine());
new Aristi_Makri_ansanswer();
////////here is the exact place i would like the sound to play!!!!
//IXOOOOOOOOOOOOOOOS
switch (epilogi) { // minima pou vgazei se poio orofo vriskese analoga me tin epilogi tou xristi
case 0: System.out.println("Vriskeste ston orofo 0");break;
case 1: System.out.println("Vriskeste ston orofo 1");break;
case 2: System.out.println("Vriskeste ston orofo 2");break;
case 3: System.out.println("Vriskeste ston orofo 3");break;
}
name.anoigma_portas(); //kalei sinartisi gia anoigma portas
name.kleisimo_fws(); // kalei sinartisi gia kleisimo fws
name.kleisimo_portas(); //kalei sinartisi gia kleisimo portas
}catch (IOException e) {};
}
public Aristi_Makri_ansanswer() {
////////////////////////////////////////////////
///////// important code here!!!/////////////////
/////////////////////////////////////////
try {
// Open an audio input stream.
URL url = this.getClass().getClassLoader().getResource("mixani.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
// Get a sound clip resource.
Clip clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public void kleisimo_portas(){ //sinartisi gia kleisimo portas
boolean porta=true;
if (porta==true){
porta=false;
System.out.println("I porta ekleise");
}
}
public void anoigma_portas(){ //sinartisi gia anoigma portas
boolean porta=false;
if (porta==false){
porta=true;
System.out.println("I porta anoikse");
}
}
public void anoigma_fws(){ // sinartisi gia anoigma fws
boolean fws=false;
if (fws==false){
fws=true;
System.out.println("to fws anoikse");
}
}
public void kleisimo_fws(){ // sinartisi gia kleisimo fws
boolean fws=true;
if (fws==true){
fws=false;
System.out.println("to fws ekleise");
}
}
}
用较短的.wav文件试试吧。如果您的程序在播放后继续播放,您知道您正在播放的剪辑太长,并阻止当前线程。 –
尝试在JavaSound信息中的[播放'剪辑'](http://stackoverflow.com/tags/javasound/info)下显示的源代码。页。 1)它是否完全符合你的要求(使用'leftright.wav')2)该代码是否适用于你的声音? –