Sending Audio Through Sockets

Okay, I have been using this tutorial as well as others as guides.

http://www.developer.com/java/other/article.php/1572251#Preface

I am a real beginner at JAVA so please bear with me for vagueness.

I want to capture the mic or line. And from there "stream out" using TCP or UDP, I am familiar in TCP and will send the output to a VB Server to handle it, then spit it back out to a java applet. I got the VB Server & Java Applet part handled.

However I have tried creating a few things, one actually sent data, but it sent the buffer array size and quit, maybe I'm not making a right .io call or while loop, who knows, but is there anybody who could help with a simple solution?

Thank you very much.

[742 byte] By [DJHotIcea] at [2007-11-26 13:06:06]
# 1

Hi,

I was involved in a project to send audio through socket

a couple of months a go.

If you don't do any encoding for the audio, the sending/capturing

process is really simple, all you have to do is to write the audio bytes

to socket, it's just like writing any other data.

Here's a thread that might help you. Check the reply #5 by me.

http://forum.java.sun.com/thread.jspa?threadID=744833&tstart=75

kari-matti

Message was edited by:

kari-matti

kari-mattia at 2007-7-7 17:14:19 > top of Java-index,Security,Cryptography...
# 2
Hi,kari-matti, can you send me code which you used to send bytes through network, receive it and then convert back to file. Thank you.
boba5555a at 2007-7-7 17:14:19 > top of Java-index,Security,Cryptography...
# 3

Hi boba

Did you read the thread that I gave link to in my previous post?

I believe that'll give you idea of how to send audio through socket

and receive it in the other end. I never had to convert the bytes back

to a file, I just played them straight.

I don't have any code, but these written in the forums.

Audio bytes are just like any other data, you'll have to send

those through socket the same way as any other data.

If you haven't used sockets before, then do some simple

programs that send/receive integers, strings etc. After you

get that working, try to send a text file. If you get this working

too, then sending audio file is no different.

In your post you say

> send bytes through network, receive it and then

> convert back to file.

I'm not sure, but I think that you don't have to do conversions

in the receiving end. In sending program, you have to read audio

bytes from file and send them to socket. And in the receiving program

you'll have receive the data from socket and write it to file. I believe

that when you write data in a file, the encoding should be correct if

your program writes all the bytes in the same order as they were sent.

There are good socket tutorial on the Internet, you should read those

if you have problems understanding them. Here's a link to one that

I think is good

http://java.sun.com/docs/books/tutorial/networking/sockets/index.html

kari-matti

kari-mattia at 2007-7-7 17:14:19 > top of Java-index,Security,Cryptography...
# 4

Hi kari,

I also have a problem while sending audio through socket!!.

I used code available at

http://java.sun.com/products/java-media/sound/samples/JavaSoundDemo/

for the client side. In this code, in saveToFile(name, fileType) method, i changed the code so that it will write data to socket instead of file.

try {

Socket soc = new Socket(myIp ,myPort);

DataOutputStream socOut = new DataOutputStream(soc.getOutputStream());

if (AudioSystem.write(audioInputStream, fileType, socOut) == -1) {

throw new IOException("Problems connecting to server");

}

soc.close();

} catch (Exception ex) { reportStatus(ex.toString()); }

Following code on the server side recieves this data and saves it to file

try {

while (clientSocket.isConnected() && (bufferSize = input.available()) > 0) {

byte[] audioData = new byte[bufferSize];

serverObj.display.append("\naudio data recieved from the client , BUFFER SIZE = " + bufferSize);

input.read(audioData , 0 , bufferSize);

byteArrayInputStream = new ByteArrayInputStream(audioData);

audioInputStream = new AudioInputStream(byteArrayInputStream,audioFormat,audioData.length/audioFormat.getFrameSize());

//audioInputStream = AudioSystem.getAudioInputStream(byteArrayInputStream);

audioFormat = audioInputStream.getFormat();

AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;

//audioInputStream.reset();

if (AudioSystem.write(audioInputStream, fileType, outputFile) == -1) {

throw new IOException("Problems connecting to server");

}

}

input.close();

clientSocket.close();

} catch (IOException e) {

System.err.println("Error in UserThread run() method");

e.printStackTrace();

}

The problem is that at server, the file gets saved contains only noise and not the actual voice.

Am i missing something here? AudioFormat on both client and server is same!

Regards,

Naveed.

naveeda at 2007-7-7 17:14:20 > top of Java-index,Security,Cryptography...