Results 1 to 7 of 7
  1. #1
    juhiswt is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default Play the wav file

    I tried to play a wav file.....but it is giving an exception noclassDefoundError here is the code..
    import java.io.IOException;
    import java.io.InputStream;

    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
    import javax.sound.sampled.UnsupportedAudioFileException;
    import javax.sound.sampled.DataLine.Info;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;

    public class PlayWaveFile
    {
    public static void main(String a[])
    {
    String wavfile=a[0];
    FileInputStream inputStream;
    try
    {
    inputStream=new FileInputStream(wavfile);
    }
    catch(FileNotFoundException e)
    {return;}
    PlaySound playSound=new PlaySound(inputStream);
    try{
    playSound.play();
    }
    catch(PlayWaveException e)
    {return;}
    }
    }
    class PlaySound {

    private InputStream waveStream;

    private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb

    /**
    * CONSTRUCTOR
    */
    public PlaySound(InputStream waveStream) {
    this.waveStream = waveStream;
    }

    public void play() throws PlayWaveException {

    AudioInputStream audioInputStream = null;
    try {
    audioInputStream = AudioSystem.getAudioInputStream(this.waveStream);
    } catch (UnsupportedAudioFileException e1) {
    throw new PlayWaveException(e1);
    } catch (IOException e1) {
    throw new PlayWaveException(e1);
    }

    // Obtain the information about the AudioInputStream
    AudioFormat audioFormat = audioInputStream.getFormat();
    Info info = new Info(SourceDataLine.class, audioFormat);

    // opens the audio channel
    SourceDataLine dataLine = null;
    try {
    dataLine = (SourceDataLine) AudioSystem.getLine(info);
    dataLine.open(audioFormat, this.EXTERNAL_BUFFER_SIZE);
    } catch (LineUnavailableException e1) {
    throw new PlayWaveException(e1);
    }

    // Starts the music :P
    dataLine.start();

    int readBytes = 0;
    byte[] audioBuffer = new byte[this.EXTERNAL_BUFFER_SIZE];

    try {
    while (readBytes != -1) {
    readBytes = audioInputStream.read(audioBuffer, 0,
    audioBuffer.length);
    if (readBytes >= 0){
    dataLine.write(audioBuffer, 0, readBytes);
    }
    }
    } catch (IOException e1) {
    throw new PlayWaveException(e1);
    } finally {
    // plays what's left and and closes the audioChannel
    dataLine.drain();
    dataLine.close();
    }

    }
    }
    class PlayWaveException extends Exception {

    public PlayWaveException(String message) {
    super(message);
    }

    public PlayWaveException(Throwable cause) {
    super(cause);
    }

    public PlayWaveException(String message, Throwable cause) {
    super(message, cause);
    }

    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please use code tags to maintain indentation and give more readability to your code.

  3. #3
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    I have used a simple code to play audio.

    Java Code:
    import sun.audio.*;
    
     class JavaAudioPlaySoundExample
    {
      public JavaAudioPlaySoundExample()
      {
        
     try{
        // open the sound file as a Java input stream
        String gongFile = "ujjal.wav";
        InputStream in = new FileInputStream(gongFile);
    
        // create an audiostream from the inputstream
        AudioStream audioStream = new AudioStream(in);
    
        // play the audio clip with the audioplayer class
        AudioPlayer.player.start(audioStream);
        }
      catch(Exception e){}
      }
    }

  4. #4
    juhiswt is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    @Ujjal Dhar
    I have neve used sun.package;what is this? And it is giving the warning that it may be removed in future release.

  5. #5
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    FAQ - Sun Packages

    Please read this before using sun.packages.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Use the static Applet method newAudioClip() to obtain an AudioClip instance. This AudioClip has a play() method.

    From that FAQ: "In general, writing java programs that rely on sun.* is risky: they are not portable, and are not supported." I don't reccommend using such methods.

  7. #7
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    From that FAQ: "In general, writing java programs that rely on sun.* is risky: they are not portable, and are not supported." I don't reccommend using such methods.
    Same from me....


    Use the static Applet method newAudioClip() to obtain an AudioClip instance. This AudioClip has a play() method.
    It is good.

Similar Threads

  1. play a wav file repeatedly
    By oontvoo in forum New To Java
    Replies: 6
    Last Post: 11-28-2010, 02:15 AM
  2. help to play file.wav in java
    By jperson in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 04:10 AM
  3. Play avi file in JMF
    By ramkumarm in forum AWT / Swing
    Replies: 0
    Last Post: 04-15-2009, 02:49 PM
  4. play vedio file and flash file in swing
    By rajasekharreddym in forum AWT / Swing
    Replies: 1
    Last Post: 07-30-2008, 09:22 PM
  5. how can I play an mp3 file in java?
    By iliana in forum Networking
    Replies: 1
    Last Post: 05-30-2008, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •