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 2 of 2

Thread: Playing .wav file

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Playing .wav file

    this is my code for playing a .wav file, but its not playing so please solve my query.
    Here is the code below
    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 SimpleAudioPlayer
    {
    	private static final int	EXTERNAL_BUFFER_SIZE = 128000;
     
     
     
    	public static void SoundPlayer(String filename)
    	{
     
    		String	strFilename = filename;
    		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);
    		}
     
    		/*
    		  From the AudioInputStream, i.e. from the sound file,
    		  we fetch information about the format of the
    		  audio data.
    		  These information include the sampling frequency,
    		  the number of
    		  channels and the size of the samples.
    		  These information
    		  are needed to ask Java Sound for a suitable output line
    		  for this audio file.
    		*/
    		AudioFormat	audioFormat = audioInputStream.getFormat();
     
    		/*
    		  Asking for a line is a rather tricky thing.
    		  We have to construct an Info object that specifies
    		  the desired properties for the line.
    		  First, we have to say which kind of line we want. The
    		  possibilities are: SourceDataLine (for playback), Clip
    		  (for repeated playback)	and TargetDataLine (for
    		  recording).
    		  Here, we want to do normal playback, so we ask for
    		  a SourceDataLine.
    		  Then, we have to pass an AudioFormat object, so that
    		  the Line knows which format the data passed to it
    		  will have.
    		  Furthermore, we can give Java Sound a hint about how
    		  big the internal buffer for the line should be. This
    		  isn't used here, signaling that we
    		  don't care about the exact size. Java Sound will use
    		  some default value for the buffer size.
    		*/
    		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 to
    			  receive audio data. We have to open the line.
    			*/
    			line.open(audioFormat);
    		}
    		catch (LineUnavailableException e)
    		{
    			e.printStackTrace();
    			System.exit(1);
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    			System.exit(1);
    		}
     
    		/*
    		  Still not enough. The line now can receive data,
    		  but will not pass them on to the audio output device
    		  (which means to your sound card). This has to be
    		  activated.
    		*/
    		line.start();
     
    		/*
    		  Ok, finally the line is prepared. Now comes the real
    		  job: we have to write data to the line. We do this
    		  in a loop. First, we read data from the
    		  AudioInputStream to a buffer. Then, we write from
    		  this buffer to the Line. This is done until the end
    		  of the file is reached, which is detected by a
    		  return value of -1 from the read method of the
    		  AudioInputStream.
    		*/
    		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.
    		  This is only necessary because of the bug noted below.
    		  (If we do not wait, we would interrupt the playback by
    		  prematurely closing the line and exiting the VM.)
     
    		  Thanks to Margie Fitch for bringing me on the right
    		  path to this solution.
    		*/
    		line.drain();
     
    		/*
    		  All data are played. We can close the shop.
    		*/
    		line.close();
     
    		/*
    		  There is a bug in the jdk1.3/1.4.
    		  It prevents correct termination of the VM.
    		  So we have to exit ourselves.
    		*/
    		//System.exit(0);
    	}
     
     
    	private static void printUsageAndExit()
    	{
    		out("SimpleAudioPlayer: usage:");
    		out("\tjava SimpleAudioPlayer <soundfile>");
    		System.exit(1);
    	}
     
     
    	private static void out(String strMessage)
    	{
    		System.out.println(strMessage);
    	}
    }
     
     
     
    /*** SimpleAudioPlayer.java ***/
    Last edited by helloworld922; March 27th, 2010 at 11:37 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Location
    Canada
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Playing .wav file

    I found an example of an audioinputstream, maybe you can mess with this to see whats wrong with yours!

    import java.io.File;
     
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.Line;
    import javax.sound.sampled.LineEvent;
    import javax.sound.sampled.LineListener;
    import javax.swing.JDialog;
    import javax.swing.JFileChooser;
     
    public class AudioTemplate extends Object implements LineListener {
      File soundFile;
     
      JDialog playingDialog;
     
      Clip clip;
     
      public static void main(String[] args) throws Exception {
        AudioTemplate s = new AudioTemplate();
      }
     
      public AudioTemplate() throws Exception {
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        soundFile = chooser.getSelectedFile();
     
        System.out.println("Playing " + soundFile.getName());
     
        Line.Info linfo = new Line.Info(Clip.class);
        Line line = AudioSystem.getLine(linfo);
        clip = (Clip) line;
        clip.addLineListener(this);
        AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
        clip.open(ais);
        clip.start();
      }
     
      public void update(LineEvent le) {
        LineEvent.Type type = le.getType();
        if (type == LineEvent.Type.OPEN) {
          System.out.println("OPEN");
        } else if (type == LineEvent.Type.CLOSE) {
          System.out.println("CLOSE");
          System.exit(0);
        } else if (type == LineEvent.Type.START) {
          System.out.println("START");
          playingDialog.setVisible(true);
        } else if (type == LineEvent.Type.STOP) {
          System.out.println("STOP");
          playingDialog.setVisible(false);
          clip.close();
        }
      }
    }

    Source: Play sound with AudioInputStream : WAV SoundDevelopmentJava Tutorial

Similar Threads

  1. Replies: 8
    Last Post: January 6th, 2010, 08:59 AM