How to detect if a MIC ONLY is connected - android

My app plays and records wildlife sounds. It can do both either using external or internal hardware.
Yet, if I have a MIC with MIC ONLY functionality plugged, my app tries to play sound through it, like if it were a headset (with speakers).
How can my app decide that there is a MIC ONLY thing plugged, and ask it to play sound through either bluetooth or external speakers? I don't want a BroadcastReceiver thing necessarily, since the MIC may be plugged before I start the app.
In my phone (Android 12 Galaxy Note 10 lite), when I plug such a MIC, it shows as "Media output" a "Headset", despite the MIC has no earbuds/speaker. Yet, if the phone rings, it plays the sound through the phone speakers, as it should. So the phone knows something that I don't.
What I did was to add this code to have an idea of the mics plugged:
List<MicrophoneInfo> allmics = mAudioManager.getMicrophones();
for (MicrophoneInfo mic : allmics) {
if(mic.getType() == 3)
mAudioManager.setSpeakerphoneOn(true);
android.util.Log.e("########### AVE",mic.getType()+" "+mic.getDescription()+" "+mic.getLocation()+" "+mic.getId());
}
And this is what I got in the LogCat:
########### AVE: 15 builtin_mic1 0 9
########### AVE: 15 builtin_mic2 0 11
########### AVE: 25 15 3 15
########### AVE: 3 h2w12509 3 12509
The first two are the builtin mics of course. I have no idea what the third one is (TYPE_REMOTE_SUBMIX), but it seems some sistem thing. The forth one is my external MIC ONLY. Yet, its type (3) is TYPE_WIRED_HEADSET. So my app thinks it is a headset, which is not.
As you can see, I tried with
mAudioManager.setSpeakerphoneOn(true);
in the above loop if a TYPE_WIRED_HEADSET is detected in the above loop, without luck: still no sound. I have <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> in the manifest.
Yet, if I also add
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
as suggested here, it does work. mAudioManager.setMode(AudioManager.MODE_NORMAL) does not...
Is this the only solution?

Related

How to reset the audio system?

I am using the logic from Android : Switching audio between Bluetooth and Phone Speaker is inconsistent to switch output between earpiece, speaker, and bluetooth. Here is the code:
//For BT
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
//For phone ear piece
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.stopBluetoothSco();
mAudioManager.setBluetoothScoOn(false);
mAudioManager.setSpeakerphoneOn(false);
//For phone speaker(loadspeaker)
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mAudioManager.stopBluetoothSco();
mAudioManager.setBluetoothScoOn(false);
mAudioManager.setSpeakerphoneOn(true);
This logic works as expected as long as no other application has used audio (media player, phone, etc.). If I run my application after running the other application, the logic goes haywire. The audio still works except the destination output is not what is expected. For example, instead of speakerphone, the output may go to earpiece.
Note that I run my application after quitting from the other application. There is no overlap.
It appears audio manager settings are not specific to an application and may mess up other applications. I am wondering if there is a way to reset audio manager before setting my mode. Regards.

How to enable android bluetooth a2dp sink and play music on android L

we recently work on a audio box which based on Android, as a audio box must act to a reciver with bluetooth(while a2dp sink profile in bluetooth can do this), but we have do many works over weeks on it , but it still not work, so i'd like to ask somebody who know or had similar expirence on it could help.
our works :
1、packages/apps/Bluetooth/res/values/config.xml
<bool name="profile_supported_a2dp_sink">true</bool>
<bool name="profile_supported_hfpclient">true</bool>
2、external/bluetooth/bluedroid/include/bt_target.h
#define BTA_AV_SINK_INCLUDED TRUE
after these bluetooth seems to worked , i can user a phone link to these box and i can play music with a player through bluetooth , and also i can see the data packages in the box by tcpdump, but there is no sound , we also find some other informations :
Android device as a receiver for A2DP profile
but it seems didn't work.
so anybody could help.
thanks a lot

Low volume in call recording (Rx)

I do the following test:
Call from android app phone to home phone, first speak from (android phone - > home phone), in the same call speak from (home phone-> android phone).
The voice (android phone - > home phone) sounds in good volume (10/10 volume). But the voice (home phone-> android phone) sounds very low (4/10 volume).
I do the same test with random call recording app in play store The voice (android phone - > home phone) sounds in good volume (10/10 volume). The voice (home phone-> android phone) sounds in good volume (10/10 volume)
I try to call record which:
1)
audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
2)
audioRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION)
3)
audioRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL)
With 1, 2 o 3 results are very similar.
I also try to up VOICE_CALL stream if AudioSource.VOICE_CALL is used like this:
audioManager manager = (AudioManager) ServiceClass
.getServiceContext()
.getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
manager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL),
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
Does the application I have tried in the Play Store process the audio in some way to turn up the volume?
I don´t show here audio quality set code because in both test (my app test, play store app test) the audio (android phone - > home phone) sounds exactly the same, the problem is (home phone-> android phone)
I would try adjusting mic sensitivity in your code.
Heres a link to another stackoverflow quesiton being answered to do this.
How to adjust microphone sensitivity while recording audio in android - Solved

Android audio automatically getting redirected to earphone

In my Android app, the user gets to specifically select the audio output between internal speakers and earphone. Here is my routine to turn the speaker phone on:
static void useSpeaker(Context ctx) {
AudioManager am = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_NORMAL);
am.setSpeakerphoneOn(true);
}
Once the audio is set to speakers, even if you plug in the earphone, the output still goes through the speaker. I have verified on a number of different tablets and phones that this logic works.
However, on one device, once the earphone is plugged in, the audio output automatically switches from speakers to earphone.
I am wondering if there is something else that I need to take care of in the code. Or, is it just this device ignoring my directive? Regards.

Android - Getting audio to play through speakers

I would like to play a mp3 audio file with the same speaker and volume when an user receive a phone call.
I am using the following code
int result = audioManager.requestAudioFocus(afChangeListener,
AudioManager.MODE_IN_CALL,
AudioManager.AUDIOFOCUS_GAIN);
audioManager.setSpeakerphoneOn(true);
with the
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
in the manifest
but it does not work. the volume is very high.
Any idea?
What that call to setSpeakerPhoneOn (most likely) will do is to route both the music and the voice call to the loudspeaker, in which case they might be mixed together and the music may be downsampled to the voice call sample rate (8 or 16 kHz).
One thing you could try is what I proposed in how to turn speaker on/off programatically in android 4.0
This could let you let you route only the music to the loudspeaker while voice call audio is routed to the earpiece. It's not guaranteed to work on all devices though.

Categories

Resources