2012-01-23 73 views
0

即時通訊嘗試在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"); 
    } 
} 

}

+0

用較短的.wav文件試試吧。如果您的程序在播放後繼續播放,您知道您正在播放的剪輯太長,並阻止當前線程。 –

+0

嘗試在JavaSound信息中的[播放'剪輯'](http://stackoverflow.com/tags/javasound/info)下顯示的源代碼。頁。 1)它是否完全符合你的要求(使用'leftright.wav')2)該代碼是否適用於你的聲音? –

回答

2

是這樣的功課?

如果您的代碼是英文的,您會得到更多的回覆。

我猜想問題是您的聲音文件與您的程序的其餘部分在相同的線程中播放,因此阻止。它需要是一個單獨的線程。

+0

如何在單獨的線程中播放聲音?編程中的線程是什麼?java代碼中的線程是什麼? – Dchris

+0

線程本質上是您的主要Java程序中的微型程序。如果你打開Windows任務管理器,並點擊「性能」,你會看到一些線程。線程可以被告知睡覺和其他功能。有關線索的更多信息,請訪問:http://docs.oracle.com/javase/tutorial/essential/concurrency/ – Jimmt

0

嘗試這一點,你需要將歌曲導入到項目src文件夾

import sun.applet.AppletAudioClip; 
... 
URL myURL= ClassLoader.getSystemResource("song.wav"); 
AppletAudioClip song = new AppletAudioClip(myURL); 
song.play(); 
...