Connecting Tech Pros Worldwide Forums | Help | Site Map

Embedding music in Java

tiktik's Avatar
Newbie
 
Join Date: Nov 2008
Location: Nadur, Gozo, Malta
Posts: 14
#1: Jun 22 '09
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

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jun 22 '09

re: Embedding music in Java


Quote:

Originally Posted by tiktik View Post

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
tiktik's Avatar
Newbie
 
Join Date: Nov 2008
Location: Nadur, Gozo, Malta
Posts: 14
#3: Jun 22 '09

re: Embedding music in Java


Quote:

Originally Posted by JosAH View Post

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.

Expand|Select|Wrap|Line Numbers
  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.net.URL;
  4.  
  5. public class Music_1 {
  6.   public static void main(String[] args) {
  7.     try {
  8.       URL url = new URL("file:Zocalo.wav");
  9.       AudioClip ac = Applet.newAudioClip(url);
  10.       ac.play();
  11.  
  12.       System.out.println("Press any key to exit.");
  13.       System.in.read();
  14.       ac.stop();
  15.     } catch (Exception e) {
  16.       System.out.println(e);
  17.     }
  18.   }
  19. }
  20.  

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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jun 22 '09

re: Embedding music in Java


Quote:

Originally Posted by tiktik View Post

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:

Expand|Select|Wrap|Line Numbers
  1. URL yourClipURL= YourClass.class.getResource("yourClipResource");
  2. AudioClip yourClip= Applet.newAudioClip(yourClipURL);
  3.  
kind regards,

Jos
tiktik's Avatar
Newbie
 
Join Date: Nov 2008
Location: Nadur, Gozo, Malta
Posts: 14
#5: Jun 23 '09

re: Embedding music in Java


Quote:

Originally Posted by JosAH View Post

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:

Expand|Select|Wrap|Line Numbers
  1. URL yourClipURL= YourClass.class.getResource("yourClipResource");
  2. AudioClip yourClip= Applet.newAudioClip(yourClipURL);
  3.  
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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Jun 23 '09

re: Embedding music in Java


Quote:

Originally Posted by tiktik View Post

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


Similar Java bytes