Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8525695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:03:37+00:00 2026-06-11T08:03:37+00:00

I am working on a peer to peer to voice call application in Android

  • 0

I am working on a peer to peer to voice call application in Android for my college project. I found sample code for streaming the mic audio through UDP. That code will help me to stream the WAV file through UDP. But it doesn’t play some files properly. And also that code doesn’t stream mic audio.
Please help me.

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:03:38+00:00Added an answer on June 11, 2026 at 8:03 am

    As for as concern with my question….

    The Solution is Always while playing the Audio through out speaker will cause Some noise….

    So i found the following two solution to fix that…

    1) Play the audio through ear piece

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

    2) Next way to do use Some Audio Codec for encoding and Decoding Your Audio to play that in noiseless..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a group project for a college course, and I've hit
I'm working on a networked application that has a peer-to-peer file transfer component (think
Recently I've been working on legacy vb.net code and during code peer review it
I'm working on a programming project--writing a basic P2P filesharing application in Python. I'm
I am working on an Android App that implements Flash Peer-assisted networking. I have
A peer of mine is working on a report that displays the weekly (Sunday
Working with an undisclosed API, I found a function that can set the number
Working on the problems on Project Euler to try to learn Clojure. I'm on
I've got a peer to peer iPhone game working pretty well. I've created a
I started working on a small game just to test peer-to-peer gameplay. Anyway, as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.