UDP音频聊天中的语音不清楚

时间:2015-02-24 13:50:45

标签: java android audio bytearray datagram

我在PC上成功开发了一个UDP Java程序,它将从PC上的 mic发送音频/语音到Android上的UDP服务器。我能够听到声音,但它不是听得见的或者是一种幼稚的声音。

我认为问题在于我使用的字节数组的大小。

我可以分别在客户端和服务器中获得字节数组[750]和[1500]的值。

这是我的代码:

public class AudioClient
{

    /**
     * 
     */

    boolean stopaudioCapture = false;
    ByteArrayOutputStream byteOutputStream;
    AudioFormat adFormat;
    TargetDataLine targetDataLine;
    AudioInputStream InputStream;
    SourceDataLine sourceLine;

    public static void main(String args[]) 
    {
        new AudioClient();
    }

    public AudioClient() 
    {
        captureAudio();

    }

    public void stopAudio()
    {
        targetDataLine.close();
    }

    private void captureAudio() {
        try {
            adFormat = getAudioFormat();
            DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, adFormat);
            targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
            targetDataLine.open(adFormat);
            targetDataLine.start();

            Thread captureThread = new Thread(new CaptureThread());
            captureThread.start();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }


    private AudioFormat getAudioFormat() 
    {
        float sampleRate = 16000.0F;
        int sampleInbits = 16;
        int channels = 1;
        boolean signed = true;
        boolean bigEndian = false;
        return new AudioFormat(sampleRate, sampleInbits, channels, signed, bigEndian);
    }

    class CaptureThread extends Thread 
    {

        byte tempBuffer[] = new byte[800];    //**Need to alter size** 

        public void run() 
        {

            DatagramSocket clientSocket;
            byteOutputStream = new ByteArrayOutputStream();
            stopaudioCapture = false;
            try {
                clientSocket = new DatagramSocket();
//              InetAddress IPAddress = InetAddress.getByName("127.0.0.1");
                InetAddress IPAddress = InetAddress.getByName("192.168.1.101");

                while (!stopaudioCapture) {
                    int cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
                    if (cnt > 0) {
                        DatagramPacket sendPacket = new DatagramPacket(tempBuffer, tempBuffer.length, IPAddress, 50005);
                        System.out.println("Sending data to"+IPAddress);
                        clientSocket.send(sendPacket);
                    }
                }
                clientSocket.close();
            } catch (Exception e) {
                System.out.println("CaptureThread::run()" + e);
                System.exit(0);
            }
        }
    }
}

用于接收音频数据并提供给发言人的Android服务器代码:

public class Speaker extends Thread
{

    int port = 50005;
    AudioFormat format = new AudioFormat();

    private int sampleRate = 44100;

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

    public void run()
    {
        Log.d("print", "Speaker is started");

        byte[] data = new byte[1500]; //**Alter size for efficiency**    

        try
        {
            DatagramSocket dsk = new DatagramSocket(port);
            DatagramPacket dgp = new DatagramPacket(data, data.length);

            AudioTrack speaker = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, audioFormat, minBufSize, AudioTrack.MODE_STREAM);

            while(true)
            {
                dsk.receive(dgp);
                byte[] addata = dgp.getData();
                speaker.write(addata, 0, addata.length);

                System.out.println("Playing");
                speaker.play();
            }
        } 
        catch(SocketException e)
        {
            Log.d("OOPS", "Speaker server got exception");
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

}

感谢您有兴趣开发这样的程序...... :)

0 个答案:

没有答案