6 Replies - 3597 Views - Last Post: 06 May 2010 - 12:59 PM Rate Topic: -----

#1 cotekyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-September 09

Getting sound to work in a jar file

Posted 04 May 2010 - 08:24 AM

OK so I am trying to implement sound in my application, it works when I run it in Eclipse, but when I export it as a runnable jar file the sound is lost and the jar file will not run when double-clicked. When I remove the sound from my application and export it as a runnable jar it works perfectly. Can somebody please tell me what I am doing wrong?

To implement sound I have been using a SoundEffect class which plays .wav files, I thought I diagnosed my problem as using that file type, and tried using .snd instead, but it didn't work. An example I found on the Sun website used
play(getClass().getResource("audio/xyz.au"));
but that only seems to work if the class extends Applet and I am extending Canvas.

Here is some of the code I'm currently using in the SoundEffect class.
SoundEffect(String soundFileName) {
      try {
         // Use URL (instead of File) to read from disk and JAR.
         URL url = this.getClass().getClassLoader().getResource(soundFileName);
         // Set up an audio input stream piped from the sound file.
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
         // Get a clip resource.
         clip = AudioSystem.getClip();
         // Open audio clip and load samples from the audio input stream.
         clip.open(audioInputStream);
      } catch (UnsupportedAudioFileException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (LineUnavailableException e) {
         e.printStackTrace();
      }
   }
 
   // Play or Re-play the sound effect from the beginning, by rewinding.
   public void play() {
      if (volume != Volume.MUTE) {
        volume = Volume.HIGH;
         if (clip.isRunning())
            clip.stop();   // Stop the player if it is still running
         clip.setFramePosition(0); // rewind to the beginning
         clip.loop(0);    // Start playing
          clip.start();
      }
   }


Any/all help would be appreciated. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Getting sound to work in a jar file

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Getting sound to work in a jar file

Posted 04 May 2010 - 12:37 PM

Are you sure the sound files are in the correct locations in the JAR?

Also, you should check out this tutorial for an easier way of going about it
http://www.dreaminco...wtopic14083.htm :)
Was This Post Helpful? 0
  • +
  • -

#3 cotekyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-September 09

Re: Getting sound to work in a jar file

Posted 05 May 2010 - 07:49 AM

Thank you, can you extend to more than one thing? Because I'm already extending Canvas.

i.e. public class XYZ extends Canvas & JApplet
Was This Post Helpful? 0
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Getting sound to work in a jar file

Posted 05 May 2010 - 01:45 PM

No, you can only extend one class. You can implement as many interfaces as needed though.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Getting sound to work in a jar file

Posted 05 May 2010 - 03:34 PM

View Postcotekyle, on 05 May 2010 - 09:49 AM, said:

Thank you, can you extend to more than one thing? Because I'm already extending Canvas.

i.e. public class XYZ extends Canvas & JApplet


As erik.price said, you cannot. However, I think that you should make your custom painting be in its own Java file, extending Canvas (or JPanel), then a separate file with the class that extends JApplet.
Was This Post Helpful? 0
  • +
  • -

#6 cotekyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-September 09

Re: Getting sound to work in a jar file

Posted 06 May 2010 - 08:30 AM

View PostDogstopper, on 05 May 2010 - 02:34 PM, said:

View Postcotekyle, on 05 May 2010 - 09:49 AM, said:

Thank you, can you extend to more than one thing? Because I'm already extending Canvas.

i.e. public class XYZ extends Canvas & JApplet


As erik.price said, you cannot. However, I think that you should make your custom painting be in its own Java file, extending Canvas (or JPanel), then a separate file with the class that extends JApplet.


I tried keeping the Sound class that extends JApplet (from the tutorial mentioned before) separate from my class extending Canvas. I am trying to call from the main class extending Canvas by
TESTSOUND.init();
after instantiating it earlier as
private SoundPlayer TESTSOUND;
. I figured then it would just call the init() method which locates the audio file and plays it but that gives me a NullPointerException for the line where I called init(). I feel like I'm just thinking in circles and not getting anywhere. >.<
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Getting sound to work in a jar file

Posted 06 May 2010 - 12:59 PM

We can't help without having code to reference.
:code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1