Tcp streaming voice

I encoded an application that transmits audio over TCP from the client to the server, but it does not work, that is, there is no audible output. Could you check my code, tell me what is wrong with it?

Customer:

public void startStreaming() {


   Thread streamThread = new Thread(new Runnable() {

       @Override
       public void run() {
            try {

                int minBufSize =AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);

                Log.d("VD ", "Bufer intioalised "+minBufSize);

                short[] buffer=new short[minBufSize];


                Log.d("VS","Buffer created of size .c" + minBufSize);
              //  DatagramPacket packet;

                final InetAddress destination = InetAddress.getByName(target.getText().toString());
                port=Integer.parseInt(target_port.getText().toString());

                Socket socket=new Socket(destination,port);

                DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
                Log.d("VS", "Address retrieved.c");


                if (minBufSize != AudioRecord.ERROR_BAD_VALUE) {
                    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
                Log.d("VS", "Recorder initialized.c");}


                if (recorder.getState() == AudioRecord.STATE_INITIALIZED){
                    Log.d("VS", "Recorder working....c");
                    recorder.startRecording();}

                BufferedWriter input;
                while(status == true) {


                    //reading data from MIC into buffer
                  int  bufferReadResult = recorder.read(buffer, 0, buffer.length);


                      dos.write(buffer,0,bufferReadResult);

                  dos.flush();



                }

            } catch(UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                Log.e("IOException message:",e.getMessage().toString());


            }



        }

Server:

public void startStreaming () {Thread streamThread = new Thread (new Runnable () {

    @Override
    public void run() {



        try {

            int  minBufSize =1024;//recorder.getMinBufferSize(sampleRate,channelConfig,audioFormat);


            ServerSocket serversocket = new ServerSocket(50005);



             // DatagramSocket socket = new DatagramSocket(50005);


              byte[] buffer = new byte[1024];


                  if (minBufSize != AudioRecord.ERROR_BAD_VALUE) {



                      speaker = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,channelConfig,audioFormat,minBufSize,AudioTrack.MODE_STREAM);


                      speaker.play();
                      Log.d("VR", "spekaer playing...");
                  }
            //  }




                  Log.d("VR", ""+status);
                  BufferedReader input;
                  InputStream is;
                  ObjectInputStream ois;
                  ByteArrayInputStream baiss;
                  socket = serversocket.accept();
                    DataInputStream dis=new DataInputStream(new BufferedInputStream(socket.getInputStream()));


                        while(status == true) {

                              //DatagramPacket packet = new DatagramPacket(buffer,buffer.length);

                            InputStream in = socket.getInputStream();

                            Log.d("content :", socket.getOutputStream().toString());


                            int i=0;
                            while (dis.available() > 0 && i < buffer.length) {
                                buffer[i]=(byte) dis.readShort();
                              i++;
                            }

       speaker.write(buffer,0,buffer.length);

Please give me the best help.

+4
source share
1 answer

Answers are given in the comments. For both the server and the client, read () / write () should have been implemented better.

+1
source

Source: https://habr.com/ru/post/1598516/


All Articles