0

I am recording some audio into a ByteArrayOutputStream with Java, but when I try to play that byte array I get a LineUnavailableException. This is my code that plays the audio:

public void playTone(ByteArrayOutputStream 
    try {
        AudioFormat format = new AudioFormat(44100, 8, 2, true, true);
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
        byte audio[] = note.toByteArray();
        InputStream input = new ByteArrayInputStream(audio);
        TargetDataLine line1 = (TargetDataLine) AudioSystem.getLine(info);
        ...
    } catch (LineUnavailableException e1) {
        e1.printStackTrace();
    }
}

This is the exception that I get:

javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 8 bit, stereo, 2 bytes/frame,  not supported.

Now I have read in this question that the parameters of AudioFormat might be wrong, but in that question the user used 24 (bit) as second parameter, where only 8 and 16 are supported.

Does anyone know what I am doing wrong?

EDIT:

From the answer below I changed the following since I do not have a file but a ByteArrayOutputStream:

AudioFormat format = new AudioFormat(44100, 16, 2, true, true);
byte audio[] = note.toByteArray();
InputStream input = new ByteArrayInputStream(audio);
audioStream = new AudioInputStream(input, format, audio.length / format.getFrameSize());
// instead of
// audioStream = AudioSystem.getAudioInputStream(soundFile);
Community
  • 1
  • 1
JeroenD
  • 343
  • 1
  • 6
  • 16

1 Answers1

0

You can clear your concept from this. I think this will helps u.

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.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class MakeSound {

    private final int BUFFER_SIZE = 128000;
    private File soundFile;
    private AudioInputStream audioStream;
    private AudioFormat audioFormat;
    private SourceDataLine sourceLine;

    /**
     * @param filename the name of the file that is going to be played
     */
    public void playSound(String filename){

        String strFilename = filename;

        try {
            soundFile = new File(strFilename);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        try {
            audioStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (Exception e){
            e.printStackTrace();
            System.exit(1);
        }

        audioFormat = audioStream.getFormat();

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        try {
            sourceLine = (SourceDataLine) AudioSystem.getLine(info);
            sourceLine.open(audioFormat);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        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();
    }
}
Jakir Hossen
  • 451
  • 4
  • 13
  • Thanks, I indeed had to change a bit since I do not have a file, but I was able to change that and it's working now. I will edit my first post with what I actually changed in your code, for if someone else needs it too. – JeroenD Apr 30 '14 at 16:58