Java: How to add Sound to Application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeNoob117
    New Member
    • Jan 2013
    • 19

    Java: How to add Sound to Application

    I want to add some background music to a credit line and other parts of the application, but I do not know how to go about such. I tried importing the sun.audio.*; and use the AudioPlayer, AudioStream, etc. however, there is a restriction to the access. Is there any way around this or any other method to add music/sound to the application?
    Any help would be appreciated, thanks in advance.
    -CodeNoob
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You should be able to play by just using
    Code:
    Clip clip = AudioSystem.getClip();
            // getAudioInputStream() also accepts a File or InputStream
            AudioInputStream ais = AudioSystem.
                getAudioInputStream( url );
            clip.open(ais);
            clip.loop(Clip.LOOP_CONTINUOUSLY);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // A GUI element to prevent the Clip's daemon Thread
                    // from terminating at the end of the main()
                    JOptionPane.showMessageDialog(null, "Close to exit!");
                }
            });
    The Java sound tutorial here :http://docs.oracle.com/javase/tutorial/sound/index.html

    Comment

    • CodeNoob117
      New Member
      • Jan 2013
      • 19

      #3
      More Information Please?

      Originally posted by r035198x
      You should be able to play by just using
      Code:
      Clip clip = AudioSystem.getClip();
              // getAudioInputStream() also accepts a File or InputStream
              AudioInputStream ais = AudioSystem.
                  getAudioInputStream( url );
              clip.open(ais);
              clip.loop(Clip.LOOP_CONTINUOUSLY);
              SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      // A GUI element to prevent the Clip's daemon Thread
                      // from terminating at the end of the main()
                      JOptionPane.showMessageDialog(null, "Close to exit!");
                  }
              });
      The Java sound tutorial here :http://docs.oracle.com/javase/tutorial/sound/index.html
      Thanks, you helped me a lot, however I am getting this error at the moment: Exception in thread "main" javax.sound.sam pled.Unsupporte dAudioFileExcep tion: could not get audio input stream from input file

      I looked through the link you provided(Thank you for doing so) but I did not manage to see any specified files the Clip and Audio classes read. What audio files are allowed or how can I get it to read an "unsupporte d" file?
      Thanks for your help
      -CodeNoob

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        The types supported are listed by calling AudioSystem.get AudioFileTypes( ); To support other formats you can install the JMF mp3 plugin available here :http://www.oracle.com/technetwork/ja...ad-137625.html

        Comment

        • CodeNoob117
          New Member
          • Jan 2013
          • 19

          #5
          Originally posted by r035198x
          The types supported are listed by calling AudioSystem.get AudioFileTypes( ); To support other formats you can install the JMF mp3 plugin available here :http://www.oracle.com/technetwork/ja...ad-137625.html
          Thank you. Though my problem persists. I will provide some code for you; if you're willing to continue to help me.

          Code:
          public class Audio {
          	public static void main(String args[]) throws IOException, UnsupportedAudioFileException, LineUnavailableException{
          		Clip clip = AudioSystem.getClip();
          		 // getAudioInputStream() also accepts a File or InputStream
          		 AudioInputStream ais = AudioSystem.getAudioInputStream((new File("Fanfare for the Common Man.wav")));
          	     clip.open(ais);
          		 clip.loop(Clip.LOOP_CONTINUOUSLY);
          		 SwingUtilities.invokeLater(new Runnable() {
          		 public void run() {
          		 // A GUI element to prevent the Clip's daemon Thread
          		 // from terminating at the end of the main()
          		 JOptionPane.showMessageDialog(null, "Close to exit!");
          		 }
          		});
          
          }
          }
          I am still getting this as a result: Exception in thread "main" javax.sound.sam pled.Unsupporte dAudioFileExcep tion: could not get audio input stream from input file
          at javax.sound.sam pled.AudioSyste m.getAudioInput Stream(Unknown Source)
          at Game2.Audio.mai n(Audio.java:30 )

          Am I doing something wrong? I have tried other methods of adding sound, however I keep hitting the same codeblock. And Thanks for everything.
          -CodeNoob

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            wav file is a container type. It's possible the file you have could be a container for an unsuppported type in which case you would need to try JMF like I suggested above. First, though try other simple wav files to see if they work, also not sure about those spaces in your file name, try renaming it to a name without spaces just see if that isn't causing a problem too.

            Comment

            Working...