8

Is there a way to launch an audio file when answering a call to be played NOT into the call (so the other side could hear), but only in the call speaker (so only our side could hear).

Sounds strange, I know but it is part of a much larger app.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
CraigInDallas
  • 227
  • 4
  • 6
  • 1
    Hello I have develop following application, It does same thing, check this application if it solve your problem then I can send you the source code. https://market.android.com/details?id=com.devindia.acr&feature=search_result – Ketan Parmar Jun 18 '11 at 12:54
  • @KPBird, this looks like it may do the trick. I would appreciate if you did send the code for us to review. Thanks for the offer. – CraigInDallas Jun 18 '11 at 13:56
  • Hello I will upload code today... – Ketan Parmar Jun 21 '11 at 07:45
  • @KPBird Hello I want to do the same task in my application. Can you tell me how to implement this one. Actually my need is "Suppose we have recorded file in our mobile,when we call to any person,as soon as he picked up the call he should listen recorded voice file,which we have recorded". Please replay me as soon as possible. Thank you – Ramakrishna Jan 04 '12 at 14:24
  • @KPBird : I would appreciate if you provide some hint of logic and share your code :) – loks Apr 25 '12 at 12:22
  • @KPBird: Seems like a popular request as I'd like to know where you uploaded too as well. Perhaps you should open-source it (or start charging for it =) – brianestey Aug 15 '12 at 02:04

2 Answers2

1

First of all, you'll need to set up a BroadcastReceiver (let's call it "CallReceiver"), and permission to know about the phone state (intuitively, the permission to add is android.permission.READ_PHONE_STATE).

Register your CallReceiver action like this.

<receiver android:name=".CallReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE"></action>
    </intent-filter>
</receiver>

At your CallReceiver, you may decide upon which actions should your audio play back (incoming/outcoming/phone ringing...), so just read the EXTRA_STATE, and getCallState() (check out the TelephonyManager docs).

About the audio, you will need to use the AudioManager, and set the "in call" mode of playback before playing the sound.

private AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);  
am.setMode(AudioManager.MODE_IN_CALL); 
am.setSpeakerphoneOn(false);

I hope this helps!

Alberto Rico
  • 397
  • 1
  • 4
  • 21
0
  private void PlayShortAudioFileViaAudioTrack(String filePath) throws IOException {
 // We keep temporarily filePath globally as we have only two sample sounds now..
         if (filePath == null)
             return;

 //Reading the file..
         byte[] byteData = null;
         File file = null;
         file = new File(filePath); // for ex. path= "/sdcard/samplesound.pcm" or "/sdcard/samplesound.wav"
         byteData = new byte[(int) file.length()];
         FileInputStream in = null;
         try {
             in = new FileInputStream(file);
             in.read(byteData);
             in.close();

         } catch (FileNotFoundException e) {
 // TODO Auto-generated catch block
             e.printStackTrace();
         }
 // Set and push to audio track..
         int intSize = android.media.AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                 AudioFormat.ENCODING_PCM_8BIT);
         AudioTrack at = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                 AudioFormat.ENCODING_PCM_8BIT, intSize, AudioTrack.MODE_STREAM);
         if (at != null) {
             at.play();
 // Write the byte array to the track
             at.write(byteData, 0, byteData.length);
             at.stop();
             at.release();
         } else
             Log.d("TCAudio", "audio track is not initialised ");
Akshay Shah
  • 748
  • 1
  • 8
  • 23