3

我已经搜索并在简单的插座上找到了两个 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

4

2 回答 2

2

getMinBufferSize()AudioRecord和可能不一样AudioTrack(很难学到)。确保您使用的是两者中较大的一个。

于 2013-09-30T10:25:23.730 回答
0

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

于 2014-08-03T23:15:12.440 回答