I am trying to make a voice server and the server is throwing this error "javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 8000.0 Hz, 8 bit, mono, 1 bytes/frame, not supported.". Here is my code, Thank you in advance
public class VoiceUser extends Thread { // CHAT USER
private ObjectOutputStream clientOutput;
public VoiceUser(Socket sv) {
try {
System.out.println("VSTART");
clientOutput = new ObjectOutputStream(sv.getOutputStream());
outputArray.add(clientOutput);
} catch (IOException e) {
System.out.println("Can't create stable connection between server and client");
}
}
public void run() {
try {
AudioFormat af = new AudioFormat(8000.0f,8,1,true,false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, af);
TargetDataLine microphone = (TargetDataLine)AudioSystem.getLine(info);
microphone.open(af);
microphone.start();
int bytesRead = 0;
byte[] soundData = new byte[1];
while(bytesRead != -1)
{
bytesRead = microphone.read(soundData, 0, soundData.length);
System.out.println(soundData.length);
if(bytesRead >= 0)
{
for(ObjectOutputStream o : outputArray) {
o.write(soundData, 0, bytesRead);
}
}
}
} catch (IOException | LineUnavailableException e) {
e.printStackTrace();
}
}
}
Related
I realized a voip application encrypted with the algorithms AES and RC4 then when I execute the application and I encrypt the voice with the two algorithms it works normal but when I add the algorithm ECC it works for the first time and after it displays this error and then nothing of these algorithms works
here is some of my code that executes voice transfer and encrypted with ECC
public VOIPClientEC(String ip, int port) {
this.ip = ip;
this.port = port;
try {
client = new Socket(ip, port);
this.start();
} catch (Exception e) {
}
}
public VOIPClientEC(Socket s) {
client = s;
this.start();
}
public void run() {
try {
//long t = System.currentTimeMillis();
OutputStream o = client.getOutputStream();
DataOutputStream dos = new DataOutputStream(o);
//dos.writeLong(t);
InputStream inp = client.getInputStream();
DataInputStream dis = new DataInputStream(inp);
// long time = dis.readLong();
EllipticCurveAlgorithm algorithm = new EllipticCurveAlgorithm();
PointProccessor processor = new PointProccessor();
long pri = 1234567891;
Point pub = processor.multiply(pri, algorithm.base);
ObjectOutputStream os = new ObjectOutputStream(o);
os.writeObject(pub.getX());
os.writeObject(pub.getY());
ObjectInputStream ois = new ObjectInputStream(inp);
Object px = ois.readObject();
System.out.println(px);
Object py = ois.readObject();
System.out.println(py);
Point p = new Point((long)px, (long)py);
inp = null;
dos = null;
dis = null;
class VOIPEcouteur extends Thread {
private BufferedInputStream buffis = null;
private InputStream is = null;
private byte[] data;
private int count,
cE;
private AudioFormat audio = null;
private SourceDataLine ligneSource = null;
private AudioInputStream audiois = null;
public VOIPEcouteur() {
try {
buffis = new BufferedInputStream(client.getInputStream());
//configuration Audio
audio = confAudio();
} catch (IOException ex) {
}
}
public void run() {
data = new byte[VOIP_BUFFER];
try {
while ((count = buffis.read(data, 0, data.length)) !=
-1 && !boolStopTalking) {
is = new ByteArrayInputStream(data);
audiois = new AudioInputStream(is, audio,
data.length / audio.getFrameSize());
DataLine.Info dataLineInfo = new DataLine.Info(
SourceDataLine.class, audio);
ligneSource = (SourceDataLine) AudioSystem.getLine(
dataLineInfo);
ligneSource.open(audio);
ligneSource.start();
while ((count = audiois.read(data, 0, data.length)) !=
-1) {
if (count > 0) {
byte[] s = algorithm.decrypt(data, pri);
ligneSource.write(s, 0, s.length);
cE++;
/* if (cE == 1){
tc3 = System.currentTimeMillis()-time;
System.out.println("temp d'execution EC : "+tc3);
}*/
}
}
}
// ligneSource.flush();
ligneSource.drain();
ligneSource.stop();
ligneSource.close();
client.close();
} catch (IOException | LineUnavailableException ex) {
}
}
}
class VOIPParleur extends Thread {
private BufferedOutputStream buffos = null;
private TargetDataLine ligneCible = null;
private AudioFormat audio = null;
private byte[] data;
private int count;
public VOIPParleur() {
try {
buffos = new BufferedOutputStream(client.
getOutputStream());
audio = confAudio();
} catch (IOException ex) {
}
}
public void run() {
try {
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class, audio);
ligneCible = (TargetDataLine) AudioSystem.getLine(
dataLineInfo);
ligneCible.open(audio);
ligneCible.start();
data = new byte[VOIP_BUFFER];
boolStopTalking = false;
try {
while (!boolStopTalking) {
count = ligneCible.read(data, 0, data.length);
if (count > 0) {
byte [] s = algorithm.encrypt(data, p);
buffos.write(s, 0, data.length);
}
}
buffos.close();
ligneCible.stop();
ligneCible.close();
client.close();
} catch (Exception e) {
}
} catch (LineUnavailableException ex) {
Logger.getLogger(VOIPClientEC.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
new VOIPParleur().start();
new VOIPEcouteur().start();
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(VOIPClientEC.class.getName()).log(Level.SEVERE, null, ex);
}
}
private AudioFormat confAudio() {
//configuration copi�e
//8000,11025,16000,22050,44100
float sampleRate = 8000.0F;
//8,16
int sampleSizeInBits = 16;
//1,2
int channels = 2;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits,
channels, signed,
bigEndian);
}
}
and that is the error
chat.VOIPClientEC$1VOIPParleur run GRAVE: null
javax.sound.sampled.LineUnavailableException: line with format
PCM_SIGNED 8000.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian 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 chat.VOIPClientEC$1VOIPParleur.run(VOIPClient.java:759)
Exception in thread "Thread-15" java.lang.NullPointerException at
chat.VOIPClientEC$1VOIPEcouteur.run(VOIPClient.java:720)
I am making a voice chat program. I have two servers one for voice and one for messages. When I connect two people I get this error, Thank you in advance. I attached the client code, ClientAudio code and the Client receive code
java.io.StreamCorruptedException: invalid type code: 00
at java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(ObjectInputStream.java:2508)
at java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectInputStream.java:2543)
at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:2702)
at java.io.ObjectInputStream.read(ObjectInputStream.java:865)
at client.chat$ClientAudioRec.run(chat.java:388)
at java.lang.Thread.run(Thread.java:745)
its calling the error on
try {
bytesRead = ((ObjectInput) i2).read(inSound, 0, inSound.length);
} catch (Exception e) {
e.printStackTrace();
}
Code
public class Client implements Runnable { // CLIENT
private String msg;
public void run() {
try {
s1 = new Socket(ipAddress, port);
s2 = new Socket(ipAddress, 1210);
o1 = new ObjectOutputStream(s1.getOutputStream());
o1.writeObject(name);
serverListModel.addElement(name);
i1 = new ObjectInputStream(s1.getInputStream());
Thread voice = new Thread(new ClientAudio());
voice.start();
while(true) {
msg = (String) i1.readObject();
String[] namePart = msg.split("-");
if(namePart[0].equals("AddName") && !namePart[1].equals(name) && !serverListModel.contains(namePart[1])) {
serverListModel.addElement(namePart[1]);
}
if(namePart[0].equals("RemoveName") && !namePart[1].equals(name)) {
serverListModel.removeElement(namePart[1]);
}
if(!msg.equals(null) && !namePart[0].equals("AddName") && !namePart[0].equals("RemoveName")) {
chatWindow.append(msg+"\n");
}
}
} catch (IOException | ClassNotFoundException e) {
chatWindow.append("Server Closed");
e.printStackTrace();
try {
s1.close();
} catch (IOException e1) {
e1.printStackTrace();
}
mainWindow(true);
}
}
}
public class ClientAudio implements Runnable { // CLIENT AUDIO
public void run() {
try {
o2 = new ObjectOutputStream(s2.getOutputStream());
System.out.println("AUDIO");
int bytesRead = 0;
byte[] soundData = new byte[1];
Thread car = new Thread(new ClientAudioRec());
car.start();
while(true) {
bytesRead = mic.read(soundData, 0, bytesRead);
if(bytesRead >= 0) {
o2.write(soundData, 0, bytesRead);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ClientAudioRec implements Runnable { // CLIENT AUDIO REC
public void run() {
i2 = new ObjectInputStream(s2.getInputStream());
System.out.println("REC");
SourceDataLine inSpeaker = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
try {
inSpeaker = (SourceDataLine)AudioSystem.getLine(info);
inSpeaker.open(af);
} catch (LineUnavailableException e1) {
System.out.println("ERROR 22");
e1.printStackTrace();
}
int bytesRead = 0;
byte[] inSound = new byte[100];
inSpeaker.start();
while(bytesRead != -1)
{
try{
bytesRead = ((ObjectInput) i2).read(inSound, 0, inSound.length);
} catch (Exception e){
e.printStackTrace();
}
if(bytesRead >= 0)
{
inSpeaker.write(inSound, 0, bytesRead);
}
}
}
}
I'm making anti-phase sound with java.(anti-phase is reflected wave. x-coordinate is not changed, but y-coordinate is upside down.)
Before reflecting sound wave, I have to get byte array(or int array) from sound.
I get sound from microphone of my laptop.
Here is my CODE(I got original code, which record sound as file, in web. I modified it little)
public class NoiseController extends Thread{
private TargetDataLine line;
private AudioInputStream audioInputStream;
public NoiseController(TargetDataLine line) {
this.line = line;
this.audioInputStream = new AudioInputStream(line);
}
public void start() {
line.start();
super.start();
}
public void stopRecording() {
line.stop();
line.close();
}
public void run() {
try {
int packet;
while((packet = audioInputStream.read()) != -1)
System.out.println(packet);
}
catch(IOException ioe) {
ioe.getStackTrace();
}
}
public static void main(String[] args) {
AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine targetDataLine = null;
try {
targetDataLine = (TargetDataLine)AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
}
catch(LineUnavailableException lue) {
out("unable to get a recording line");
lue.printStackTrace();
System.exit(-1);
}
AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
NoiseController recorder = new NoiseController(targetDataLine);
System.out.println(targetDataLine);
System.out.println(targetType);
out("Press ENTER to start the recording.");
try {
System.in.read();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
recorder.start();
out("Recording...");
out("Press ENTER to stop the recording.");
try {
System.in.read();
System.in.read();
}
catch(IOException ioe) {
ioe.getStackTrace();
}
recorder.stopRecording();
out("Recording stopped.");
}
private static void out(String msg) {
System.out.println(msg);
}
}
However, Console doesn't print anything while recording...
It shows just
com.sun.media.sound.DirectAudioDevice$DirectTDL#25154f_
WAVE
Press ENTER to start the recording.
Recording...
Press ENTER to stop the recording.
Recording stopped.
If I edit run() like AudioSystem.write(stream, fileType, out);
instead of
int packet;
while((packet = audioInputStream.read()) != -1)
System.out.println(packet);
program saves wav file.
What is wrong in my program?
you are not printing the Exception as Uwe Allner said.
I´ve also try to correct it and I think the result must be like this:
public class NoiseController extends Thread {
private final TargetDataLine line;
private final AudioInputStream audioInputStream;
public NoiseController(final TargetDataLine line) {
this.line = line;
this.audioInputStream = new AudioInputStream(line);
}
#Override
public void start() {
line.start();
super.start();
}
public void stopRecording() {
line.stop();
line.close();
try {
audioInputStream.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
try {
final int bufferSize = 1024;
int read = 0;
final byte[] frame = new byte[bufferSize];
while ((read = audioInputStream.read(frame)) != -1 && line.isOpen()) {
// only the first read bytes are valid
System.out.println(Arrays.toString(frame));
}
} catch (final IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(final String[] args) {
final AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);
final DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine targetDataLine = null;
try {
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
} catch (final LineUnavailableException lue) {
out("unable to get a recording line");
lue.printStackTrace();
System.exit(-1);
}
final AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
final NoiseController recorder = new NoiseController(targetDataLine);
System.out.println(targetDataLine);
System.out.println(targetType);
out("Press ENTER to start the recording.");
try {
System.in.read();
} catch (final IOException ioe) {
ioe.printStackTrace();
}
recorder.start();
out("Recording...");
out("Press ENTER to stop the recording.");
try {
System.in.read();
System.in.read();
} catch (final IOException ioe) {
ioe.printStackTrace();
}
recorder.stopRecording();
out("Recording stopped.");
}
private static void out(final String msg) {
System.out.println(msg);
}
}
I am trying to write a little program that reads a wav file and sends the output as if it would come from my microphone. Unfortunately I do not have much experience with the sound API.
Background: What I am basically trying to realize is a program that plays a sound while I am in a voicechat (i.e Teamspeak, Ventrilo). To get that to work now I would have to switch the recording device to "What you hear", play the sound and then switch back to microphone. The program should simulate input from the microphone.
So far I could not get any further than just playing the sound. I guess I just need a different SourceLine?
public class Player {
private final int BUFFER_SIZE = 128000;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
public void playSound(File soundFile) {
try {
audioStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
audioFormat = audioStream.getFormat();
DataLine.Info infoIn = new DataLine.Info(SourceDataLine.class,
audioFormat);
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(infoIn);
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();
}
}
Solution:
Install VB Audio Cable (http://vb-audio.pagesperso-orange.fr/Cable/). It's donationware. Make VB-Output standard recording device. In the microphone properties choose VB-Input as playback.
public class Player {
private final int BUFFER_SIZE = 128000;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
public void playSound(File soundFile) {
try {
audioStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
audioFormat = audioStream.getFormat();
DataLine.Info infoIn = new DataLine.Info(SourceDataLine.class,
audioFormat);
try {
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
Mixer mixer = null;
for (int i = 0; i < mixerInfos.length; i++) {
System.out.println(mixerInfos[i].getName());
if (mixerInfos[i].getName().equals(
"CABLE Input (VB-Audio Virtual Cable)")) {
mixer = AudioSystem.getMixer(mixerInfos[i]);
break;
}
}
sourceLine = (SourceDataLine) mixer.getLine(infoIn);
sourceLine.open(audioFormat);
} catch (LineUnavailableException 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();
}
}
You can install some program like Virtual Audio Cable and from your music player redirect playing sound to virtual input. In your program you must listen that virtual input source and you can for test redirect to normal headphones or speaker received signal or saved it to file.
I have a game that I want to play audio in. What I want to be able to do is that I should be able to declare a ClipPlayer object with the arguments of a path name. Then the constructor should be able to load the sound and then I can just make a call with that object so it won't lag. This is the code that I am using currently for sound:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
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;
import javax.sound.sampled.UnsupportedAudioFileException;
public class ClipPlayer {
AudioInputStream in;
AudioFormat decodedFormat;
AudioInputStream din;
AudioFormat baseFormat;
SourceDataLine line;
private boolean loop;
private BufferedInputStream stream;
// private ByteArrayInputStream stream;
/**
* recreate the stream
*
*/
public void reset() {
try {
stream.reset();
in = AudioSystem.getAudioInputStream(stream);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
line = getLine(decodedFormat);
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
line.close();
din.close();
in.close();
} catch (IOException e) {
}
}
ClipPlayer(String filename, boolean loop) {
this(filename);
this.loop = loop;
}
ClipPlayer(String filename) {
this.loop = false;
try {
InputStream raw = Object.class.getResourceAsStream(filename);
stream = new BufferedInputStream(raw);
// ByteArrayOutputStream out = new ByteArrayOutputStream();
// byte[] buffer = new byte[1024];
// int read = raw.read(buffer);
// while( read > 0 ) {
// out.write(buffer, 0, read);
// read = raw.read(buffer);
// }
// stream = new ByteArrayInputStream(out.toByteArray());
in = AudioSystem.getAudioInputStream(stream);
din = null;
if (in != null) {
baseFormat = in.getFormat();
decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, baseFormat
.getSampleRate(), 16, baseFormat.getChannels(),
baseFormat.getChannels() * 2, baseFormat
.getSampleRate(), false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
line = getLine(decodedFormat);
}
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
private SourceDataLine getLine(AudioFormat audioFormat)
throws LineUnavailableException {
SourceDataLine res = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat);
return res;
}
public void play() {
try {
boolean firstTime = true;
while (firstTime || loop) {
firstTime = false;
byte[] data = new byte[4096];
if (line != null) {
line.start();
int nBytesRead = 0;
while (nBytesRead != -1) {
nBytesRead = din.read(data, 0, data.length);
if (nBytesRead != -1)
line.write(data, 0, nBytesRead);
}
line.drain();
line.stop();
line.close();
reset();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Declare, load and cache the sounds as class attributes. Then when it is time to hear them, play them in a Clip.