简体   繁体   中英

Live audio streaming over TCP in Android

I am trying to send live audio from one app to other app over TCP. I am able to male socket and connect them but i am not getting any kind of sound. Please help me. This is sender code.

public void startStreaming() {

        Thread streamThread = new Thread(new Runnable() {


            @Override
            public void run() {
                try {

                    socket = new ServerSocket(port);
                    s = socket.accept();

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

                    byte[] buffer = new byte[1024];
                    BufferedOutputStream out = new BufferedOutputStream(new ByteArrayOutputStream(1024));

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

                    Log.d("VS","Buffer created of size " + minBufSize);

                    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
                    Log.d("VS", "Recorder initialized");

                    recorder.startRecording();



                    while(status == true) {


                        recorder.read(buffer, 0, buffer.length);

                        Log.d("VS", "Packet created");

                        out.write(buffer);
                        out.flush();

                        Log.d("VS", "Packet Sent");



                    }



                } catch(UnknownHostException e) {
                    Log.e("VS", "UnknownHostException");
                } catch (IOException e) {
                    Log.e("VS", "IOException");
                }


            }

        });
        streamThread.start();
    }
}

This is receiver code.

public void receiveButton(View view){
            status = true;
            Log.d("VR","start receiving called");
            startReceiving();

        }

    public void startReceiving() {

        Thread receiveThread = new Thread (new Runnable() {

            @Override
            public void run() {

                try {

                    socket = new Socket("192.168.0.3",50005);
                    Log.d("VR", "Socket Created1");


                    byte[] buffer = new byte[1024];

                    BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(buffer));

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

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

                    speaker.play();

                    while(status == true) {


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

                        in.read(buffer);

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


                        speaker.write(buffer, 0, buffer.length);
                        Log.d("VR", "Writing buffer content to speaker");

                    }


                } catch (SocketException e) {
                    Log.e("VR", "SocketException");
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }

        });
        receiveThread.start();
    }

}

Please help me. While seeing log it feels that code is working fine but there is no sound is coming.

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

these permissions are also added in manifest.xml

You are not using the socket. You have to get the receiver code you have to use the socket.getIntpuStreamer() and use it to read datas and save them on the buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM