Results 1 to 5 of 5

Thread: Sound streaming

  1. #1
    Hamin is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Question Sound streaming

    Hello,
    I want to develop an application where users can vocally chat through a Java socket. My idea of how it would work is that one user would instance a ServerSocket, the call accept() and wait for a client. When the client connected, a new thread would be started on both computers that would monitor the computer's microphone and send the data through the socket. The main thread would continually monitor the input stream for new data that it would send to the speaker. How could I do this? Or is there an easier approach than the idea I had?

    Thanx,
    Hamin

  2. #2
    JDiver is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Sound streaming

    Hi,

    I dont know of any other way to do this. By reading your post I cant tell where your problem is but i suggest that you make yourself familiar with the following topics if you aren't already:

    io / nio
    sockets (Lesson: All About Sockets (The Java™ Tutorials > Custom Networking))
    sound (Trail: Sound (The Java™ Tutorials))

    I think it should be pretty straightforward. I am not sure about the performance though since i've never done anything like this and most VoIP applications use UDP.

  3. #3
    Hamin is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Sound streaming

    So are you saying it might be easier if I had a group of people to use UDP multicasting instead?

  4. #4
    JDiver is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Sound streaming

    I havent used a lot of UDP so i don't know wether it's actually easier to use but it gives you more performance. There is a good comparison between TCP and UDP here: TCP vs UDP - Difference and Comparison | Diffen. The only problem is that using UDP you might not be able to route the AudioInputStream through the socket in another AudioInputStream because the api might not be able to handle lost packages. If you really want to put a lot of effort into it you could read the rfcs about SIP and RTP and implement that protocol. Another issue when using UDP is to implement a good error handling algorithm that would hide any lost packets from the user using the arrived packets only so that the transmission seems smooth. Even though it isn't.

  5. #5
    sanjayagal is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default Re: Sound streaming

    try this

    Warning: Dont execute using sampe pc use different pc for each files otherwise it will be rise error ( mic already busy )

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;

    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.Mixer;
    import javax.sound.sampled.SourceDataLine;
    import javax.sound.sampled.TargetDataLine;

    public class sender {
    ServerSocket MyService;
    Socket clientSocket = null;
    BufferedInputStream input;
    TargetDataLine targetDataLine;

    BufferedOutputStream out;
    ByteArrayOutputStream byteArrayOutputStream;
    AudioFormat audioFormat;

    SourceDataLine sourceDataLine;
    byte tempBuffer[] = new byte[10000];

    sender() throws LineUnavailableException{
    try {
    audioFormat = getAudioFormat();
    DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class,audioFormat);
    sourceDataLine = (SourceDataLine)
    AudioSystem.getLine(dataLineInfo);
    sourceDataLine.open(audioFormat);
    sourceDataLine.start();
    MyService = new ServerSocket(500);
    clientSocket = MyService.accept();
    captureAudio();
    input = new BufferedInputStream(clientSocket.getInputStream()) ;
    out=new BufferedOutputStream(clientSocket.getOutputStream( ));

    while(input.read(tempBuffer)!=-1){
    sourceDataLine.write(tempBuffer,0,10000);
    }
    } catch (IOException e) {

    e.printStackTrace();
    }

    }
    private AudioFormat getAudioFormat(){
    float sampleRate = 8000.0F;
    int sampleSizeInBits = 16;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = false;
    return new AudioFormat(
    sampleRate,
    sampleSizeInBits,
    channels,
    signed,
    bigEndian);
    }
    public static void main(String s[]) throws LineUnavailableException{
    sender s2=new sender();
    }


    private void captureAudio() {
    try {

    Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
    System.out.println("Available mixers:");
    for (int cnt = 0; cnt < audioformat =" getAudioFormat();" datalineinfo =" new" mixer =" AudioSystem.getMixer(mixerInfo[3]);" targetdataline =" (TargetDataLine)" capturethread =" new" cnt =" targetDataLine.read(tempBuffer," stopcapture =" false;" out =" null;" in =" null;" sock =" null;" tx =" new" sock =" new" out =" new" in =" new" mixerinfo =" AudioSystem.getMixerInfo();" cnt =" 0;" audioformat =" getAudioFormat();" datalineinfo =" new" mixer =" AudioSystem.getMixer(mixerInfo[3]);" targetdataline =" (TargetDataLine)" capturethread =" new" datalineinfo1 =" new" sourcedataline =" (SourceDataLine)" playthread =" new" bytearrayoutputstream =" new" stopcapture =" false;" cnt =" targetDataLine.read(tempBuffer,"> 0) {

    byteArrayOutputStream.write(tempBuffer, 0, cnt);

    }
    }
    byteArrayOutputStream.close();
    } catch (Exception e) {
    System.out.println(e);
    System.exit(0);
    }
    }
    }

    private AudioFormat getAudioFormat() {
    float sampleRate = 8000.0F;

    int sampleSizeInBits = 16;

    int channels = 1;

    boolean signed = true;

    boolean bigEndian = false;

    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
    bigEndian);
    }

    class PlayThread extends Thread {
    byte tempBuffer[] = new byte[10000];

    public void run() {
    try {
    while (in.read(tempBuffer) != -1) {
    sourceDataLine.write(tempBuffer, 0, 10000);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    }

    ----------------------------------------------

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.Mixer;
    import javax.sound.sampled.SourceDataLine;

    import javax.sound.sampled.TargetDataLine;

    public class Tx {
    boolean stopCapture = false;

    ByteArrayOutputStream byteArrayOutputStream;

    AudioFormat audioFormat;

    TargetDataLine targetDataLine;

    AudioInputStream audioInputStream;

    BufferedOutputStream out = null;

    BufferedInputStream in = null;

    Socket sock = null;

    SourceDataLine sourceDataLine;

    public static void main(String[] args) {
    Tx tx = new Tx();
    tx.captureAudio();

    }

    private void captureAudio() {
    try {

    sock = new Socket("192.168.1.51", 500);

    out = new BufferedOutputStream(sock.getOutputStream());
    in = new BufferedInputStream(sock.getInputStream());

    Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
    System.out.println("Available mixers:");
    for (int cnt = 0; cnt < audioformat =" getAudioFormat();" datalineinfo =" new" mixer =" AudioSystem.getMixer(mixerInfo[3]);" targetdataline =" (TargetDataLine)" capturethread =" new" datalineinfo1 =" new" sourcedataline =" (SourceDataLine)" playthread =" new" bytearrayoutputstream =" new" stopcapture =" false;" cnt =" targetDataLine.read(tempBuffer,"> 0) {

    byteArrayOutputStream.write(tempBuffer, 0, cnt);

    }
    }
    byteArrayOutputStream.close();
    } catch (Exception e) {
    System.out.println(e);
    System.exit(0);
    }
    }
    }

    private AudioFormat getAudioFormat() {
    float sampleRate = 8000.0F;

    int sampleSizeInBits = 16;

    int channels = 1;

    boolean signed = true;

    boolean bigEndian = false;

    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
    bigEndian);
    }

    class PlayThread extends Thread {
    byte tempBuffer[] = new byte[10000];

    public void run() {
    try {
    while (in.read(tempBuffer) != -1) {
    sourceDataLine.write(tempBuffer, 0, 10000);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    }

Similar Threads

  1. video streaming
    By anniya0n in forum JDBC
    Replies: 3
    Last Post: 05-08-2013, 03:21 PM
  2. Problems with sound in java and USB sound card
    By marblecatdog in forum New To Java
    Replies: 1
    Last Post: 04-19-2011, 12:02 PM
  3. Java MP3 streaming
    By Noktasal in forum Networking
    Replies: 0
    Last Post: 12-18-2009, 04:38 PM
  4. simple mp3 streaming
    By zildjohn01 in forum CLDC and MIDP
    Replies: 0
    Last Post: 11-22-2008, 05:27 AM
  5. streaming mp3
    By arnab321 in forum CLDC and MIDP
    Replies: 1
    Last Post: 10-11-2008, 03:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •