Embedding music in Java  | Newbie | | Join Date: Nov 2008 Location: Nadur, Gozo, Malta
Posts: 14
| | |
Hey there,
I was doing a simple program using JCreator where I wanted to embed a song. However I don't know how I can do this, and although I browsed thoroughly through the web, I did not find any examples on how one can embed some music in his program...
Any idea on how I can embed a song from My Documents (or elsewhere) into my program?
Any help would be much appreciated.
Regards,
Tiktik
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Embedding music in Java Quote:
Originally Posted by tiktik Hey there,
I was doing a simple program using JCreator where I wanted to embed a song. However I don't know how I can do this, and although I browsed thoroughly through the web, I did not find any examples on how one can embed some music in his program...
Any idea on how I can embed a song from My Documents (or elsewhere) into my program?
Any help would be much appreciated.
Regards,
Tiktik
There's a method newAudioClip() in the Applet class; no need to worry because it's a static method and no Applet is needed; it could've had a better place but there are more examples like that. Given a clip you can play() it, stop() it and loop() it over and over again.
kind regards,
Jos
|  | Newbie | | Join Date: Nov 2008 Location: Nadur, Gozo, Malta
Posts: 14
| | | re: Embedding music in Java Quote:
Originally Posted by JosAH There's a method newAudioClip() in the Applet class; no need to worry because it's a static method and no Applet is needed; it could've had a better place but there are more examples like that. Given a clip you can play() it, stop() it and loop() it over and over again.
kind regards,
Jos Thanks a lot,
I've found an example as well an did this program, however there is one problem. - import java.applet.Applet;
-
import java.applet.AudioClip;
-
import java.net.URL;
-
-
public class Music_1 {
-
public static void main(String[] args) {
-
try {
-
URL url = new URL("file:Zocalo.wav");
-
AudioClip ac = Applet.newAudioClip(url);
-
ac.play();
-
-
System.out.println("Press any key to exit.");
-
System.in.read();
-
ac.stop();
-
} catch (Exception e) {
-
System.out.println(e);
-
}
-
}
-
}
-
It compiles and runs, without any errors. But the music does not come out. Where should I put the music file which i want to play (in which folder)?
kind regards,
tiktik
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Embedding music in Java Quote:
Originally Posted by tiktik It compiles and runs, without any errors. But the music does not come out. Where should I put the music file which i want to play (in which folder)? Don't rely on an artifact that your (sound) resource is stored in a file (it might be stored in a .jar file). For now store the sound file where your class file is stored and find it relative to that class by doing: -
URL yourClipURL= YourClass.class.getResource("yourClipResource");
-
AudioClip yourClip= Applet.newAudioClip(yourClipURL);
-
kind regards,
Jos
|  | Newbie | | Join Date: Nov 2008 Location: Nadur, Gozo, Malta
Posts: 14
| | | re: Embedding music in Java Quote:
Originally Posted by JosAH Don't rely on an artifact that your (sound) resource is stored in a file (it might be stored in a .jar file). For now store the sound file where your class file is stored and find it relative to that class by doing: -
URL yourClipURL= YourClass.class.getResource("yourClipResource");
-
AudioClip yourClip= Applet.newAudioClip(yourClipURL);
-
kind regards,
Jos
Thanks, I done just that, and the resource file is indeed getting accessed now... however still no music comes out. I find it very strange seeing that the sound file is being accessed, yet still no sound is heard. Is the Windows Media Player supposed to open upon access of the resource file?
thanks a lot,
tiktik
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Embedding music in Java Quote:
Originally Posted by tiktik Thanks, I done just that, and the resource file is indeed getting accessed now... however still no music comes out. I find it very strange seeing that the sound file is being accessed, yet still no sound is heard. Is the Windows Media Player supposed to open upon access of the resource file? When you print out the URL, does it really point to your sound clip? Can you play sounds on your computer at all? The volume is down mayhap?
kind regards,
Jos (just guessing)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|