views:

1884

answers:

4

I currently have code that reads a recording in from the devices mic using the AudioRecord class and then playing it back out using the AudioTrack class.

My problem is that when I play it out it plays vis the speaker phone.

I want it to play out via the ear piece on the device.

Here is my code:

public class LoopProg extends Activity {


 boolean isRecording; //currently not used
 AudioManager am;
 int count = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        am.setMicrophoneMute(true);
        while(count <= 1000000){
        Record record = new Record();  
        record.run();
        count ++;
        Log.d("COUNT", "Count is : " + count);
        }
    } 

   public class Record extends Thread
   {

           static final int bufferSize = 200000;
           final short[] buffer = new short[bufferSize];
           short[] readBuffer = new short[bufferSize];

           public void run() {  
            isRecording = true;
            android.os.Process.setThreadPriority
            (android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

            int buffersize = AudioRecord.getMinBufferSize(11025,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);

                           AudioRecord arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
                                           11025,
                                           AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                           AudioFormat.ENCODING_PCM_16BIT,
                                           buffersize);

                           AudioTrack atrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                                           11025,
                                           AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                           AudioFormat.ENCODING_PCM_16BIT,
                                           buffersize,
                                           AudioTrack.MODE_STREAM);


        am.setRouting(AudioManager.MODE_NORMAL,1,
                             AudioManager.STREAM_MUSIC);


                           int ok = am.getRouting(AudioManager.ROUTE_EARPIECE);
                           Log.d("ROUTING", "getRouting = " + ok);
                           setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
                           //am.setSpeakerphoneOn(true);
                           Log.d("SPEAKERPHONE", "Is speakerphone on? : " + am.isSpeakerphoneOn());
                           am.setSpeakerphoneOn(false);
                           Log.d("SPEAKERPHONE", "Is speakerphone on? : " + am.isSpeakerphoneOn());
                           atrack.setPlaybackRate(11025);

                           byte[] buffer = new byte[buffersize];
                           arec.startRecording();
                           atrack.play();

                           while(isRecording) {
                                   arec.read(buffer, 0, buffersize);
                                   atrack.write(buffer, 0, buffer.length);
                           }

                           arec.stop();
                           atrack.stop();
                           isRecording = false;
           }
   } 
}

As you can see if the code I have tried using the AudioManager class and its methods including the deprecated setRouting method and nothing works, the setSpeatPoneOn method seems to have no effect at all, neither does the routing method.

Has anyone got any ideas on how to get it to play via the earpiece instead of the spaker phone?

+1  A: 

There was some related discussion in this recent question:
http://stackoverflow.com/questions/1993471/android-can-i-mute-currently-playing-audio-applications

Based on the AudioManager source code, it seems that you must be in "call mode" before the setSpeakerphoneOn method has any effect.

However, I recently saw an application that could seamlessly switch between earpiece and speakerphone while still showing the current stream as the "media" stream, so I would be interested in any further answers.

Christopher
Ahh, so I think the device I saw this working on was running 1.5. Unfortunately the AudioManager APIs seem to be often changing between releases. The routing and speakerphone stuff was definitely one area that changed between 1.5 and 1.6. Which sucks. :(
Christopher
Hi Christopher, I currently use this code to do it and it works in 1.5 audio_service.setSpeakerphoneOn(false); audio_service.setMode(AudioManager.MODE_IN_CALL); audio_service.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); However its broken from 1.6 on and I cant find the new solution. I have tried setting the SpeakerPhoneOn method to false but it still places out the speaker, I have tried changing all the streams around and still no luck
Donal Rafferty
Yes,it seems to be fairly broken from 1.6 on which is fairly annoying at the minute, most new devices are using a higher version than 1.5!
Donal Rafferty
A: 

If the ear piece is connected to the phone with bluetooth (which I assume it is), have you tried calling AudioManager.setBluetoothScoOn(true)?

I've been through the Android reference and this is the only thing I could find that you didn't mention trying.

Bloodsplatter
It doesn't sound like he's using a Bluetooth headset. The earpiece is the built-in speaker you have your ear against during phone calls.
Christopher
Yep as christopher says I'm trying to route the audio through the ear piece that is on the device itself, where people listen to for normal phone calls.I can use the setRouting method and it works in 1.5, however I cant find a solution to routing the audio to the ear piece from 1.6 on
Donal Rafferty
A: 

I appear to have got it working on 1.6.

So I said I'd post here how I done it.

To get it working in 1.6 I:

Used the AudioManager class to use the setSpeakerPhoneOn() to false, I then used the Voice_Call_Stream and add volueme control to the Voice_Call_Stream.

The setSpeakerPhoneOn(false) method is used in onCreate() of the activity and this appears to route to the headset, I then used a button and used the setSpeakerPhoneOn(true) method and the audio gets routed to the speaker.

The method only appears to work when it is used in onCreate() for me and I haven't tested it extensively but for the moment it allows me to switch between headset and speaker on a 1.6 device

Donal Rafferty
A: 
public MediaPlayer m_mpSpeakerPlayer;

private AudioManager m_amAudioManager;

m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

// 從Receiver Earpiece發音

m_amAudioManager.setSpeakerphoneOn(false);

m_amAudioManager.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); 

Log.i(TAG, String.valueOf(m_amAudioManager.getRouting(AudioManager.ROUTE_EARPIECE))); 

setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);

// 把聲音設定成從Earpiece出來
// 重點在這行,要把它設定為正在通話中
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);

// 開始放音樂
m_mpSpeakerPlayer.reset();

m_mpSpeakerPlayer.setDataSource("sdcard/receiver.mp3");

m_mpSpeakerPlayer.prepare();

m_mpSpeakerPlayer.start();

//最後再把它設定從Speaker放音,達成!

m_amAudioManager.setMode(AudioManager.MODE_NORMAL);
Achigo