0

我正在为我的大学项目开发​​ Android 中的点对点语音呼叫应用程序。我找到了通过 UDP 流式传输麦克风音频的示例代码。该代码将帮助我通过 UDP 流式传输 WAV 文件。但它不能正确播放某些文件。而且该代码不会流式传输麦克风音频。请帮我。

public class UdpStream extends Activity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.udpstream);
        Button btnSend = (Button)findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(LOG_TAG, "btnSend clicked");
                SendAudio();
            }
        });

        Button btnRecv = (Button)findViewById(R.id.btnRecv);
        btnRecv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(LOG_TAG, "btnRecv clicked");
                RecvAudio();
            }
        });

        Button btnStrmic = (Button)findViewById(R.id.btnStrmic);
        btnStrmic.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(LOG_TAG, "btnStrmic clicked");
                SendMicAudio();
            }
        });

    }

    static final String LOG_TAG = "UdpStream";
    static final String AUDIO_FILE_PATH = "/sdcard/1.wav";
    static final int AUDIO_PORT = 2048;
    static final int SAMPLE_RATE = 8000;
    static final int SAMPLE_INTERVAL = 20; // milliseconds
    static final int SAMPLE_SIZE = 2; // bytes per sample
    static final int BUF_SIZE = SAMPLE_INTERVAL*SAMPLE_INTERVAL*SAMPLE_SIZE*2;
    protected String host="localhost";
    public void RecvAudio()
    {
        Thread thrd = new Thread(new Runnable() {
            @Override
            public void run() 
            {
                Log.e(LOG_TAG, "start recv thread, thread id: "
                    + Thread.currentThread().getId());
                AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, 
                        SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                        AudioFormat.ENCODING_PCM_16BIT, BUF_SIZE, 
                        AudioTrack.MODE_STREAM);
                track.play();
                try
                {
                    DatagramSocket sock = new DatagramSocket(AUDIO_PORT);
                    byte[] buf = new byte[BUF_SIZE];

                    while(true)
                    {
                        DatagramPacket pack = new DatagramPacket(buf, BUF_SIZE);
                        sock.receive(pack);
                        Log.d(LOG_TAG, "recv pack: " + pack.getLength());
                        track.write(pack.getData(), 0, pack.getLength());
                    }
                }
                catch (SocketException se)
                {
                    Log.e(LOG_TAG, "SocketException: " + se.toString());
                }
                catch (IOException ie)
                {
                    Log.e(LOG_TAG, "IOException" + ie.toString());
                }
            } // end run
        });
        thrd.start();
    }
    Toast toast;
    int port=3008;
    public void SendAudio()
    {

        toast=Toast.makeText(getApplicationContext(), port + " : " + host , Toast.LENGTH_SHORT);

        Thread thrd = new Thread(new Runnable() {
            @Override
            public void run() 
            {
                Log.e(LOG_TAG, "start send thread, thread id: "
                    + Thread.currentThread().getId());
                long file_size = 0;
                int bytes_read = 0;
                int bytes_count = 0;
                File audio = new File(AUDIO_FILE_PATH);
                FileInputStream audio_stream = null;
                file_size = audio.length();
                byte[] buf = new byte[BUF_SIZE];
                try
                {

                    EditText d= (EditText) findViewById(R.id.txttohost);
                    host=d.getText().toString();


                    if(host=="")
                    {
                    port=2048;
                    host="localhost";

                    }
                    port=2048;
                    InetAddress addr = InetAddress.getByName(host);
                    toast.setText(port + " : " + addr.toString());
                    toast.show();
                    DatagramSocket sock = new DatagramSocket();
                    audio_stream = new FileInputStream(audio);

                    while(bytes_count < file_size)
                    {
                        bytes_read = audio_stream.read(buf, 0, BUF_SIZE);
                        DatagramPacket pack = new DatagramPacket(buf, bytes_read,
                                addr, port);
                        sock.send(pack);
                        bytes_count += bytes_read;
                        Log.d(LOG_TAG, "bytes_count : " + bytes_count);
                        Thread.sleep(SAMPLE_INTERVAL, 0);
                    }
                }
                catch (InterruptedException ie)
                {
                    Log.e(LOG_TAG, "InterruptedException");
                }
                catch (FileNotFoundException fnfe)
                {
                    Log.e(LOG_TAG, "FileNotFoundException");
                }
                catch (SocketException se)
                {
                    Log.e(LOG_TAG, "SocketException");
                }
                catch (UnknownHostException uhe)
                {
                    Log.e(LOG_TAG, "UnknownHostException");
                }
                catch (IOException ie)
                {
                    Log.e(LOG_TAG, "IOException");
                }
            } // end run
        });
        thrd.start();
    }

    public void SendMicAudio()
    {
        Thread thrd = new Thread(new Runnable() {
            @Override
            public void run() 
            {
                Log.e(LOG_TAG, "start SendMicAudio thread, thread id: "
                    + Thread.currentThread().getId());
                AudioRecord audio_recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, SAMPLE_RATE,
                            AudioFormat.CHANNEL_CONFIGURATION_MONO,
                            AudioFormat.ENCODING_PCM_16BIT,
                            AudioRecord.getMinBufferSize(SAMPLE_RATE,
                                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                    AudioFormat.ENCODING_PCM_16BIT) * 10);
                int bytes_read = 0;
                int bytes_count = 0;
                byte[] buf = new byte[BUF_SIZE];
                try
                {
                    InetAddress addr = InetAddress.getLocalHost();
                    DatagramSocket sock = new DatagramSocket();

                    while(true)
                    {
                        bytes_read = audio_recorder.read(buf, 0, BUF_SIZE);
                        DatagramPacket pack = new DatagramPacket(buf, bytes_read,
                                addr, AUDIO_PORT);
                        sock.send(pack);
                        bytes_count += bytes_read;
                        Log.d(LOG_TAG, "bytes_count : " + bytes_count);
                        Thread.sleep(SAMPLE_INTERVAL, 0);
                    }
                }
                catch (InterruptedException ie)
                {
                    Log.e(LOG_TAG, "InterruptedException");
                }
//                catch (FileNotFoundException fnfe)
//                {
//                    Log.e(LOG_TAG, "FileNotFoundException");
//                }
                catch (SocketException se)
                {
                    Log.e(LOG_TAG, "SocketException");
                }
                catch (UnknownHostException uhe)
                {
                    Log.e(LOG_TAG, "UnknownHostException");
                }
                catch (IOException ie)
                {
                    Log.e(LOG_TAG, "IOException");
                }
            } // end run
        });
        thrd.start();
    }
}
4

1 回答 1

1

至于关心我的问题......

解决方案是始终在通过外置扬声器播放音频时会导致一些噪音....

所以我找到了以下两个解决方案来解决这个问题......

1)通过耳机播放音频

audio_service.setSpeakerphoneOn(false); audio_service.setMode(AudioManager.MODE_IN_CALL); audio_service.setRouting(AudioManager.MODE_NORMAL,AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);

2)下一个方法是使用一些音频编解码器来编码和解码你的音频以无噪音播放。

于 2012-09-14T15:15:17.520 回答