Loop audio from mic to earpiece?

All your problems with Audio, Video and Images.

Loop audio from mic to earpiece?

Postby Draffodx » Thu Jan 21, 2010 12:33 pm

Hi all,

I was wondering is it possible with Android to record from the mic and
play the sound back out the earpiece with just a 1 second delay?

I have seen the tutorials and can currently record from the mike and
save a 3gp file to the sd card.

However is it posssible to record and say stream the audio straight to
the ear piece with just a 1 - 2 second delay?

If so how would I go about doing this?

Would I need to record from the mic and save it to a file and then
repaly to the ear piece? and continually do this?

Or would it be possible to record from the mic and stream it directly
to the ear piece with just a slight delay?

Any help, info or directions to tutorials would be greatly
appreciated,

Thanks in advance
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Top

Postby Sir^Knigget » Thu Jan 21, 2010 1:13 pm

Use AudioRecord, it allows you to read recorded sound directly into a buffer, so you can stream it to the earpiece using AudioTrack.
Sir^Knigget
Freshman
Freshman
 
Posts: 9
Joined: Wed Sep 30, 2009 6:00 pm

Postby Draffodx » Fri Jan 22, 2010 2:01 pm

Hi Sir^Knigget,

Thanks for that,

I have this at the minute:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class LoopProg extends Activity {
  2.  
  3.        
  4.  
  5.        
  6.  
  7.         boolean isRecording; //currently not used
  8.  
  9.         AudioManager am;
  10.  
  11.        
  12.  
  13.     /** Called when the activity is first created. */
  14.  
  15.     @Override
  16.  
  17.     public void onCreate(Bundle savedInstanceState) {
  18.  
  19.         super.onCreate(savedInstanceState);
  20.  
  21.         setContentView(R.layout.main);
  22.  
  23.         am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  24.  
  25.         Record record = new Record();  
  26.  
  27.         record.run();
  28.  
  29.        
  30.  
  31.     }
  32.  
  33.  
  34.  
  35.           public class Record extends Thread
  36.  
  37.           {
  38.  
  39.                  // SoundPower sndPower = new SoundPower();
  40.  
  41.  
  42.  
  43.                   static final int bufferSize = 200000;
  44.  
  45.                   final short[] buffer = new short[bufferSize];
  46.  
  47.                   short[] readBuffer = new short[bufferSize];
  48.  
  49.  
  50.  
  51.                   public void run() {  
  52.  
  53.                           isRecording = true;
  54.  
  55.                           android.os.Process.setThreadPriority
  56.  
  57.                           (android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
  58.  
  59.  
  60.  
  61.                           int buffersize = AudioRecord.getMinBufferSize(11025,
  62.  
  63.                           AudioFormat.CHANNEL_CONFIGURATION_MONO,
  64.  
  65.                           AudioFormat.ENCODING_PCM_16BIT);
  66.  
  67.  
  68.  
  69.                                          AudioRecord arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
  70.  
  71.                                                          11025,
  72.  
  73.                                                          AudioFormat.CHANNEL_CONFIGURATION_MONO,
  74.  
  75.                                                          AudioFormat.ENCODING_PCM_16BIT,
  76.  
  77.                                                          buffersize);
  78.  
  79.  
  80.  
  81.                                          AudioTrack atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
  82.  
  83.                                                          11025,
  84.  
  85.                                                          AudioFormat.CHANNEL_CONFIGURATION_MONO,
  86.  
  87.                                                          AudioFormat.ENCODING_PCM_16BIT,
  88.  
  89.                                                          buffersize,
  90.  
  91.                                                          AudioTrack.MODE_STREAM);
  92.  
  93.                                          
  94.  
  95.                                          
  96.  
  97.                                                                 am.setRouting(AudioManager.MODE_INVALID, (true ? AudioManager.ROUTE_EARPIECE : 0),
  98.  
  99.                                                          AudioManager.ROUTE_EARPIECE);
  100.  
  101.                                          
  102.  
  103.                                          am.getRouting(AudioManager.ROUTE_EARPIECE);
  104.  
  105.  
  106.  
  107.                                          setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
  108.  
  109.                                          am.setSpeakerphoneOn(true);
  110.  
  111.                                          Log.d("SPEAKERPHONE", "Is speakerphone on? : " + am.isSpeakerphoneOn());
  112.  
  113.                                          am.setSpeakerphoneOn(false);
  114.  
  115.                                          Log.d("SPEAKERPHONE", "Is speakerphone on? : " + am.isSpeakerphoneOn());
  116.  
  117.                                          atrack.setPlaybackRate(11025);
  118.  
  119.  
  120.  
  121.                                          byte[] buffer = new byte[buffersize];
  122.  
  123.                                          arec.startRecording();
  124.  
  125.                                          atrack.play();
  126.  
  127.  
  128.  
  129.                                          while(isRecording) {
  130.  
  131.                                                  arec.read(buffer, 0, buffersize);
  132.  
  133.                                                  atrack.write(buffer, 0, buffer.length);
  134.  
  135.                                          }
  136.  
  137.  
  138.  
  139.                                          arec.stop();
  140.  
  141.                                          atrack.stop();
  142.  
  143.                                          isRecording = false;
  144.  
  145.                   }
  146.  
  147.           }
  148.  
  149. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


