Results 1 to 11 of 11
- 12-22-2009, 12:51 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Why doesnt the sound play when I change the path?
Hi. I am trying to play a background sound in my program and so far, after searching and reading lots of posts I finally have a "TaDa" sound playing once when the program starts. But what I want is a sound to be looped everytime I start a program. That sound will be in the same folder as the program.
This is the code that I put in public static void main() to play the sound:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
java_testt b = new java_test();
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.startApp();
}
});
}
private void startApp()
{playStartupSound();}
private void endApp() {System.exit(0);}
private void playStartupSound() {
Runnable soundPlayer = new Runnable() {
public void run() {
try {
File tadaSound = new File(System.getenv("windir") "/" //This is the windows tada sound. All I want is
"media/tada.wav"); //to change the path to another sound
AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(new FileInputStream(tadaSound));
AudioFormat audioFormat = audioInputStream
.getFormat();
DataLine.Info dataLineInfo = new DataLine.Info(
Clip.class, audioFormat);
Clip clip = (Clip) AudioSystem
.getLine(dataLineInfo);
clip.open(audioInputStream);
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
Thread soundPlayingThread = new Thread(soundPlayer);
soundPlayingThread.start();
}
}
Does anyone know how to choose another sound in the same folder as java_test and make it work with this code because when I change the ath it doesnt run.
Thanks.
- 12-22-2009, 01:02 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
What happens when you change the literal substring "tada.wav" to another (existing) file? That should run as well if this version works ...
kind regards,
Jos
- 12-22-2009, 01:06 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Well it doesnt work because the TaDa file is situated in the Windows installation folder whereas the sound I want to be played is situated in C:\users\azaz\netbeans\3.6\sampledir\welcome.wav and when I change the path it doesnt work. I just need help with the path because I cannot get it to work. The "WINDIR" but confuses me.
This is the error I get:
java.io.FileNotFoundException: C:\Windows\media\welcome.wav (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
at java_assignment$2.run(java_assignment.java:341)
at java.lang.Thread.run(Thread.java:595)
- 12-22-2009, 01:19 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 12-22-2009, 01:44 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Well after I do that the program runs, the drawing n the 700x700px screen appears but there is no sound and I get this:
java.io.FileNotFoundException: C:\users\azaz\netbeans\3.6\sampledir\welcome.wav (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
at java_assignment$2.run(java_assignment.java:341)
at java.lang.Thread.run(Thread.java:595)
And also my program is called java_assigmnment but in the file there is also another file called java_assignment$2, why is that?
Thanks in advance
- 12-22-2009, 01:56 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Well, if the jvm can't find the file where you told to look for it, it really isn't there; check that complete file path. w.r.t. that java_assignment$2 class: that is the name of an anonymous class (e.g. an ActionListener); it needs to have a name so javac, your compiler, made one for you.
kind regards,
Jos
- 12-22-2009, 02:03 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Hi. Thanks a lot for your help. Is does work now. I have forgotten a . the path is C:\Users\azaz\.netbeans\3.6\sampledir. Do you see the dot between azaz and netbeans? Thats what i missed.
Can you help me loop the sound so it plays until I close the application?
Thanks a lot for your help Jos. You really helped me. Thank you.Last edited by ProGenius; 12-22-2009 at 02:16 PM.
- 12-22-2009, 03:21 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 12-22-2009, 04:36 PM #9
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
maybe you can just create a loop, i am not sure but maybe you can do something like this.
can someone confirm this? but i think that is a way to make a loop.Java Code:music = 5; if (music <6) { //play sound here }
- 12-22-2009, 04:39 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 12-22-2009, 04:58 PM #11
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
Similar Threads
-
JFrame window doesnt change background color
By Addez in forum New To JavaReplies: 7Last Post: 11-07-2009, 09:38 PM -
Dll Call doesnt work
By INFACT in forum New To JavaReplies: 1Last Post: 10-04-2009, 09:31 PM -
My program doesnt display anything
By Bojevnik in forum AWT / SwingReplies: 2Last Post: 10-19-2007, 02:50 PM -
Make sound play in a java application
By lenny in forum AWT / SwingReplies: 2Last Post: 08-13-2007, 11:45 AM -
Play sound and Video in a Mobile Phone
By luisarca in forum Sun Java Wireless ToolkitReplies: 2Last Post: 05-23-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks