By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,651 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,651 IT Pros & Developers. It's quick & easy.

Java: How to add Sound to Application

P: 19
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
Mar 31 '13 #1

✓ answered by r035198x

You should be able to play by just using
Expand|Select|Wrap|Line Numbers
  1. Clip clip = AudioSystem.getClip();
  2.         // getAudioInputStream() also accepts a File or InputStream
  3.         AudioInputStream ais = AudioSystem.
  4.             getAudioInputStream( url );
  5.         clip.open(ais);
  6.         clip.loop(Clip.LOOP_CONTINUOUSLY);
  7.         SwingUtilities.invokeLater(new Runnable() {
  8.             public void run() {
  9.                 // A GUI element to prevent the Clip's daemon Thread
  10.                 // from terminating at the end of the main()
  11.                 JOptionPane.showMessageDialog(null, "Close to exit!");
  12.             }
  13.         });
  14.  
The Java sound tutorial here :http://docs.oracle.com/javase/tutorial/sound/index.html

Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
You should be able to play by just using
Expand|Select|Wrap|Line Numbers
  1. Clip clip = AudioSystem.getClip();
  2.         // getAudioInputStream() also accepts a File or InputStream
  3.         AudioInputStream ais = AudioSystem.
  4.             getAudioInputStream( url );
  5.         clip.open(ais);
  6.         clip.loop(Clip.LOOP_CONTINUOUSLY);
  7.         SwingUtilities.invokeLater(new Runnable() {
  8.             public void run() {
  9.                 // A GUI element to prevent the Clip's daemon Thread
  10.                 // from terminating at the end of the main()
  11.                 JOptionPane.showMessageDialog(null, "Close to exit!");
  12.             }
  13.         });
  14.  
The Java sound tutorial here :http://docs.oracle.com/javase/tutorial/sound/index.html
Mar 31 '13 #2

P: 19
@r035198x
Thanks, you helped me a lot, however I am getting this error at the moment: Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: 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 "unsupported" file?
Thanks for your help
-CodeNoob
Mar 31 '13 #3

10K+
P: 13,264
The types supported are listed by calling AudioSystem.getAudioFileTypes(); To support other formats you can install the JMF mp3 plugin available here :http://www.oracle.com/technetwork/ja...ad-137625.html
Mar 31 '13 #4

P: 19
@r035198x
Thank you. Though my problem persists. I will provide some code for you; if you're willing to continue to help me.

Expand|Select|Wrap|Line Numbers
  1. public class Audio {
  2.     public static void main(String args[]) throws IOException, UnsupportedAudioFileException, LineUnavailableException{
  3.         Clip clip = AudioSystem.getClip();
  4.          // getAudioInputStream() also accepts a File or InputStream
  5.          AudioInputStream ais = AudioSystem.getAudioInputStream((new File("Fanfare for the Common Man.wav")));
  6.          clip.open(ais);
  7.          clip.loop(Clip.LOOP_CONTINUOUSLY);
  8.          SwingUtilities.invokeLater(new Runnable() {
  9.          public void run() {
  10.          // A GUI element to prevent the Clip's daemon Thread
  11.          // from terminating at the end of the main()
  12.          JOptionPane.showMessageDialog(null, "Close to exit!");
  13.          }
  14.         });
  15.  
  16. }
  17. }
  18.  
I am still getting this as a result: Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
at Game2.Audio.main(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
Apr 1 '13 #5

10K+
P: 13,264
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.
Apr 1 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.