944,028 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 61332
  • Java RSS
Jan 25th, 2005
0

Play a .wav file

Expand Post »
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?

Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Jan 25th, 2005
0

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.

Java Syntax (Toggle Plain Text)
  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,

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Jan 28th, 2005
0

Re: Play a .wav file

looks like the class doesn't support the audio type you're loading.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jan 30th, 2005
0

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
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Feb 1st, 2005
0

Re: Play a .wav file

Thanks for all the people who helped me on this thread.


regards,
George
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Aug 5th, 2007
0

Re: Play a .wav file

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BestAim is offline Offline
1 posts
since Aug 2007
Nov 19th, 2008
0

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
Reputation Points: 9
Solved Threads: 2
Light Poster
AlbertPi is offline Offline
33 posts
since Nov 2008
Nov 20th, 2008
1

Re: Play a .wav file

Click to Expand / Collapse  Quote originally posted by AlbertPi ...
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"
Reputation Points: 935
Solved Threads: 356
Posting Maven
stultuske is online now Online
2,520 posts
since Jan 2007
May 16th, 2011
-1
Re: Play a .wav file
Big thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gejemur is offline Offline
1 posts
since May 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: GUI Window - Exit Button?
Next Thread in Java Forum Timeline: IDE for Java?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC