Link to home
Start Free TrialLog in
Avatar of Drop_of_Rain
Drop_of_Rain

asked on

Look over modified audio class

Hello objects and CEHJ.  I modified this class to get rid of the command line arg. It compiles and excepts a String strFilename , would you look it over to see if i got it right.  How will I call the class from another?

Do i need to worry about this in the class?
                  e.printStackTrace();
                  System.exit(1);



import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;



public class AudioPlayer
{
      private static final int      EXTERNAL_BUFFER_SIZE = 128000;
        private static String strFilename;


      public AudioPlayer (String strFilename)
      {

            File      soundFile = new File(strFilename);
                  //  We have to read in the sound file.
            AudioInputStream      audioInputStream = null;
            try
            {
                  audioInputStream = AudioSystem.getAudioInputStream(soundFile);
            }
            catch (Exception e)
            {
                  /*
                    In case of an exception, we dump the exception
                    including the stack trace to the console output.
                    Then, we exit the program.
                  */
                  e.printStackTrace();
                  System.exit(1);
            }

            AudioFormat      audioFormat = audioInputStream.getFormat();
                      //    Asking for a SourceDataLine (for playback)
              SourceDataLine      line = null;
            DataLine.Info      info = new DataLine.Info(SourceDataLine.class, audioFormat);
            try
            {
                  line = (SourceDataLine) AudioSystem.getLine(info);

                     //    The line is there, but it is not yet ready
                  line.open(audioFormat);
            }
            catch (LineUnavailableException e)
            {
                  e.printStackTrace();
                  System.exit(1);
            }
            catch (Exception e)
            {
                  e.printStackTrace();
                  System.exit(1);
            }

                   //get audio output to device activated
             
            line.start();
            // to write data to the line. We do this
            int      nBytesRead = 0;
            byte[]      abData = new byte[EXTERNAL_BUFFER_SIZE];
            while (nBytesRead != -1)
            {
                  try
                  {
                        nBytesRead = audioInputStream.read(abData, 0, abData.length);
                  }
                  catch (IOException e)
                  {
                        e.printStackTrace();
                  }
                  if (nBytesRead >= 0)
                  {
                        int      nBytesWritten = line.write(abData, 0, nBytesRead);
                  }
            }

            
              //Wait until all data are played.
            line.drain();
              //All data are played. We can close the shop.
            line.close();
                //There is a bug in the jdk1.3/1.4. we exit

            System.exit(0);
        }


}





SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Drop_of_Rain
Drop_of_Rain

ASKER

So how will i call this from another class? I seem to have trouble getting this part, just be patient with me.