Walkies Talkie in Android (Real time not achieved)





Download Full Source code from GitHub Walkie-Talkie


Overview

This application is being developed of two users only but if you want it can be extend for almost 20 users.Now the MainActivity.java file is the core of all the operation in which both the client service and the server service have been defined and in the resource folder you are able to find the UI of the application.
In case of UI it can only one ImageButton ( Basically used in places where  you required  clickable images so that it can perform every function of simple buttons). Its source it being linked to a drawable folder in which selector is being used such that you can differentiate between if you are sending the voice or receiving the voice. In case of sending the voice the image color is being changed to green and do nothing which remain in its original color.


Whereas in case of the source code there are three main operation which are being performed one is the server side performance and other one is client side and the last part which is recording or playing the audio.In this particular application you the IP address of the receiving machine or sending machine i.e to which phone are you sending the audio . You can find in the program the intialization of  the clientRxThread AsynTask Class here you have specify the address.



clientRxThread =new ClientRxThread("ip address of the sending machine"
, SocketServerPORT);
clientRxThread.execute();
}

How is the touch works it is obtain by using the OnTouchListener which is attached to button in the onCreate method when the button is touched down the the MotionEvent.Action_Down is envocked which start the function startRecording and then invoke the Asyntask named client RxThread to send the recorded file to the other user. After the invoke and complete operation it then start acting as a server to receive file from the other user or send file once again.

















private OnTouchListener mTalkTouch = new OnTouchListener() {
public boolean onTouch(View yourButton, MotionEvent motion) {  
switch (motion.getAction()) {  
case MotionEvent.ACTION_DOWN:  
yourButton.setPressed(true);  
try { clientRxThread.cancel(true);  
startRecording();  
}
catch (IllegalStateException e)
{  
e.printStackTrace();

catch (IOException e) {
e.printStackTrace();  

break; 
case MotionEvent.ACTION_UP: 
yourButton.setPressed(false);  
try {  
stopRecording(); 
serverSocketThread = new ServerSocketThread();
serverSocketThread.execute();  
} catch (IllegalArgumentException e) { 
e.printStackTrace();  
} catch (IllegalStateException e) {  
e.printStackTrace();  
} catch (IOException e) {  
e.printStackTrace(); 
}  
break;}
return true; }};


When the Application start you find the image button if you pressed the image button then this startRecording() starts in which it records the audio file save it in phone storage where the other function stopRecording() is being called when you remove your hand from the image button .It actually is like the real life walkie in when pressed then u can speak and the others are the receiver listening to your voice.











private void startRecording() throws IOException { 
Toast.makeText(getApplicationContext(), "Recording...", 
Toast.LENGTH_SHORT).show();
mFileOut = new FileOutputStream(mFileName);
mFD = mFileOut.getFD();
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
mRecorder.setOutputFile(mFD);
mRecorder.prepare();
mRecorder.start();  
} private void stopRecording() throws IOException 
{  
Toast.makeText(getApplicationContext(), "Stopping...", 
Toast.LENGTH_SHORT).show();
mRecorder.stop(); 
mRecorder.release(); 
mRecorder = null; 
mFileOut.close();  
mFileOut = null; } };


ServerSocketThread is the AsynTask class in which it accepts the audio file and plays the received audio is done by using the Class MediaPlayer in which the location of the file is being passed and then prepare the audio and play it.



FileInputStream fileIn; try { fileIn = new FileInputStream(file);
mFD = fileIn.getFD(); 
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(mFD);
mPlayer.prepare();
mPlayer.start();

Before closing of your application you have to close all the port otherwise it will continue to receive till your application is connected to a network.For that this piece of code is written it close all the port and socked through with transmission has been made






protected void onDestroy() {
super.onDestroy(); 
if (serverSocket != null) {
try {  
serverSocket.close(); 
} catch (IOException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
}  
}  
}

Comments

Popular posts from this blog

Making Bootable Pendrive with .bat file

Ek Beej Tha Gaya Bahut Hee Gaharaee Mein Boya - Nanha Paudha

R-W-W (Real, Win, Worth it ) - Boost your idea