0

i've developed an Android application. like voice chating

streaming audio between two android device works with WIFI on the local network.

but when i press the start button nothing happends.it should Ring and make connection

server code:

package com.example.voicechatserver;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.app.Activity;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {


private Button receiveButton,stopButton;
private TextView recive;
private EditText port;
public static DatagramSocket socket;
private AudioTrack speaker;
private int port_num=4444;

private int sampleRate = 44100;      
private int channelConfig =  AudioFormat.CHANNEL_OUT_MONO;    
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;       
int minBufSize =AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
byte[] buffer;  //256

private boolean status = true;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    receiveButton = (Button) findViewById (R.id.receive_button);
    stopButton = (Button) findViewById (R.id.stop_button);
    recive= (TextView) findViewById(R.id.receive_label);
    //recive.setText("Created...!");
    receiveButton.setOnClickListener(receiveListener);
    stopButton.setOnClickListener(stopListener);
    //receiveButton.setText("Start Reciving...!");
    //stopButton.setText("Stop Reciving...!");
    port=(EditText) findViewById(R.id.editText1);
    //port.setText("1234");

    AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
   // sampleRate =Integer.parseInt( audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE));
}


private final OnClickListener stopListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        status = false;
        //recive.setText("Recoder Stop...!");
        speaker.release();
        Log.d("VR","Speaker released");

    }

};


private final OnClickListener receiveListener = new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        status = true;
       // recive.setText(" before Start ");

        startReceiving();

    }

};

public void startReceiving() {

    Thread receiveThread = new Thread (new Runnable() {

        @Override
        public void run() {



                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        port_num=Integer.parseInt(port.getText().toString()); 
                       buffer= new byte[4096];
                        DatagramSocket socket = null;

                        try {
                            socket = new DatagramSocket(port_num);
                            Log.d("VR", "Socket Created");
                            speaker = new AudioTrack(AudioManager.STREAM_VOICE_CALL,sampleRate,channelConfig,audioFormat,16000,AudioTrack.MODE_STREAM);
                           //AudioTrack.MODE_STREAM,
                            speaker.play();
                            while(status == true) {

                                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");
                               buffer= Base64.decode(buffer, Base64.DEFAULT);
                                //sending data to the Audiotrack obj i.e. speaker
                                speaker.write(buffer, 0, minBufSize);
                                Log.d("VR", "Writing buffer content to speaker");
                        }

                        } catch (SocketException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }



                    }
                });}

    });
    receiveThread.start();
}}

Client code:

package com.example.voicechatclient;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;



import android.app.Activity;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

private EditText target,target_port;
private TextView streamingLabel;
private Button startButton,stopButton;

public byte[] buffer;
public static DatagramSocket socket;
private int port=1370;        
AudioRecord recorder;


private int sampleRate = 44100;
private int channelConfig = AudioFormat.CHANNEL_IN_MONO;    
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;       

private boolean status = true;




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_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);
    target_port=(EditText) findViewById(R.id.target_Port);
    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);
                   buffer= Base64.encode(buffer, Base64.DEFAULT);
                    //putting buffer in the packet
                    port=Integer.parseInt(target_port.getText().toString());
                    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();
 }
 }

tested local Ip address correctly. IP's like 192.168.1.100 (my phone's ip) 10.0.2.2 and ....

Ryan10
  • 126
  • 1
  • 10
  • Maybe your streamThread or receiveThread crashed and your not catching it! and I can't see any code stoping your threads from executing and cleanly releasing the socket connection – Gomino Jun 02 '15 at 16:08
  • the only thing i see i Logcat is : IOException. in client side. and no red log in server side – Ryan10 Jun 02 '15 at 16:19
  • What is the description message of the exception ? – Gomino Jun 02 '15 at 16:21
  • no description. it is this line is runing. Log.e("VS", "IOException"); – Ryan10 Jun 02 '15 at 16:27
  • replace your custom log with `e.printStackTrace()` and check the description message again – Gomino Jun 02 '15 at 16:29
  • i did. the logcat is empty.seems working normaly – Ryan10 Jun 02 '15 at 18:41
  • no one?any thing to say – Ryan10 Jun 03 '15 at 08:58
  • Looks like you may have a problem with your LogCat connection. it is not normal if you can see the ` Log.e("VS", "IOException")` but not `e.printStackTrace()`. Try changing you're log like this `Log.e("VS", "IOException message:" e.getMessage())` – Gomino Jun 03 '15 at 09:06
  • 06-03 05:52:27.930: E/AndroidRuntime(855): FATAL EXCEPTION: Thread-86 06-03 05:52:27.930: E/AndroidRuntime(855): java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord. 06-03 05:52:27.930: E/AndroidRuntime(855): at android.media.AudioRecord.startRecording(AudioRecord.java:517) 06-03 05:52:27.930: E/AndroidRuntime(855): at com.example.voicechatclient.MainActivity$3.run(MainActivity.java:119) – Ryan10 Jun 03 '15 at 09:54
  • Did you add the `` permission to the manifest file ? – Gomino Jun 03 '15 at 10:19

