如何在客户端和服务器之间传输语音?

时间:2015-07-17 19:08:31

标签: android

我正在开发客户端和服务器之间的语音聊天App.streaming语音。

我曾尝试使用Datagramsocket,但有很多问题。

这次我正在尝试使用Tcp。但我无法传输数据。我的代码出了什么问题?

客户:

public void startStreaming() {


    Thread streamThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {


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

                Log.d("VS", "Socket Created.c");

                byte[] buffer = new byte[256];




                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);

                Log.d("VS", "Address retrieved.c");

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


                if (recorder.getState() == AudioRecord.STATE_INITIALIZED)

                    recorder.startRecording();}


                while(status == true) {


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

                    Log.d("", ""+buffer.length);

                    //encoding to base64
                 // String buffer1= Base64.encodeToString(buffer, Base64.DEFAULT);

                    //putting buffer in the packet

                    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());

                    out.writeObject(buffer);  


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

                  //  socket.send(packet);


                }

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


            } 


        }

    });
    streamThread.start();
 }

服务器:

public void startReceiving() {


    Thread receiveThread = new Thread (new Runnable() {



        @Override
        public void run() {



            try {

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


                ServerSocket serversocket = new ServerSocket(50005);



                 // DatagramSocket socket = new DatagramSocket(50005);


                  byte[] buffer = new byte[2560];


                      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...");
                      }
                //  }


                  //minimum buffer size. need to be careful. might cause problems. try setting manually if any problems faced
                 // int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
                      Log.d("VR", ""+status);

                      InputStream is;
                      ObjectInputStream ois;
                            while(status == true) {

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

                                  socket = serversocket.accept();
                                  is = socket.getInputStream();

                                  ois = new ObjectInputStream(is);


                                 // socket.receive(packet);


                                  Log.d("VR", "Packet Received.s");

                                  //reading content from packet
                               //  buffer=packet.getData();

                                  Log.d("VR", "Packet data read into buffer.s");


                                  //sending data to the Audiotrack obj i.e. speaker
                                  speaker.write( ois.toString().getBytes(), 0, minBufSize);

                                 Log.d("VR", "Writing buffer content to speaker.s");
                        }

                        } catch (SocketException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }




                }

我认为问题在于转移声音。

请给我最好的帮助

1 个答案:

答案 0 :(得分:0)

客户方:

 BufferedWriter input;
                while(status == true) {
                  int  bufferReadResult = recorder.read(buffer, 0, buffer.length);
                      dos.write(buffer,0,bufferReadResult);
                  dos.flush();

                }