在没有sip的情况下在android中进行点对点语音呼叫

时间:2015-04-07 02:00:16

标签: android streaming voip audio-streaming peer

我正在开发一个应用程序,它允许用户通过流式传输原始pcm音频流在LAN(WI-FI)内进行语音呼叫。 但我无法播放收到的音轨

这是我的接收者代码:

   private AudioTrack speaker;



    public void startReceiving() {

        Thread receiveThread = new Thread (new Runnable() {

            @Override
            public void run() {

                try {

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


                    byte[] buffer = new byte[256];


                    //minimum buffer size. need to be careful. might cause problems. try setting manually if any problems faced
                    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) {
                        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", "Writing buffer content to speaker");

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

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


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


                recorder.startRecording();


                InetAddress IPAddress = InetAddress.getByName("192.168.0.101");
                byte[] sendData = new byte[1024];
                byte[] receiveData = new byte[1024];


                while (status==true)
                {
                    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 1127);
                    socket.send(sendPacket);
                }

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


        }

    });
    streamThread.start();
}

我不能使用SIP,它只是一个简单的音频流。 请任何人帮助我

感谢。

1 个答案:

答案 0 :(得分:0)

您有两个问题:

  1. 你的接收器线程似乎没有播放......它总是在循环中,永远不会到达speaker.play()。
  2. 您的发送线程从不发送任何数据......例如:

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