Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By gimbal2

Thread: streaming voice between two phones

  1. #1
    M0TRIX is offline Member
    Join Date
    Dec 2013
    Posts
    28
    Rep Power
    0

    Default streaming voice between two phones

    hello.

    i was seeking about sample code about voice chat.so i found it somewhere but i have few

    question about it i was wondering if you help me out....

    the sample code is two part


    1.The activity to send voice.

    PHP Code:
    public class VoiceSenderActivity extends Activity {
    
    private EditText target;
    private TextView streamingLabel;
    private Button startButton,stopButton;
    
    public byte[] buffer;
    public static DatagramSocket socket;
    private int port=50005;         //which port??
    AudioRecord recorder;
    
    //Audio Configuration. 
    private int sampleRate = 8000;      //How much will be ideal?
    private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;    
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;       
    
    private boolean status = true;
    
    
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        target = (EditText) findViewById (R.id.target_IP);
        streamingLabel = (TextView) findViewById(R.id.streaming_label);
        startButton = (Button) findViewById (R.id.start_button);
        stopButton = (Button) findViewById (R.id.stop_button);
    
        streamingLabel.setText("Press Start! to begin");
    
        startButton.setOnClickListener (startListener);
        stopButton.setOnClickListener (stopListener);
    }
    
    private final OnClickListener stopListener = new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
                    status = false;
                    recorder.release();
                    Log.d("VS","Recorder released");
        }
    
    };
    
    private final OnClickListener startListener = new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
                    status = true;
                    startStreaming();           
        }
    
    };
    
    public void startStreaming() {
    
    
        Thread streamThread = new Thread(new Runnable() {
    
            @Override
            public void run() {
                try {
    
    
                    int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
                    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(target.getText().toString());
                    Log.d("VS", "Address retrieved");
    
    
                    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
                    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) {
                    Log.e("VS", "IOException");
                } 
    
    
            }
    
        });
        streamThread.start();
     }
     }

    2.The activity to receive voice

    PHP Code:
    public class VoiceReceiverActivity extends Activity {
    
    
    private Button receiveButton,stopButton;
    
    public static DatagramSocket socket;
    private AudioTrack speaker;
    
    //Audio Configuration. 
    private int sampleRate = 8000;      //How much will be ideal?
    private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;    
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;       
    
    private boolean status = true;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        receiveButton = (Button) findViewById (R.id.receive_button);
        stopButton = (Button) findViewById (R.id.stop_button);
        findViewById(R.id.receive_label);
    
        receiveButton.setOnClickListener(receiveListener);
        stopButton.setOnClickListener(stopListener);
    
    }
    
    
    private final OnClickListener stopListener = new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            status = false;
            speaker.release();
            Log.d("VR","Speaker released");
    
        }
    
    };
    
    
    private final OnClickListener receiveListener = new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
            status = true;
            startReceiving();
    
        }
    
    };
    
    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];
    
    
                    //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");
                        }
                    }
    
    
                } catch (SocketException e) {
                    Log.e("VR", "SocketException");
                }
    
    
            }
    
        });
        receiveThread.start();
    }
    
    }
    my questtions :

    1_should i make two spreated project for each part??

    2_should i enter internet IP address? or local network ip address??

    3_the sending part(sending code) doesn't have receiving ability??

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: streaming voice between two phones

    I hope the lack of answers makes you realize it is not really a good idea to yank some random code off the net and then start asking questions about it. If you want to know something about this code, try to contact the author. If you want help with a problem here, write your own code so you at least know and understand what it does.
    M0TRIX likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    M0TRIX is offline Member
    Join Date
    Dec 2013
    Posts
    28
    Rep Power
    0

    Default Re: streaming voice between two phones

    hello

    i have coded such thing once but the program dosen't work!!! that why i looked for sample code....

    my question is :i can't make a connection with local ip and internet ip.....why?

  4. #4
    Gotcha is offline Member
    Join Date
    Jun 2015
    Location
    America
    Posts
    29
    Rep Power
    0

    Default Re: streaming voice between two phones

    Ports < 1024 && > 49150 are restricted so that may be your entire problem.
    Last edited by Gotcha; 06-17-2015 at 04:52 PM. Reason: Range was off by one.. :)

Similar Threads

  1. java-forums on phones
    By mbschutlz97 in forum Suggestions & Feedback
    Replies: 2
    Last Post: 05-09-2014, 03:59 PM
  2. sending message to phones
    By sivasankar in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-16-2011, 08:56 AM
  3. Sending sms from pc to mobile phones
    By leoanto in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 12-02-2010, 03:07 PM
  4. Peer To Peer Voice and Video Streaming
    By hardeepbhullar in forum Networking
    Replies: 0
    Last Post: 10-31-2009, 12:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •