2015-01-23 12 views
0

나는 Java에서 사운드를 재생하려고 노력해 왔으며 멈추었습니다.AudioClip이 작동하지 않습니다.

public class Audioapp extends JApplet 
{ 

    public class Sound{ 
     public AudioClip song; 
     public URL songPath; 
     Sound(){ 

      try{ 

       songPath = new URL(getCodeBase(), "leftfordeath.wav"); 

       song = Applet.newAudioClip(songPath); 

      } 
      catch (Exception e) {} 

     } 
     public void playSoundOnce(){ 
      song.play(); 
     } 
    } 

    public void init(){ 
     Sound test = new Sound(); 
     test.playSoundOnce(); 
    } 

} 

내 메인에서 init을 호출하면 song.play()에 nullPointerExeption이 있다고 표시됩니다. 내가 뭘 잘못하고 있니, 제발 도와주세요 ...

+0

정말 JApplet을 사용해야합니까? 당신의 주된 방법으로 그것을 부른 이후로 당신이하지 말 것을 제안 할 것입니다. 또한 예외를 무시하지 말고 try-catch 블록의 catch 부분에 e.printStackTrace()를 추가하고 그 내용을 확인하십시오 ... – MadProgrammer

답변

1

playsoundOnce 코드가있는 곳에서이 코드를 대체하여 어떻게 사용하는지보십시오.

public void playSoundOnce() 
{ 
    try 
    { 
    InputStream inputStream = getClass().getResourceAsStream(leftfordeath.wav); 
    AudioStream audioStream = new AudioStream(inputStream); 
    AudioPlayer.player.start(audioStream); 
    } 
    catch (Exception e) 
    { 
    if (debugFileWriter!=null) e.printStackTrace(debugFileWriter); 
    } 
} 
+0

스택 추적을 작성하면 가장 먼저 할 일이 될 것입니다. 코드가 현재 작성된 방식으로, 애플릿은 '블라인드 비행'입니다. 그러나 그것을 변경 :'if (debugFileWriter! = null) e.printStackTrace (debugFileWriter); else e.printStackTrace();'.. –