如何在Android中的套接字上实现音频呼叫

时间:2013-09-04 05:53:46

标签: android audio stream voip voice

我搜索并在简单套接字上找到两个Android手机之间的语音流或音频呼叫代码。我实现了这个,但是这段代码不起作用。我无法听到任何声音。

接收人代码:

    private int sampleRate = 44100;
    private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;    
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;       
    int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);


public void startReceiving() {

        Thread receiveThread = new Thread (new Runnable() {

            @Override
            public void run() {

                try {

                    DatagramSocket socket = new DatagramSocket(50005);
                    Log.d("VR", "Socket Created");


                    byte[] buffer = new byte[256];
                    speaker = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,channelConfig,audioFormat,minBufSize,AudioTrack.MODE_STREAM);



                    while(status == true) {
                        try {


                            DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
                            socket.receive(packet);
                            Log.d("VR", "Packet Received");

                            //reading content from packet
                            buffer=packet.getData();
                            Log.d("VR", "Packet data read into buffer");

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

                            Log.d("VR", String.valueOf(buffer));

                            speaker.play();

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


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


            }

        });
        receiveThread.start();
    }

发件人代码:

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

            @Override
            public void run() {
                try {

                    DatagramSocket socket = new DatagramSocket();
                    Log.d("VS", "Socket Created");

                    byte[] buffer = new byte[minBufSize];

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

                    final InetAddress destination = InetAddress.getByName("192.168.0.216");
                    Log.d("VS", "Address retrieved");


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

                    recorder.startRecording();


                    while(status == true) {


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

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

                        socket.send(packet);
                    }

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

        });
        streamThread.start();
     }

我调试了代码,数据包成功传输,并且还调用了speaker.play()。但是没有Voice

我已在单个应用程序和活动中实现此代码。有两个按钮Start ListeningStart Streaming

2 个答案:

答案 0 :(得分:2)

对于getMinBufferSize()AudioRecord

AudioTrack可能不一样(了解了很难)。确保你使用的是较大的一个。

答案 1 :(得分:0)

一切对我来说都很好......只是想问一下如何在公共IP上发送数据包,例如119.43.214.5。我已经制作了两个应用程序,他们可以在localhost上发送数据包客户端已获得服务器应用程序的IP地址。问题是IP是公共IP,客户端没有在该应用上发送数据。