Embedding music in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tiktik
    New Member
    • Nov 2008
    • 14

    Embedding music in Java

    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
    Recognized Expert MVP
    • Mar 2007
    • 11453

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

    Comment

    • tiktik
      New Member
      • Nov 2008
      • 14

      #3
      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.

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

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        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:

        Code:
        URL yourClipURL= YourClass.class.getResource("yourClipResource");
        AudioClip yourClip= Applet.newAudioClip(yourClipURL);
        kind regards,

        Jos

        Comment

        • tiktik
          New Member
          • Nov 2008
          • 14

          #5
          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:

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

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            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)

            Comment

            Working...