Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: More getResource and Eclipse question

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default More getResource and Eclipse question

    This is kind of an extension from a previous post that I did. The following code I got from KevinWorkman and it deals with loading a wav file as a resource. I took Kevin's code and created a stand alone program to see if it works. I can get it to compile and run without errors but no sound plays. I'm using Eclipse IDE.

    I've imported the wav file into the src directory which contains the class that calls the resource. I've also imported it into the root directory of the project itself. I've used a forward slash at the beginning of the file name. I also tried to just load it as a File instead of using getClass().getResource("boxing_bell.wav"); I can't get it to play.

    I'm at a complete loss.

    Here's the code:

    public class playClip {
     
    	private Clip music;
     
    	private void startSong(){
    			try{
    			    System.out.println("Starting...\n");
    				AudioInputStream stream = AudioSystem.getAudioInputStream(
    						getClass().getResource("boxing_bell.wav"));
    //				AudioInputStream stream = AudioSystem.getAudioInputStream(new File("boxing_bell.wav"));
    				music = AudioSystem.getClip();
    				music.open(stream);
    				music.start();
    				System.out.println("Done...\n");
    			}
    			catch(Exception e){
    				e.printStackTrace();
    				music = null;
    			}
     
    		}
     
    	public static void main(String args[])
    	{
    		playClip play = new playClip();
     
    		play.startSong();
    	}
     
    }

    I also tried making a resource directory within the src directory and including that in the build path.

    Any ideas of what I'm doing wrong? I think it has more to do with Eclipse than the code. The reason I say this is I found a book on Safari Online that is almost identical to the code above.

    Can you say "Clueless!"

    Thanks

    Curt


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: More getResource and Eclipse question

    Going over your other thread, you cannot use

    AudioInputStream stream = AudioSystem.getAudioInputStream(getClass().getResource("boxing_bell.wav"));

    getClass().getResource() is used for getting the wav file from within a jar.

    Because you have written and compiled this within Eclipse, you need to put the .wav file in the projects root directory and use:

    AudioInputStream stream = AudioSystem.getAudioInputStream(new File("boxing_bell.wav"));

    Hopefully that should work.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: More getResource and Eclipse question

    If you are still having issues, try this code. I've used it in the past and know it works.

    import java.io.File;
    import java.io.IOException;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineEvent;
    import javax.sound.sampled.LineEvent.Type;
    import javax.sound.sampled.LineListener;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.UnsupportedAudioFileException;
     
    public class PlayAudio {
     
    	private static void playClip(File clipFile) throws IOException, 
    	  UnsupportedAudioFileException, LineUnavailableException, InterruptedException {
    	  class AudioListener implements LineListener {
    	    private boolean done = false;
    	    @Override public synchronized void update(LineEvent event) {
    	      Type eventType = event.getType();
    	      if (eventType == Type.STOP || eventType == Type.CLOSE) {
    	        done = true;
    	        notifyAll();
    	      }
    	    }
    	    public synchronized void waitUntilDone() throws InterruptedException {
    	      while (!done) { wait(); }
    	    }
    	  }
    	  AudioListener listener = new AudioListener();
    	  AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(clipFile);
    	  try {
    	    Clip clip = AudioSystem.getClip();
    	    clip.addLineListener(listener);
    	    clip.open(audioInputStream);
    	    try {
    	      clip.start();
    	      listener.waitUntilDone();
    	    } finally {
    	      clip.close();
    	    }
    	  } finally {
    	    audioInputStream.close();
    	  }
    	}
     
    	public static void main(String[] args) throws IOException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException{
    		File wav = new File("test.wav"); 
    		playClip(wav);
     
    	}
     
    }

    Make sure the wav file is in the correct location. In this case it's in the project root.

    May also be worth looking at - Incredibly Easy Way To Play Sounds
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: More getResource and Eclipse question

    Quote Originally Posted by JavaPF View Post

    Because you have written and compiled this within Eclipse, you need to put the .wav file in the projects root directory and use:

    AudioInputStream stream = AudioSystem.getAudioInputStream(new File("boxing_bell.wav"));

    Hopefully that should work.
    JavaPF,

    Thanks for the reply. I tried the above, again, but to no avail. I had it commented out to show that I had tried it, but it still didn't work. I thought it was something I was/wasn't doing in Eclipse but I compiled the code on the command line and it still didn't work, so I'm not sure what is going on.

    As far as your 2nd reply, I put the code in and it worked. Thanks for that.

    The whole point of this was the code I have is very long and tedious. The code I got from KevinWorkman was very short and compact and much easier to understand.

    Thanks for all the help.

  5. #5
    Banned
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: More getResource and Eclipse question

    To get resources you need to go and get a shovel. You can dig down and collect the rocks and stuff. Thats what i do in minecraft. Thank me if i helped

Similar Threads

  1. Replies: 24
    Last Post: August 4th, 2014, 12:49 PM
  2. Help with Eclipse
    By Graham Montgomery in forum Java IDEs
    Replies: 1
    Last Post: April 29th, 2011, 09:07 AM
  3. Eclipse goes beyond me a bit
    By MagicMojo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 9th, 2011, 10:05 AM
  4. Eclipse
    By nasi in forum Java IDEs
    Replies: 3
    Last Post: May 6th, 2010, 01:35 PM
  5. Eclipse on Mac
    By Cuju in forum Java IDEs
    Replies: 6
    Last Post: March 19th, 2010, 09:29 PM