在Android中通过TCP实时流式传输音频

时间:2022-03-01 19:00:36

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.

我试图通过TCP将实时音频从一个应用程序发送到其他应用程序。我能够插入男性插座并连接它们,但我没有得到任何声音。请帮我。这是发件人代码。

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

这些权限也会在manifest.xml中添加

1 个解决方案

#1


0  

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.

您没有使用套接字。您必须获取接收器代码,您必须使用socket.getIntpuStreamer()并使用它来读取数据并将它们保存在缓冲区中。

#1


0  

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.

您没有使用套接字。您必须获取接收器代码,您必须使用socket.getIntpuStreamer()并使用它来读取数据并将它们保存在缓冲区中。