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

时间:2015-08-18 07:14:55

标签: java android

我正尝试通过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();
    }
}

这是接收者代码。

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

}

请帮帮我。 看到日志时,感觉代码工作正常,但没有声音传来。

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

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

1 个答案:

答案 0 :(得分:0)

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