简体   繁体   中英

Android recording and playing +speakerphone as an option

I am programming a sip application for Android. During a call I must record the microphone input and play the incoming audio. There must be an optional using of speakerphone. It must be Android 1.5+ (1.5, 1.6, 2.0, 2.1, 2.2, future versions) compatible and device portable.

OK. So I am using AudioTrack to play incoming audio, AudioRecord to record data from the microphone and AudioManager.setSpeakerphoneOn() to enable or disable speakerphone.

Sounds simple but it is not as simple as it should be. AudioManager.setSpeakerphoneOn(false) does not work unless AudioManager.setMode(AudioManager.MODE_IN_CALL) is called. So I have to be in MODE_IN_CALL.

Still OK and simple and it works on my G1 with Android 1.6, older Samsung phone, emulators, many our customer devices etc. But it does not work everywhere :(

On Samsung tablet GT-P1000 I get stream of 0, 0, 0, 0, ... as microphone input. I think the same problem is on Motorola phones (customers complain). After some test I realised that it is caused by AudioManager.setMode(AudioManager.MODE_IN_CALL). This causes that microphone does not work on some devices. But I have to call it because otherwise I can not disable speakerphone.

Is there a simple way how to make it simple, correct and working? And if possible without hell like Sipdroid highly dangerous source code:

if (RtpStreamReceiver.samsung) {
    AudioManager am = (AudioManager) Receiver.mContext.getSystemService(Context.AUDIO_SERVICE);
    am.setMode(AudioManager.MODE_IN_CALL);
    am.setMode(AudioManager.MODE_NORMAL);
}

or

 void initMode() {
     samsung = Build.MODEL.contains("SAMSUNG") || Build.MODEL.contains("SPH-") ||
     Build.MODEL.contains("SGH-") || Build.MODEL.contains("GT-");

     if (Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL &&
     (Receiver.pstn_state == null || Receiver.pstn_state.equals("IDLE"))) {
       setMode(AudioManager.MODE_NORMAL); 
 }

Thanks a lot Jan

Try it out........

AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM