July 7th, 2008

More Java Sounds

Here’s an update to my last post:  My cousin, Dave.  (Way to go, Dave!) submitted this example of another (simpler) way to play sound files:

import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
public class SimpleSoundPlayer2 {
public static void main( String[] args )
    {
        // The audio file to play
        String audioFile = "/1-welcome.wav";
        // Check that the resource is available
        URL url = SimpleSoundPlayer2.class.getResource(audioFile);
        if( url == null ) {
            System.out.println("Resource not found.");
            System.exit(1);
        }
        try {
            playSound(url);
        } catch( IOException e ) {
            System.err.println("IOException occured: " + e );
            e.printStackTrace();
            System.exit(1);
        }
    }
    public static void playSound( URL url ) throws IOException {
        AudioInputStream audioStream = null;
        try {
            audioStream = AudioSystem.getAudioInputStream( url );
        } catch( UnsupportedAudioFileException e ) {
            System.err.println("The audio file is not a recognized format.");
            return;
        }
        // Print out some info about the sound
        System.out.printf("URL: %s \nFormat: %s\n", url.toString(),
            audioStream.getFormat().toString() );
        try {
            // Create the clip and open the stream
            Clip clip = AudioSystem.getClip();
            clip.open(audioStream);
            // Start playing the sound
            clip.start();
            // Wait until the sound finishes playing
            while (clip.isActive()) {
                try { Thread.sleep(99); } catch (Exception e) {break;}
            }
            // Close the clip
            clip.close();
        } catch (LineUnavailableException e ) {
            System.err.println("Unable to create Clip: " + e);
            e.printStackTrace();
            System.exit(1);
        }
    }

}

I believe the same rules apply here as far as where you put for resource files and the pathing used to find them.

July 5th, 2008

Loading Resources From Jar Files In Java

Okay, this is a frustrating one.  You make a cool java app with sounds and icons and then you go to deploy it, but it doesn’t work on your friends computer.   Well if you are a newbie, like me, you might not realize that you need to deploy those sound/image files with your program.   Sounds pretty straight forward, right?   Well, it is if you know the directory where your friend will be running the program from and where you can access the files.   How about putting all those nice resources in a jar file and deploying that with your program instead.  That’s actually the easy part, but then how do you get them out to use at run time.   There are a few articles around on this, but none seemed to tell me enough about how to do this.  I finally got it working, so I figured I’d share it for the rest of us java greenhorns…

First I’ve copied and modified a program to play wave files (I just googled for it since I’m not smart enough to write this stuff from scratch).   You can find this code at:

anyexample.com

The important part of the code is here:

