DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Play a .wav file (http://www.daniweb.com/forums/thread17484.html)

George2 Jan 25th, 2005 6:07 am
Play a .wav file
 
Hello everyone,


When using the following source codes to play a local .wav file, I always find .wav files can not played. Does anyone know what is the trouble?

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.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
 
public class Play {
  /** Plays audio from given file names. */
  public static void main( String [] args ) {
      // Check for given sound file names.
      if (args.length < 1) {
        System.out.println( "Play usage:" );
        System.out.println( "\tjava Play <sound file names>*" );
        System.exit( 0 );
      }
 
      // Process arguments.
      for (int i = 0; i < args.length; i++ )
        playAudioFile( args[ i ] );
 
      // Must exit explicitly since audio creates non-daemon threads.
      System.exit( 0 );
  } // main
 
  public static void playAudioFile( String fileName ) {
      File soundFile = new File( fileName );
 
      try {
        // Create a stream from the given file.
        // Throws IOException or UnsupportedAudioFileException
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream( soundFile );
        // AudioSystem.getAudioInputStream( inputStream ); // alternate audio stream from inputstream
        playAudioStream( audioInputStream );
      } catch ( Exception e ) {
        System.out.println( "Problem with file " + fileName + ":" );
        e.printStackTrace();
      }
  } // playAudioFile
 
  /** Plays audio from the given audio input stream. */
  public static void playAudioStream( AudioInputStream audioInputStream ) {
      // Audio format provides information like sample rate, size, channels.
      AudioFormat audioFormat = audioInputStream.getFormat();
      System.out.println( "Play input audio format=" + audioFormat );
 
      // Open a data line to play our type of sampled audio.
      // Use SourceDataLine for play and TargetDataLine for record.
      DataLine.Info info = new DataLine.Info( SourceDataLine.class, audioFormat );
      if ( !AudioSystem.isLineSupported( info ) ) {
        System.out.println( "Play.playAudioStream does not handle this type of audio on this system." );
        return;
      }
 
      try {
        // Create a SourceDataLine for play back (throws LineUnavailableException). 
        SourceDataLine dataLine = (SourceDataLine) AudioSystem.getLine( info );
        // System.out.println( "SourceDataLine class=" + dataLine.getClass() );
 
        // The line acquires system resources (throws LineAvailableException).
        dataLine.open( audioFormat );
 
        // Adjust the volume on the output line.
        if( dataLine.isControlSupported( FloatControl.Type.MASTER_GAIN ) ) {
            FloatControl volume = (FloatControl) dataLine.getControl( FloatControl.Type.MASTER_GAIN );
            volume.setValue( 100.0F );
        }
 
        // Allows the line to move data in and out to a port.
        dataLine.start();
 
        // Create a buffer for moving data from the audio stream to the line. 
        int bufferSize = (int) audioFormat.getSampleRate() * audioFormat.getFrameSize();
        byte [] buffer = new byte[ bufferSize ];
 
        // Move the data until done or there is an error.
        try {
            int bytesRead = 0;
            while ( bytesRead >= 0 ) {
              bytesRead = audioInputStream.read( buffer, 0, buffer.length );
              if ( bytesRead >= 0 ) {
                  // System.out.println( "Play.playAudioStream bytes read=" + bytesRead +
                  //    ", frame size=" + audioFormat.getFrameSize() + ", frames read=" + bytesRead / audioFormat.getFrameSize() );
                  // Odd sized sounds throw an exception if we don't write the same amount.
                  int framesWritten = dataLine.write( buffer, 0, bytesRead );
              }
            } // while
        } catch ( IOException e ) {
            e.printStackTrace();
        }
 
        System.out.println( "Play.playAudioStream draining line." );
        // Continues data line I/O until its buffer is drained.
        dataLine.drain();
 
        System.out.println( "Play.playAudioStream closing line." );
        // Closes the data line, freeing any resources such as the audio device.
        dataLine.close();
      } catch ( LineUnavailableException e ) {
        e.printStackTrace();
      }
  } // playAudioStream
} // Play

When using the above program to play a local .wav file, errors such as the following messages are thrown,

Play input audio format=PCM_UNSIGNED, 11025.0 Hz, 8 bit, mono, audio data
Play.playAudioStream does not handle this type of audio on this system.

I have changed many .wav files, but the message "Play.playAudioStream does not handle this type of audio on this system." is always printed and no sound is heard.


Thanks in advance,
George

George2 Jan 25th, 2005 11:58 pm
Re: Play a .wav file
 
Hello everyone,


I have made a relatively simple sample to illustrate my issue. My previous sample is relatively complicated. Sorry for inconvenience.

import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
public class Sampled

        public static void main(String[] args)
        {   
        try{     
                AudioInputStream stream        = AudioSystem.getAudioInputStream(new File("c:\\temp\\sample.wav"));     
                AudioFormat format = stream.getFormat();     
                DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());     
                Clip clip = (Clip) AudioSystem.getLine(info);       
                clip.open(stream);     
                clip.start();
        } catch (Exception e)
        {     
                e.printStackTrace();   
        } 
}
}

When run the above sample, the following exceptions will be thrown,

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
        at javax.sound.sampled.AudioSystem.getAudioInputStream        at Sampled.main

Can anyone help?


regards,
George

Phaelax Jan 28th, 2005 5:38 am
Re: Play a .wav file
 
looks like the class doesn't support the audio type you're loading.

George2 Jan 30th, 2005 11:00 pm
Re: Play a .wav file
 
Thanks Phaelax,


Quote:

Originally Posted by Phaelax
looks like the class doesn't support the audio type you're loading.

Your reply is very helpful. I am wondering how to detect what audio types are supported. Can you help?


regards,
George

George2 Feb 1st, 2005 11:51 pm
Re: Play a .wav file
 
Thanks for all the people who helped me on this thread.


regards,
George

BestAim Aug 5th, 2007 12:52 pm
Re: Play a .wav file
 
no, we're not going to do that.

AlbertPi Nov 19th, 2008 11:54 am
Re: Play a .wav file
 
George,
I got the same error as you had iy before. How did you solve this issue ? Can share your solution with us ?


Thanks,
Albert

stultuske Nov 20th, 2008 6:12 am
Re: Play a .wav file
 
Quote:

Originally Posted by AlbertPi (Post 739707)
George,
I got the same error as you had iy before. How did you solve this issue ? Can share your solution with us ?


Thanks,
Albert

Albert, deer lad...
this post is ancient, in dog-years, it would have to go to school already.
playing wav files is supported by Java without having to implement, load, ... external jars.

I once wrote a wav player when I was hungover, and it took me about 3 minutes. no doubt the sober you can do it as well. just google on "Java" and "audio" or "Java" and "Wav"


All times are GMT -4. The time now is 1:40 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC