Download Source
Step 1
Create MainActivity class for audio record and play
package com.javaorigin.audio; import android.media.AudioFormat; import android.media.AudioManager; import android.media.AudioRecord; import android.media.AudioTrack; import android.media.MediaRecorder; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { AudioManager am = null; AudioRecord record =null; AudioTrack track =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setVolumeControlStream(AudioManager.MODE_IN_COMMUNICATION); init(); (new Thread() { @Override public void run() { recordAndPlay(); } }).start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } private void init() { int min = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, min); int maxJitter = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT); track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM); } private void recordAndPlay() { short[] lin = new short[1024]; int num = 0; am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); am.setMode(AudioManager.MODE_IN_COMMUNICATION); record.startRecording(); track.play(); while (true) { num = record.read(lin, 0, 1024); track.write(lin, 0, num); } } boolean isSpeaker = false; public void modeChange(View view) { Button modeBtn=(Button) findViewById(R.id.modeBtn); if (isSpeaker == true) { am.setSpeakerphoneOn(false); isSpeaker = false; modeBtn.setText("Call Mode"); } else { am.setSpeakerphoneOn(true); isSpeaker = true; modeBtn.setText("Speaker Mode"); } } boolean isPlaying=true; public void play(View view){ Button playBtn=(Button) findViewById(R.id.playBtn); if(isPlaying){ record.stop(); track.pause(); isPlaying=false; playBtn.setText("Play"); }else{ record.startRecording(); track.play(); isPlaying=true; playBtn.setText("Pause"); } } }Step 2
Add Button for Play/Pause and Call mode/ Speaker mode
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/modeBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="24dp" android:text="Call Mode" android:onClick="modeChange"/> <Button android:id="@+id/playBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/modeBtn" android:layout_alignBottom="@+id/modeBtn" android:layout_marginLeft="30dp" android:layout_toRightOf="@+id/modeBtn" android:onClick="play" android:text="Pause" /> </RelativeLayout>
Step 3
Add audio related permission to Android.xml
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission> <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
how it works?
ReplyDeleteWorks great
ReplyDeletepls upload screen shots of app as well with your source code, thnks for this code ..
ReplyDeleteWorks Great.......
ReplyDeleteBut I want to give delay in replying the sound ....for that I have done maxJitter*19 . and it worked
But I have increased the audiotrack frequency i.e.
track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 9000, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM);
after giving 9000 frequency it gives me delay for sometimes only and after that it immediately replies to my voice which I really don't want ....
Plz Help me in this...
Thanks in advance
short[] lin = new short[1024];
ReplyDeleteint num = 0;
I wonder why lin.length() = 1024??
how do you have this num?
int min = AudioRecord.getMinBufferSize(8000,
ReplyDeleteAudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION,
8000, AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT, min);
int maxJitter = AudioTrack.getMinBufferSize(8000,
AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT);
track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 8000,
AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
maxJitter, AudioTrack.MODE_STREAM);
when i change AudioFormat.CHANNEL_OUT_MONO to AudioFormat.CHANNEL_OUT_STEREO, it can't work?!!
Some one help me plz!!
thanks
This comment has been removed by the author.
ReplyDeleteHow to add resource mp3 file to AudioTrack
ReplyDeletehow it works?
ReplyDeletehi frnds,
ReplyDeletei want to record my incoming and out going calls in my phone
can you send me code for this
Thanks
hi frnds,
ReplyDeletei want to record my incoming and out going calls in my phone
can you send me code for this
Thanks
This was an nice and amazing and the given contents were very useful and the precision given here is good.
ReplyDeletejava training in chennai
java training in bangalore
java online training
java training in pune
Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.python training in chennai
ReplyDeletepython training in bangalore
python online training
python training in pune
Fantastic work! This is the type of information that should follow collective approximately the web. Embarrassment captivating position Google for not positioning this transmit higher! Enlarge taking place greater than and visit my web situate
ReplyDeleteData Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in OMR
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore
Data Science training in marathahalli
Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision.
ReplyDeleterpa training in Chennai
rpa training in Chennai
rpa training in Chennai
rpa training in velachery
rpa training in tambaram
rpa training in sholinganallur
rpa training in anna nagar
rpa online training
After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience. Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in
ReplyDeletepython training in rajajinagar
Python training in btm
Python training in usa
This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me..
ReplyDeleteDevOps online Training
DevOps Training in USA
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
ReplyDeleteBlueprism training in Chennai
Blueprism training in Bangalore
Amazon Web Services (AWS) is the most popular and most widely used Infrastructure as a Service (IaaS) cloud in the world.AWS has four core feature buckets—Compute, Storage & Content Delivery, Databases, and Networking. At a high level, you can control all of these with extensive administrative controls accessible via a secure Web client.For more information visit aws online
ReplyDeletetraining
Nice Blog...Thanks for Sharing!!!
ReplyDeleteJava Training in Chennai
Python Training in Chennai
IOT Training in Chennai
Selenium Training in Chennai
Data Science Training in Chennai
FSD Training in Chennai
MEAN Stack Training in Chennai
Amazing blog! Your post concept is very comprehensive. It was very helpful for develop my knowledge. Thanks to you....
ReplyDeletePHP Training Institute in Bangalore
PHP Classes in Bangalore
PHP Course in Chennai
PHP Course in Mogappair
PHP Training in Saidapet
PHP Training in Chennai Velachery
PHP Training in Kandanchavadi
PHP Course in Sholinganallur
I think things like this are really interesting. I absolutely love to find unique places like this. It really looks super creepy though!!
ReplyDeletemachine learning classroom training in chennai
python machine learning training in chennai
machine learning training in velachery
top institutes for machine learning in chennai
The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea.
ReplyDeletehere by i also want to share this.
data science online training
python online training
uipath online training
data science with python online training
rpa online training
Android Interview Questions and Answers
ReplyDeleteThis was an nice and amazing and the given contents were very useful and the precision given here is good.
ReplyDeleteTraining
nice post
ReplyDeletebest python training in chennai
best python training in sholinganallur
best python training institute in omr
python training in omr
selenium training in chennai
selenium training in omr
selenium training in sholinganallur
Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
ReplyDeletepython training in bangalore
hello guys... i get an error "uses or overrides a deprecated api"...i used API 23 to run this code...on executing i got an error "Your app isn't working " ...someone help me !!
ReplyDeleteGuys I m getting an error ‘unfortunately your app has stopped ‘ plz help me
ReplyDeleteI learned World's Trending Technology from certified experts for free of cost. I Got a job in decent Top MNC Company with handsome 14 LPA salary, I have learned the World's Trending Technology from Data science training in btm layout experts who know advanced concepts which can help to solve any type of Real-time issues in the field of Python. Really worth trying hkbk group of institutions
ReplyDeleteThanks for the tutorial, it helps me lot, thanks for the code
ReplyDeleteYou given a nice post Cognos tm1 online training
ReplyDeleteYou shared a very nice and interesting post ethical hacking certification
ReplyDeletethanks for your blog ba online training hyderabad
ReplyDeleteVery interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.python training in bangalore
ReplyDeleteThese provided information was really so nice,thanks for giving that post and the more skills to develop after refer that post.salesforce developer training in bangalore
ReplyDeleteBeing new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving.servicenow training in bangalore
ReplyDeleteReally it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.informatica training in bangalore
ReplyDeleteReally it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.devops Training in Bangalore
ReplyDeleteThis comment has been removed by the author.
ReplyDeletesuper...!
ReplyDeleteinternship in chennai for ece students
internships in chennai for cse students 2019
Inplant training in chennai
internship for eee students
free internship in chennai
eee internship in chennai
internship for ece students in chennai
inplant training in bangalore for cse
inplant training in bangalore
ccna training in chennai
Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
ReplyDeleteservicenow online training
top servicenow online training
best servicenow online training
ReplyDeleteClass College Education training Beauty teaching university academy lesson teacher master student spa manager skin care learn eyelash extensions tattoo spray
daythammynet
daythammynet
daythammynet
daythammynet
daythammynet
daythammynet
daythammynet
daythammynet
daythammynet
Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing. sharepoint developer training.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks for sharing such a great information..Its really nice and informative..
ReplyDeleteazure online training
Effective blog with a lot of information. I just Shared you the link below for ACTE .They really provide good level of training and Placement,I just Had Microsoft Azure Classes in ACTE , Just Check This Link You can get it more information about the Microsoft Azure course.
ReplyDeleteJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
ReplyDeleteThis is most informative and also this post most user friendly and super navigation to all posts. Thank you so much for giving this information to me. Informative training in Chennai.
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
Hey guy's i have got something to share from my research work
ReplyDeleteCoderefinery
Tukui
Lakedrops
Thanks for your blog...
ReplyDelete2020 data mining projcts chennai
2020 neural networks projects chennai
mobile computing projects chennai
This is perfect contentpython institute
ReplyDeleteThis post is so interactive and informative.keep update more information...
ReplyDeleteDigital Marketing Course in Tambaram
Digital Marketing Course in Chennai
best mobile crm for small business
ReplyDelete