2

I have a very simple class that can play a sound file with the following code:

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Sound{

private Clip sound;

public Sound(String location){

    try{ 
        sound = AudioSystem.getClip();
        File file = new File(location);
        sound.open(AudioSystem.getAudioInputStream(file));
    }
    catch(IOException | LineUnavailableException | UnsupportedAudioFileException error){
        System.out.println(error);
    }

}

public void play(){

    sound.start();

}

}

However, when I create an instance of this class and call the play function on it I don't get any sound. I hear a pop when the sound starts and when it ends but not the actual file. Also I don't get any errors of any sort.

What am I doing wrong?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Ood
  • 1,445
  • 4
  • 23
  • 43
  • Which is the file format? Does it uses a codec? – Mansueli Jun 19 '14 at 22:35
  • No, It's a normal .WAV file and I have tried different ones. – Ood Jun 19 '14 at 22:36
  • You have to wait for the clip to finish, see [this question](https://stackoverflow.com/questions/557903/how-can-i-wait-for-a-java-sound-clip-to-finish-playing-back) – greg-449 Jun 20 '14 at 10:48
  • This answer did not do the trick for me. I know the clip is starting and it ends because there is a quiet *pop* noise, but I can't hear the actual file. – Ood Jun 20 '14 at 11:51
  • why don't you use javax.media package? – lakshman Jun 24 '14 at 11:04
  • have a look at this link which may help you - http://stackoverflow.com/questions/2416935/how-to-play-wav-files-with-java – BSrinivas Jun 25 '14 at 01:52

3 Answers3

1

Use this and note this is not my code it's from here: How to play .wav files with java The only thing I did was post it here and optimize it a little.

private final int BUFFER_SIZE = 128000;
private AudioInputStream audioStream;
private SourceDataLine sourceLine;
/**
 * @param filename the name of the file that is going to be played
 */
 public void playSound(String filename){
try {
    audioStream = AudioSystem.getAudioInputStream(new File(filename));
} catch (Exception e){
    e.printStackTrace();
}
try {
    sourceLine = (SourceDataLine) AudioSystem.getLine(new DataLine.Info(SourceDataLine.class, audioStream.getFormat()));
    sourceLine.open(audioStream.getFormat());
} catch (LineUnavailableException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
sourceLine.start();
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
    try {
        nBytesRead = audioStream.read(abData, 0, abData.length);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (nBytesRead >= 0) {
        @SuppressWarnings("unused")
        int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
    }
}
sourceLine.drain();
sourceLine.close();
}

I hope this helps.

Community
  • 1
  • 1
Roan
  • 1,200
  • 2
  • 19
  • 32
0

Try something like:

File soundFile = new File( "something.wav" );
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream( soundFile );
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();//This plays the audio

You might have to use AudioSystem.getClip() after loading the audio stream.

famousgarkin
  • 13,687
  • 5
  • 58
  • 74
0

It has been my experience that the audio file one is using the frequent culprit. Java, apparently, cannot play compressed sound files or something like that. It plays linear PCM files only. I could be wrong about the only. Anyone have an example that plays any type of sound file?

Richard
  • 1
  • 1