簡體   English   中英

Java-播放錄制的音頻-LineUnavailableException

[英]Java - Playing recorded audio - LineUnavailableException

我正在用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.

現在,我已經在問題中閱讀到,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);

您可以從中清除您的概念。 我認為這對您有幫助。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM