javax.sound.sampled.LineUnavailableException试图录制声音

时间:2016-05-04 03:06:30

标签: java server voice-recognition javax.sound.sampled

我正在制作一个java语音聊天程序,服务器端语音类正在抛出此错误

javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 8000.0 Hz, 8 bit, mono, 1 bytes/frame,  not supported.
    at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:513)
    at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:121)
    at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:153)
    at client.VoiceUser.run(VoiceUser.java:34)

错误是在这行代码上抛出的

microphone.open(audioformat);

代码:

package client;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

public class VoiceUser extends Thread {
    private ObjectOutputStream clientOutput;
    private TargetDataLine microphone;
    private ArrayList<ObjectOutputStream> vOutputArray = new ArrayList<ObjectOutputStream>();
    private AudioFormat audioformat;

    public VoiceUser(Socket sv, ArrayList<ObjectOutputStream> outputArray) throws LineUnavailableException {
        try {
            clientOutput = new ObjectOutputStream(sv.getOutputStream());
            vOutputArray.equals(clientOutput);
            vOutputArray.add(clientOutput);
        } catch (IOException e) {
            System.out.println("Can't create stable connection between server and client");
        }
    }
    public void run() {
        try {
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioformat);
            microphone = (TargetDataLine)AudioSystem.getLine(info);
            audioformat = new AudioFormat(8000.0f,8,1,true,false);
            microphone.open(audioformat);
            microphone.start();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
        int bytesRead = 0;
        byte[] soundData = new byte[3072];
        int offset = 0;
        while(bytesRead != -1)
        {
            bytesRead = microphone.read(soundData, 0, soundData.length);
            if(bytesRead >= 0) {
                offset += bytesRead;
                if(offset == soundData.length) {
                    send(soundData, bytesRead);
                    offset = 0;
                }
            }
        }
    }
public void send(byte[] soundData, int bytesRead) {
    for(ObjectOutputStream o : vOutputArray) {
        try {
            o.write(soundData, 0, bytesRead);
            o.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }
}
}

1 个答案:

答案 0 :(得分:0)

它告诉你该行不可用。有几个原因 - 您的声卡不支持以您指定的特定格式录制。您可以尝试不同的格式,例如可能支持22050Hz。

另一个原因是,声卡已经忙于录音。在Windows上,您无法同时使用两个麦克风进行录制。您需要重新设计服务器以仅允许一次录制会话。