package vc_client1;

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 voiceReceiver {

    public Thread playThread;
    boolean stopCapture = false;
    static ByteArrayOutputStream byteArrayOutputStream;
    AudioFormat audioFormat;
    static TargetDataLine targetDataLine;
    AudioInputStream audioInputStream;
    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    static Socket sock = null;
    static SourceDataLine sourceDataLine;
        String IP=mainFrame.serIP2;
        static int port=5000;

    public voiceReceiver() {

        System.out.println("Hiiiiiiiiiiiiii");
    }

        public void captureAudio() {
        try {

            sock = new Socket(IP,port);

                        System.out.println("Ip address capture bye client is : "+IP);

            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 < mixerInfo.length; cnt++) {
                System.out.println(mixerInfo[cnt].getName());
            }
            audioFormat = getAudioFormat();

            DataLine.Info dataLineInfo = new DataLine.Info(
                    TargetDataLine.class, audioFormat);

            Mixer mixer = AudioSystem.getMixer(mixerInfo[3]);

            targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);

            targetDataLine.open(audioFormat);
            targetDataLine.start();

            Thread captureThread = new CaptureThread();
            captureThread.start();

            DataLine.Info dataLineInfo1 = new DataLine.Info(
                    SourceDataLine.class, audioFormat);
            sourceDataLine = (SourceDataLine) AudioSystem
                    .getLine(dataLineInfo1);
            sourceDataLine.open(audioFormat);
            sourceDataLine.start();

             playThread = new PlayThread();
            playThread.start();

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

        /////////////////////////////////////////////

    void stopCapture() {
       // throw new UnsupportedOperationException("Not yet implemented");
        playThread.stop();
        playThread=null;
    }
////////////////////////////////////////////////////////////////////    
        class CaptureThread extends Thread {

        byte tempBuffer[] = new byte[10000];

        public void run() {
            byteArrayOutputStream = new ByteArrayOutputStream();
            stopCapture = false;
            try {
                while (!stopCapture) {

                    int cnt = targetDataLine.read(tempBuffer, 0,
                            tempBuffer.length);

                    out.write(tempBuffer);

                    if (cnt > 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();
            }
        }
    }




}

I am trying to stop the above class thread from a button, but I could not, I tryed to use playThread.stop in the function stopCapture, then I call this function from the button, but it doesn't work.

Here's what the Java API documentation has to say:

stop()
Deprecated.
This method is inherently unsafe. (...) Many uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. If the target thread waits for long periods (on a condition variable, for example), the interrupt method should be used to interrupt the wait.

Ok, thank you, I know that Mr.JamesCherril, I tried also many different ways, but unfortunatly not sacceed. This is a one way, but not working

private boolean threadDone = false;

//////////

 public void stopCapture() {
        threadDone = true;
    }
///////////////////////

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

Maybe it's always waiting on in.read? You could try closing that input stream, which should cause the read to throw an exception

I think, there is no way to close in.read except closing it by closing the socket which will cause the system to exit such this way:

try {
                         sock.getInputStream().close();
        } catch (IOException ex) {
            Logger.getLogger(voiceReceiver.class.getName()).log(Level.SEVERE, null, ex);
        }

Do you know another way?

You could just close the input stream associated with that socket, but anyway closing the stream or socket should not result in the close throwing an exception, it's the read that will throw it. It's up to you whether you exit or not in the appropriate catch.

Oh Ok, I got it. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.