Reputation: 59
I am making a java voice chat program and the server side voice class is throwing this error
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)
the error is being thrown on this line of code
microphone.open(audioformat);
Code:
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();
}
}
}
}
Upvotes: 1
Views: 1774
Reputation: 25220
It tells you that line is not available. There are couple of reasons - your sound card does not support recording at that specific format you specified. You can try different formats, for example 22050Hz might be supported.
Another reason, the sound card is already busy with recording. On Windows you can not record with two microphones simultaneously. You need to redesign your server to allow just a single recording session.
Upvotes: 1