Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By zFollette

Thread: problem playing sound when running jar

  1. #1
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Post problem playing sound when running jar

    hi everyone. long time since i posted/coded something again.

    so i came up with a little project as a todo in my free time.
    unfortunatly i ran into a little problem:

    when i'm trying to run the program from my IDE it works just fine, the sound is playing without a problem.
    but when i export the project to a JAR, the program starts, but it fails to load the soundfile. (prob a nub mistake from my part, but I can't seem to find it)

    here is the code:

    to play the sound:
    Java Code:
    package GUI.sound;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineUnavailableException;
    import javax.swing.JOptionPane;
    
    public class SoundPlayer {
        private AudioInputStream audioStream;
        private Clip clip;
        private boolean playing;
        
        public SoundPlayer(){
        	playing = false;
        }
        
        public void initSound(String filename){
            try {
            	InputStream in = getClass().getResourceAsStream(filename);
                audioStream = AudioSystem.getAudioInputStream(in);
            } catch (Exception e){
            	JOptionPane.showMessageDialog(null, String.format("error opening file\n%s", filename));
                e.printStackTrace();
            }
            try{
            	clip = AudioSystem.getClip();
            	clip.open(audioStream);
            }catch(LineUnavailableException lue){
            	JOptionPane.showMessageDialog(null, "error: line not avaible");
            	lue.printStackTrace();
            }catch(IOException ioe){
            	JOptionPane.showMessageDialog(null, "ioe.printStackTrace()");
            	ioe.printStackTrace();
            }catch(Exception e){
            	JOptionPane.showMessageDialog(null, "error opening file");
            	e.printStackTrace();
            }
        }
        
        public void stopPlaying(){
        	clip.stop();
        	playing = false;
        }
        
        public void startPlaying(){
        	playing = true;
        	clip.loop(Clip.LOOP_CONTINUOUSLY);
        }
        
        public boolean isPlaying(){
        	return playing;
        }
        
    }
    and this is the call i make to the above class:
    Java Code:
    private final String SOUNDLOCATION = "/sound/intro2.wav";
    
    	public void initSound(){
    		sound.initSound(SOUNDLOCATION);
    	}
    	
    	public boolean isPlaying(){
    		return sound.isPlaying();
    	}
    	
    	public void startSound(){
    		sound.startPlaying();
    	}
    	
    	public void stopSound(){
    		sound.stopPlaying();
    	}
    the sound file is also located in a folder named:
    sound

    which is located in the src folder.

    any help or hints would be really nice. ^^

    grtz liluma

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: problem playing sound when running jar

    If the code works in one place and not in another, it's probably a file location problem. What is the difference in file locations between where it works and where it doesn't?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default Re: problem playing sound when running jar

    structure in the IDE:
    problem playing sound when running jar-idestructure.png

    jar structure:
    problem playing sound when running jar-jarstructure.png

    so the maps are basicly the same?
    unless i'm overlooking something?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: problem playing sound when running jar

    The path searched with the getResourceAsStream() method is relative to the classpath.
    The post doesn't show what is in the sound folder that is in the jar file.

    Try removing the leading / in the path.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: problem playing sound when running jar

    If Norm's suggestion does not work, try adding './'

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: problem playing sound when running jar

    it fails to load the soundfile.
    Is there an error message?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default Re: problem playing sound when running jar

    i get this error:

    problem playing sound when running jar-error.png

    the error is thrown after trying to make the input stream.

  8. #8
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default Re: problem playing sound when running jar

    have found the problem

    after reading through the entire api of getAudioInputStream i found this:
    "The implementation of this method may require multiple parsers to examine the stream to determine whether they support it. These parsers must be able to mark the stream, read enough data to determine whether they support the stream, and, if not, reset the stream's read pointer to its original position. If the input stream does not support these operation, this method may fail with an IOException."

    so i tried wrapping my InputStream with a BufferedInputStream, and that solved the problem. ^^

    so code to play sound is:
    Java Code:
    package GUI.sound;
    
    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineUnavailableException;
    import javax.swing.JOptionPane;
    
    public class SoundPlayer {
        private AudioInputStream audioStream;
        private Clip clip;
        private boolean playing;
        
        public SoundPlayer(){
        	playing = false;
        }
        
        public void initSound(String filename){
            try {
            	InputStream in = getClass().getResourceAsStream(filename);
            	InputStream bufferedIn = new BufferedInputStream(in);
                audioStream = AudioSystem.getAudioInputStream(bufferedIn);           
            } catch (Exception e){
            	JOptionPane.showMessageDialog(null, String.format("error opening file\n%s", filename));
                e.printStackTrace();
            }
            try{
            	clip = AudioSystem.getClip();
            	clip.open(audioStream);
            }catch(LineUnavailableException lue){
            	JOptionPane.showMessageDialog(null, "error: line not avaible");
            	lue.printStackTrace();
            }catch(IOException ioe){
            	JOptionPane.showMessageDialog(null, "ioe.printStackTrace()");
            	ioe.printStackTrace();
            }catch(Exception e){
            	JOptionPane.showMessageDialog(null, "error opening file");
            	e.printStackTrace();
            }
        }
        
        public void stopPlaying(){
        	clip.stop();
        	playing = false;
        }
        
        public void startPlaying(){
        	playing = true;
        	clip.loop(Clip.LOOP_CONTINUOUSLY);
        }
        
        public boolean isPlaying(){
        	return playing;
        }
        
    }
    thnks for all the friendly help. ^^

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: problem playing sound when running jar

    No you didn't. The information you found talks about an IOException while you were getting a NullPointerException. You made the problem go away by doing something else.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default Re: problem playing sound when running jar

    the nullpointer was thrown because the jar couldn't decide wether it was able to play the inputstream or not. (from what i can understand)
    that nullpointer was thrown as second exception. (because audioStream was null at that time, because the jvm wasn't able to determine if it was playable or not)

    I solved the first exception (java not beeing able to determine if it supported the file or not) and never got the second exception to be thrown again...
    don't see how that didn't solve my problem. O.o

  11. #11
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: problem playing sound when running jar

    Quote Originally Posted by liluma View Post
    the nullpointer was thrown because the jar couldn't decide wether it was able to play the inputstream or not. (from what i can understand)
    that nullpointer was thrown as second exception. (because audioStream was null at that time, because the jvm wasn't able to determine if it was playable or not)

    I solved the first exception (java not beeing able to determine if it supported the file or not) and never got the second exception to be thrown again...
    don't see how that didn't solve my problem. O.o
    You have solved your problem, but what gimbal2 is saying, is that you don't understand why your problem is solved. Your problem still exists. You still don't know why this is happening, so even though you aren't faced with your problem at the current time, the problem is still present.
    gimbal2 likes this.

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: problem playing sound when running jar

    Well at least there we are on the same page.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default Re: problem playing sound when running jar

    could you at least tell me, or give an indication as to where i would find the reason why it now is working/ wasn't working?
    Since I'm interested in knowing the why's, so i don't bump in to the same problems on later occasions.

    any help would be appreciated.

  14. #14
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: problem playing sound when running jar

    My only guess (I don't have experience with this kind of thing) is that you compiled your code into a JAR file after you made your method and before you added your sound file.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: problem playing sound when running jar

    (I don't have experience with this kind of thing
    Strange that you offer advice then.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: problem playing sound when running jar

    Quote Originally Posted by Norm View Post
    Strange that you offer advice then.
    Did you forget to quote the 'My only guess' part?

Similar Threads

  1. Playing Sound Effects in Java
    By CuppaCoffee in forum New To Java
    Replies: 3
    Last Post: 12-22-2012, 03:37 AM
  2. sound playing
    By nikkka in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-10-2011, 06:38 PM
  3. Re-playing sound
    By JSK in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-06-2008, 12:34 PM
  4. Playing sound in applet??????
    By Bagesh in forum Java Applets
    Replies: 2
    Last Post: 07-13-2007, 05:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •