Java - 播放录制的音频 - LineUnavailableException

时间:2014-04-30 16:33:34

标签: java audio

我正在使用Java将一些音频录制到ByteArrayOutputStream中,但是当我尝试播放该字节数组时,我得到一个LineUnavailableException。这是播放音频的代码:

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();
    }
}

这是我得到的例外:

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

现在我已在this问题中读到AudioFormat的参数可能有误,但在该问题中,用户使用24(位)作为第二个参数,其中仅支持8和16。

有谁知道我做错了什么?

编辑:

从下面的答案我改变了以下内容,因为我没有文件而是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);

1 个答案:

答案 0 :(得分:0)

您可以从中清除您的概念。我想这会有所帮助。

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();
    }
}