473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
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

5 7273
r035198x
13,262 8TB
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
@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
r035198x
13,262 8TB
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
@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
r035198x
13,262 8TB
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

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

Similar topics

2
by: Josh | last post by:
I am having a problem with sound in my program, it takes information from a TargetDataLine, put's it in a temporary file, then takes it back out of the temporary file and plays it using a...
2
by: Udipta | last post by:
I am making a java application where I am required to record voice in a-law format. The program i have written starts perfectly without giving any error or warning, but as soon as i start recording...
1
by: Maciek Zobniow | last post by:
....or at least Sun JMS. Do you know any? Thanks! -- Maciej Zobniow
2
jeffbroodwar
by: jeffbroodwar | last post by:
i'm currently working on a project about webservices. i tried to create an ordinary service that inserts data to the database. here's the code : ===================================================...
2
by: lucifer | last post by:
Hi we have Sun Java System Application Server Platform Edition 9.0 running in our office but I have to work in Php but dont know how to run php on the server. .So can any body tell me how can i...
0
by: abhilash12 | last post by:
pls give me example for business tier for java web application
1
by: sukatoa | last post by:
Now, im currently on an experiment on Java Sound API.... now, i am attempting to capture the duration ( time limit of a given wav file )but still have no idea... I found this link -7.8 but i...
1
by: ajmerasunny | last post by:
Hi All, I have a java web application in which I need to read a configuration file(WebAgent.conf) that has the reference of another configuration file(SmHost.conf). I am placing both these...
2
Raazkalra
by: Raazkalra | last post by:
Hello! I'm new here and it is my first post so pardon me if i make mistake.I want to make java mobile application.plz tell me the prerequiste to create an application.i have a basic knowledge of...
3
by: Akino877 | last post by:
Hello, I have a Java Swing application that I would like to be able to forward to or to run it - sorry, I am not sure if I am using the right term - from a JSP page. And I would like for my Java...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.