Ryan110 Ryan110 - 4 months ago 26
Android Question

How to receive from datagram socket

I'm developing an App it's like voice chat client connects to server and then voice chat begins.

But server side has problem. The problem is receiving method of datagram socket.

socket.receive();

block waiting for input.

server side :

private int channelConfig = AudioFormat.CHANNEL_OUT_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
AudioRecord recorder;

private boolean status = true;



@SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.GINGERBREAD) @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}


receiveButton = (Button) findViewById (R.id.receive_button);
stopButton = (Button) findViewById (R.id.stop_button);
receive_label= (TextView) findViewById(R.id.receive_label);
receiveButton.setOnClickListener(receiveListener);
stopButton.setOnClickListener(stopListener);
port=(EditText) findViewById(R.id.editText1);
button= (Button) findViewById(R.id.button1);
button.setOnClickListener(close);
//AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
// sampleRate =Integer.parseInt( audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE));
}

private final OnClickListener close = new OnClickListener() {

@Override
public void onClick(View arg0) {
System.exit(0);
}

};



private final OnClickListener stopListener = new OnClickListener() {

@Override
public void onClick(View v) {
status = false;
speaker.release();
Log.d("VR","Speaker released.s");

}

};


private final OnClickListener receiveListener = new OnClickListener() {

@Override
public void onClick(View arg0) {
status = true;

receive_label.setText("socket...2");
startReceiving();

}

};

public void startReceiving() {


final Thread receiveThread = new Thread (new Runnable() {



@Override
public void run() {



try {

int minBufSize =4096;//recorder.getMinBufferSize(sampleRate,channelConfig,audioFormat);


Log.d("VR", ""+channelConfig+" "+audioFormat+sampleRate);

DatagramSocket socket = new DatagramSocket(50005);
Log.d("VR", "Socket Created.s");


byte[] buffer = new byte[minBufSize+=4096];



// for (int sampleRate : new int[] {44100,8000, 11025, 16000 }) { // add the rates you wish to check against




Log.d("bufersize", "bufer size :"+minBufSize);
Log.d("bufersize", "bufer size :"+sampleRate);
if (minBufSize != AudioRecord.ERROR_BAD_VALUE) {



speaker = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,channelConfig,audioFormat,minBufSize*=10,AudioTrack.MODE_STREAM);


speaker.play();
Log.d("VR", "spekaer playing...");
}
// }


//minimum buffer size. need to be careful. might cause problems. try setting manually if any problems faced
// int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
Log.d("VR", ""+status);

while(status == true) {

DatagramPacket packet = new DatagramPacket(buffer,buffer.length);

socket.receive(packet);


Log.d("VR", "Packet Received.s");

//reading content from packet
buffer=packet.getData();
Log.d("VR", "Packet data read into buffer.s");


//sending data to the Audiotrack obj i.e. speaker
speaker.write(buffer, 0, minBufSize);
Log.d("VR", "Writing buffer content to speaker.s");
}

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




}


});

receiveThread.start();
}}


client side :

@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") public class MainActivity extends Activity {

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

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

//AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

private int sampleRate =8000;//=Integer.parseInt(audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE));
private int channelConfig = AudioFormat.CHANNEL_IN_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

private boolean status = true;
String size;


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

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}


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");
button= (Button) findViewById(R.id.button1);
startButton.setOnClickListener (startListener);
stopButton.setOnClickListener (stopListener);
button.setOnClickListener(close);

target.setText("10.0.2.15");
target_port.setText("50005");

AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
// sampleRate = Integer.parseInt(audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE));
size = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
// Log.d("Buffer Size and sample rate", "Size :" + size + " & Rate: " + rate);
}

private final OnClickListener close = new OnClickListener() {

@Override
public void onClick(View arg0) {
System.exit(0);
}

};



private final OnClickListener stopListener = new OnClickListener() {

@Override
public void onClick(View arg0) {
status = false;
recorder.release();
Log.d("VS","Recorder released.c");
}

};




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.c");

byte[] buffer = new byte[minBufSize+=4096];

Log.d("VS","Buffer created of size .c" + minBufSize);
DatagramPacket packet;

final InetAddress destination = InetAddress.getByName(target.getText().toString());
Log.d("VS", "Address retrieved.c");
Log.d("VS", size+"");
if (minBufSize != AudioRecord.ERROR_BAD_VALUE) {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize*=10);
Log.d("VS", "Recorder initialized.c");


if (recorder.getState() == AudioRecord.STATE_INITIALIZED)

recorder.startRecording();}


while(status == true) {


//reading data from MIC into buffer
minBufSize = recorder.read(buffer, 0, buffer.length);

//encoding to base64
// String buffer1= Base64.encodeToString(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) {
e.printStackTrace();
} catch (IOException e) {
Log.e("IOException message:",e.getMessage().toString());


}


}

});
streamThread.start();
}


}


please give me your best help

Thanks in advance.!

Answer

send and recieve

BufferedWriter input;
                while(status == true) {
                  int  bufferReadResult = recorder.read(buffer, 0, buffer.length);
                      dos.write(buffer,0,bufferReadResult);
                  dos.flush();

                }
Comments