2 Answers2

0

You're question might have already been answer here : https://stackoverflow.com/a/5440517/795245

If you're permission are set correctly I think the problem is that your device does not support a 44100 sample rate, and then you recorder may have not been initialized correctly.

Add some sanity check before creating your AudioRecord object after checking the minBufferSize:

int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
if (minBufSize != AudioRecord.ERROR_BAD_VALUE) {
   // check if we can instantiate and have a success 
   AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, audioFormat, minBufSize);

   if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
       //Do whatever you want with the recorder
       recorder.startRecording();
       ...
   } 
}

You should try out other sample rate values : { 8000, 11025, 22050, 44100 };

Community
  • 1
  • 1
Gomino
  • 12,127
  • 4
  • 40
  • 49
  • Make sure you are not connecting to your socket from the main thread. – Gomino Jun 03 '15 at 18:52
  • solved. there is no Error in log cat.so what's the problem now? – Ryan10 Jun 03 '15 at 19:44
  • How can I tell you what to do without any input from you ? – Gomino Jun 04 '15 at 12:17
  • okay what do you need?Logcat?. do you think i enter the wrong ip address?how can i make sure about that? i'm useing hot spot software in my lap top for wifi server – Ryan10 Jun 04 '15 at 12:47
  • Maybe your thread still has an uncaught exception. Try to catch the "Exception" object also and add a printStackTrace. – Gomino Jun 04 '15 at 14:53
  • i have two side.server and client. the server side works normlay at emulator and on the phone. but client side dose not work on emulator but works on phone... i dout i ip address.mybe i put the wrong ip – Ryan10 Jun 04 '15 at 14:57
  • log cat for client:Could not get audio input for record source 1 initialization check failed Error code -20 when initializing native AudioRecord object. FATAL EXCEPTION: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord. android.media.AudioRecord.startRecording(AudioRecord.java:517) com.example.voicechatclient.MainActivity$3.run(MainActivity.java:132) java.lang.Thread.run(Thread.java:841) – Ryan10 Jun 04 '15 at 14:58
  • Again Hardware support is very limited on emulator, please don't try to access the mic from the emulator. So if I understand correctly it is working on real phones, right ? – Gomino Jun 04 '15 at 15:07
  • i found the problem!!!!!!! look i coded the java listener(server side) which listens on spceial port till client connects to it. do you know what happend? when i enterd the correct ip it connect to server but made an exception – Ryan10 Jun 04 '15 at 15:20
  • So is your question answered ? – Gomino Jun 04 '15 at 15:22
  • so now.i got client is working i mean connects to server. the main problem is ip address.i think i enter the wrong ip.or server side dose not work normaly.no my question still unanswered.look at the title of topic – Ryan10 Jun 04 '15 at 15:32
  • I'm loosing my time here. You got too many problem to start with. – Gomino Jun 04 '15 at 15:36
  • tnx you for smashing me. u helped me out with this.my problem is almost solved. tnx man – Ryan10 Jun 04 '15 at 16:52
  • Common man, I'm here to provide answers to questions, not to dev step by step to figure out what is actually the problem. regards – Gomino Jun 04 '15 at 17:29
0

if your problem is that

  • androidApp <-> wifi-wlan <-> androidApp works as expected and
  • androidApp <-> gprs-umts-internet <-> androidApp does not work

This is because nearly all gprs-umts-internet-serviceprovider block incoming connections.

In other words you cannot reach a server running on your phone from the internet. you have to have an internetserver where both androidApps connect to. maybe a vpn tunnel will also work.

k3b
  • 14,517
  • 7
  • 53
  • 85