It records and plays back fine, however it sends the audio to the speaker phone, is anybody aware of how to get the audio to send to the ear piece?
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Postby Draffodx » Mon Jan 25, 2010 1:34 pm

This piece of code works for routing the audio to the ear piece in 1.5

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. audio_service.setSpeakerphoneOn(false);
  2.  
  3. audio_service.setMode(AudioManager.MODE_IN_CALL);
  4.  
  5. audio_service.setRouting(AudioManager.MODE_NORMAL,
  6.  
  7. AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


However it doesn't work in 1.6 +

Has anyone any suggestions as to how to get it working in 1.6?
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Postby Draffodx » Tue Jan 26, 2010 1:28 pm

For anyone that comes across this I have it working in 1.6

using the audiomanager to set the speakerphone off in oncreate seems to work, I can now use it to switch between the earpiece and speaker
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Postby today913 » Wed Mar 03, 2010 8:10 am

Hi,

I am working in a project which has the similar problem.

First step, I would like to implement loopback that is the user can speak on phone, and hear his own voice on that phone.

Next, I would like to implement transmission of voice from the user's phone to another connected Bluetooth device(maybe another cell phone) via Bluetooth.

How can I implement the voice transmission via Bluetooth, could you please give me some advices?

Appreciate you all!!
today913
Freshman
Freshman
 
Posts: 5
Joined: Sun May 31, 2009 4:25 am

Top

Postby today913 » Wed Mar 03, 2010 9:21 am

Draffodx wrote:For anyone that comes across this I have it working in 1.6

using the audiomanager to set the speakerphone off in oncreate seems to work, I can now use it to switch between the earpiece and speaker


Hi Draffodx,

How can I switch vioce between the earpiece and speaker? Can you help me? I think it's very useful for me, Thank you very much!
today913
Freshman
Freshman
 
Posts: 5
Joined: Sun May 31, 2009 4:25 am

REQ: Play audio file to callee from caller in android

Postby golemnagesh » Fri Feb 04, 2011 12:15 pm

Hello All,

Please help me out how to play a song to callee from caller when call is in running, when call gets connected if callee lift the call automatically song should play to callee, and call should be disconnected when playing done.
golemnagesh
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Jan 12, 2011 6:55 am

Re: Loop audio from mic to earpiece?

Postby shai » Mon Feb 14, 2011 8:50 pm

I've done it some time ago, check out HearPlugs on the Android Market.

The tips you got here are fine, if you didn't solve this just contact me and I will help you
shai
Freshman
Freshman
 
Posts: 2
Joined: Mon Jun 21, 2010 9:52 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Exabot [Bot] and 6 guests