0

I wanted to try to run one example of "Playing sound in Java" but its not working, throwing me an Exception! This is The JFrame Class

public class JFrame {
JFrame frame = new JFrame();
}

Here is the SoundEffect enum.

import java.io.IOException;
import java.net.URL;
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;


public enum SoundEffect {
SONG("Sound.wav");
 public static enum Volume {
      MUTE, LOW, MEDIUM, HIGH
   }
 public static Volume volume = Volume.LOW;
   private Clip clip;
   SoundEffect(String SONG) {
          try {
             URL url = this.getClass().getClassLoader().getResource(SONG);
             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
             clip = AudioSystem.getClip();
             clip.open(audioInputStream);
          } catch (UnsupportedAudioFileException e) {
             e.printStackTrace();
          } catch (IOException e) {
                 e.printStackTrace();
          } catch (LineUnavailableException e) {
                 e.printStackTrace();
              }
       }
   public void play() {
          if (volume != Volume.MUTE) {
             if (clip.isRunning())
                clip.stop();   
             clip.setFramePosition(0); 
             clip.start();     
          }
       }
   static void init() {
          values();
   }
}

The SoundEffectDemo class.

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class SoundEffectDemo extends JFrame {

public SoundEffectDemo(){
    SoundEffect.init();
     SoundEffect.volume = SoundEffect.Volume.LOW;
     Container cp = this.getContentPane();
      cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
      JButton btnSound1 = new JButton("Sound 1");
      btnSound1.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
             SoundEffect.SONG.play();
          }
       });
      cp.add(btnSound1);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setTitle("Test SoundEffct");
      this.pack();
      this.setVisible(true);
}
public static void main(String[] args) {
    new SoundEffectDemo();
}

}

Everything seems to be normal, there are no red lines. But whenever i try to run it, its giving me that.

Exception in thread "main" java.lang.ExceptionInInitializerError
at SoundEffectDemo.<init>(SoundEffectDemo.java:12)
at SoundEffectDemo.main(SoundEffectDemo.java:30)
Caused by: java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(Unknown Source)
at javax.sound.midi.MidiSystem.getSequence(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at SoundEffect.<init>(SoundEffect.java:20)
at SoundEffect.<clinit>(SoundEffect.java:11)
... 2 more

Any idea how to fix that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Leo
  • 1
  • I'd take a look at this question/answers - http://stackoverflow.com/questions/2416935/how-to-play-wav-files-with-java. As a guess I'd say you don't have a default device configured for clip playback as described in the JavaDoc for AudioSystem.getClip() - http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html#getClip() – Nick Holt Jan 07 '14 at 17:05
  • 1
    The stack trace seems to indicate that `url` is null. Which probably means Sound.wav isn't in the classpath. – VGR Jan 07 '14 at 18:14
  • Yes but i know another way of adding sound to java and when i do it that way "Sound.wav" works without problems. – Leo Jan 07 '14 at 19:20
  • 1
    Tip: Add @VGR (or whoever - the `@` is important) to *notify* them of a new comment. -- BTW - agree this really comes down to that either the sound is not on the class-path, is not the place on the class-path you expect, or uses different case than seen. A lot more detail of the class-path and where the sound is located would be very handy in solving this. E.G. What is the class-path? Which Jar is the sound located in? What is the output of `jar -tvf that.jar`? – Andrew Thompson Jan 08 '14 at 00:40
  • Sorry but i really dont know what is those things about.Today I tried writing one program which is loading .txt file but it gives me "issue:java.io.FileNotFoundException", can you help me how can i make my programs see my files and read them. Usually my programs are in "C:/eclipse/eclipse1/ProjectName". – Leo Jan 08 '14 at 10:47
  • 1
    *"Sorry but i really dont know what is those things about."* Sorry, but given that 'shrug of the shoulders' I really can't help you. SO is not the place to find a tutor, and that is what you seem to need. – Andrew Thompson Jan 09 '14 at 01:53

0 Answers0