public void run() {
/*File soundFile = new File(filename);
if (!soundFile.exists()) {
  System.err.println("Wave file not found: " + filename);
  return;
}
*/
URL url = getClass().getResource(filename);
if (url != null) {
//System.out.println("sound file = " + filename);
AudioInputStream audioInputStream = null;
try {
    audioInputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(filename));
    // audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch (UnsupportedAudioFileException e1) {
   e1.printStackTrace();
   return;
}
catch (IOException e1) {
  e1.printStackTrace();
  return;
}

Notice, I’ve changed from using a File to a URL.  The variable filename is just a string which is declared earlier.  The important (and not immediately obvious) part is… What do I use for the filename?    Here’s where it gets interesting:

I’ve created a package called  joetest.resources  and put my sound files in there.   I’m using netbeans, so here’s a screen shot of what it looks like:

packages

I’ve dropped some sound files in the joetest.resources folder. I picked these up from Soundsnap which is a cool site for free sound files.
Now comes the part that gave me fits trying to get to work. Let’s say I wanted to play the blaster.wav file. In order to access it, here’s what I assign to the filename variable:

filename = “/joetest/resources/blaster.wav”;

Now that that is done, this is what my Main.java looks like:

package joestest.testprograms;
public class Main {
   public static void main(String[] args) {
       new joestest.tools.WavePlayer("/joestest/resources/blaster.wav").start(); 
   }
}

That’s all there is to it!    Okay, now that I know how to do this it seems simple, but when you are starting out in java this can be a real pain.

Incidentally,  this is very similar to how you would get icons for buttons etc.  Here’s an example:

btnMyButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/joetest/resources/buttonicon.gif"))); 

April 6th, 2008

OS X Leopard

Well, I did it. Last year after being a long time windows user, I made the jump to OS X. I was not too impressed with Vista and wanted to try something new. I was very impressed with the mac from the beginning and thanks to tools like vmware fusion I was able to get all the windows functionality I needed. Still it was hard to give up my pc habit. I still have my windows box sitting side by side with my mac, just in case. Occasionally there are some games that I want to play that the mac won’t support. That and some of my work related programs I need don’t have mac counter parts and sometimes it takes to long to boot into my vm, so I just slide my chair over and use the pc for a bit.

Okay, enough about that. This weekend, I finally gave in and upgraded from OS X 10.4 (Tiger) to OS X 10.5 (Leopard). So far, I like what I see. It’s very slick looking.  Here are some thoughts:

 

  • Installation:  Super easy.  Just took a while.  No issues to speak of.
  • Eye Candy:  Very nice presentation.   It looks very sharp
  • Stacks :  Stacks are pretty handy and make getting organized very easy.
  • VNC:  This was probably the coolest feature for me (geek thing).  Being able to remote into my windows or linux box using vnc is very very nice.  I used Chicken of the VNC before and it got me by, but this is much more responsive and usable.
  • Spaces:  A trick stolen from linux, but it is really nice to have 4 desktops to organize your stuff into
  • Finder:  Much improved.  The interface is nicer and allowing me to see shared computers is a much needed improvement
  • Quick Look:  Another very nifty feature.   You can quickly browse through your documents in a folder and get quick glimpses of them (and pictures) without having to open the whole thing.  
All in all, it’s been a pretty positive experience so far.   I’ll blog some more and this as I figure out more things.
Night!

April 4th, 2008

Cruising the Caribbean

I just got back from a seven day cruise in the caribbean.    It was quite a bit of fun (I got married at sea which seems pretty fitting for an old sailor).    Cruises are an interesting way to travel,  and actually an excellent vacation if you plan on taking the kids.   My only real complaint is that you don’t have much time in any one port, so skip the tours and look around.   It’s a great way to sample a location and see if you want to come back later and spend some time (and then do the tours).   Also, you have to be prepared to relax.  When you are confined to the ship during  your transiting time at sea, it’s a great time to hang out, talk, read, and sample a few cocktails.

Here were our stops:

  1. Jamaica (Ocho Rios) :  Very seedy.   I was offered weed before I was even off the pier.   Maybe there are better locations in Jamaica, but this was not one I’d recommend.   I did have a cheeseburger in paradise, but it was not as good as the ones I get from Main Street Burger in Meridan, Idaho.
  2. Grand Cayman : Crap!  This port was cancelled due to choppy seas.   The port closed and we spent another day at sea.  Important safety tip.   Be very careful if you are planning a wedding on shore.  There’s no guarantee the ship will stop there.  (Luckily, we got married at sea)
  3. Cozumel:  Another mistake here.   I think this could have been a fun port, but we went to the Mayan ruins at Tulum which were very cool (Except that my son was hot and complaining and my wife was feeling sick so I think I was the only on that enjoyed it).  The mistake was that this trip shot the day and by the time we got back we were too tired and had very little time to do much shopping.  So, I say again.  Forget the tours.
  4. Princess Cays:  This was just a private beach owned by princess cruises that we got to hang out at for the day.   It was a pretty nice day at the beach since only the people from our ship were there.   Also, they had a daycare facility on the beach so we could even swim a little on our own.  All in all it was fun, but nothing else to do if you don’t like beach activities.

Overall:  Definitey a fun and relaxing trip!  The cruise staff were awesome and the ship was very nice.  I enjoyed it, but if you don’t like crowds then make sure you get a room with a balcony so you can have your own private deck to hang out on.

On shore in the Princess Cays