Ryan10
Ryan10

Reputation: 126

No connection between devices?

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 ....

Upvotes: 0

Views: 78

Answers (2)

k3b
k3b

Reputation: 14755

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.

Upvotes: 0

Gomino
Gomino

Reputation: 12347

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 };

Upvotes: 0

Related Questions