Play a .wav file

Reply

Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Play a .wav file

 
0
  #1
Jan 25th, 2005
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?

  1. import java.io.File;
  2. import java.io.IOException;
  3.  
  4. import javax.sound.sampled.AudioFormat;
  5. import javax.sound.sampled.AudioInputStream;
  6. import javax.sound.sampled.AudioSystem;
  7. import javax.sound.sampled.DataLine;
  8. import javax.sound.sampled.FloatControl;
  9. import javax.sound.sampled.LineUnavailableException;
  10. import javax.sound.sampled.SourceDataLine;
  11.  
  12. public class Play {
  13. /** Plays audio from given file names. */
  14. public static void main( String [] args ) {
  15. // Check for given sound file names.
  16. if (args.length < 1) {
  17. System.out.println( "Play usage:" );
  18. System.out.println( "\tjava Play <sound file names>*" );
  19. System.exit( 0 );
  20. }
  21.  
  22. // Process arguments.
  23. for (int i = 0; i < args.length; i++ )
  24. playAudioFile( args[ i ] );
  25.  
  26. // Must exit explicitly since audio creates non-daemon threads.
  27. System.exit( 0 );
  28. } // main
  29.  
  30. public static void playAudioFile( String fileName ) {
  31. File soundFile = new File( fileName );
  32.  
  33. try {
  34. // Create a stream from the given file.
  35. // Throws IOException or UnsupportedAudioFileException
  36. AudioInputStream audioInputStream = AudioSystem.getAudioInputStream( soundFile );
  37. // AudioSystem.getAudioInputStream( inputStream ); // alternate audio stream from inputstream
  38. playAudioStream( audioInputStream );
  39. } catch ( Exception e ) {
  40. System.out.println( "Problem with file " + fileName + ":" );
  41. e.printStackTrace();
  42. }
  43. } // playAudioFile
  44.  
  45. /** Plays audio from the given audio input stream. */
  46. public static void playAudioStream( AudioInputStream audioInputStream ) {
  47. // Audio format provides information like sample rate, size, channels.
  48. AudioFormat audioFormat = audioInputStream.getFormat();
  49. System.out.println( "Play input audio format=" + audioFormat );
  50.  
  51. // Open a data line to play our type of sampled audio.
  52. // Use SourceDataLine for play and TargetDataLine for record.
  53. DataLine.Info info = new DataLine.Info( SourceDataLine.class, audioFormat );
  54. if ( !AudioSystem.isLineSupported( info ) ) {
  55. System.out.println( "Play.playAudioStream does not handle this type of audio on this system." );
  56. return;
  57. }
  58.  
  59. try {
  60. // Create a SourceDataLine for play back (throws LineUnavailableException).
  61. SourceDataLine dataLine = (SourceDataLine) AudioSystem.getLine( info );
  62. // System.out.println( "SourceDataLine class=" + dataLine.getClass() );
  63.  
  64. // The line acquires system resources (throws LineAvailableException).
  65. dataLine.open( audioFormat );
  66.  
  67. // Adjust the volume on the output line.
  68. if( dataLine.isControlSupported( FloatControl.Type.MASTER_GAIN ) ) {
  69. FloatControl volume = (FloatControl) dataLine.getControl( FloatControl.Type.MASTER_GAIN );
  70. volume.setValue( 100.0F );
  71. }
  72.  
  73. // Allows the line to move data in and out to a port.
  74. dataLine.start();
  75.  
  76. // Create a buffer for moving data from the audio stream to the line.
  77. int bufferSize = (int) audioFormat.getSampleRate() * audioFormat.getFrameSize();
  78. byte [] buffer = new byte[ bufferSize ];
  79.  
  80. // Move the data until done or there is an error.
  81. try {
  82. int bytesRead = 0;
  83. while ( bytesRead >= 0 ) {
  84. bytesRead = audioInputStream.read( buffer, 0, buffer.length );
  85. if ( bytesRead >= 0 ) {
  86. // System.out.println( "Play.playAudioStream bytes read=" + bytesRead +
  87. // ", frame size=" + audioFormat.getFrameSize() + ", frames read=" + bytesRead / audioFormat.getFrameSize() );
  88. // Odd sized sounds throw an exception if we don't write the same amount.
  89. int framesWritten = dataLine.write( buffer, 0, bytesRead );
  90. }
  91. } // while
  92. } catch ( IOException e ) {
  93. e.printStackTrace();
  94. }
  95.  
  96. System.out.println( "Play.playAudioStream draining line." );
  97. // Continues data line I/O until its buffer is drained.
  98. dataLine.drain();
  99.  
  100. System.out.println( "Play.playAudioStream closing line." );
  101. // Closes the data line, freeing any resources such as the audio device.
  102. dataLine.close();
  103. } catch ( LineUnavailableException e ) {
  104. e.printStackTrace();
  105. }
  106. } // playAudioStream
  107. } // 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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: Play a .wav file

 
0
  #2
Jan 25th, 2005
Hello everyone,


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

  1. import java.io.File;
  2.  
  3. import javax.sound.sampled.AudioFormat;
  4. import javax.sound.sampled.AudioInputStream;
  5. import javax.sound.sampled.AudioSystem;
  6. import javax.sound.sampled.Clip;
  7. import javax.sound.sampled.DataLine;
  8. public class Sampled
  9. {
  10. public static void main(String[] args)
  11. {
  12. try{
  13. AudioInputStream stream = AudioSystem.getAudioInputStream(new File("c:\\temp\\sample.wav"));
  14. AudioFormat format = stream.getFormat();
  15. DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());
  16. Clip clip = (Clip) AudioSystem.getLine(info);
  17. clip.open(stream);
  18. clip.start();
  19. } catch (Exception e)
  20. {
  21. e.printStackTrace();
  22. }
  23. }
  24. }

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

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

Can anyone help?


regards,
George
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Play a .wav file

 
0
  #3
Jan 28th, 2005
looks like the class doesn't support the audio type you're loading.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: Play a .wav file

 
0
  #4
Jan 30th, 2005
Thanks Phaelax,


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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: Play a .wav file

 
0
  #5
Feb 1st, 2005
Thanks for all the people who helped me on this thread.


regards,
George
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1
Reputation: BestAim is an unknown quantity at this point 
Solved Threads: 0
BestAim BestAim is offline Offline
Newbie Poster

Re: Play a .wav file

 
0
  #6
Aug 5th, 2007
no, we're not going to do that.
Last edited by jwenting; Aug 5th, 2007 at 1:57 pm. Reason: we're not going to do your homework for you.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 33
Reputation: AlbertPi is an unknown quantity at this point 
Solved Threads: 2
AlbertPi AlbertPi is offline Offline
Light Poster

Re: Play a .wav file

 
0
  #7
Nov 19th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Play a .wav file

 
0
  #8
Nov 20th, 2008
Originally Posted by AlbertPi View Post
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"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC