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.
Related
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?
I am trying to record both Uplink and Downlink voice using Android. Regardless the law and everything, i am already aware, so please do not put comments related to the law.
The code below works fine, except when i mute the microphone, it wont record the downlink voice.
I am using Android 8.1. I've tried using a third party app called ACR on the same device, and it works fine, while i am muted, it still records the downlink voice.
val audioManager = applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
val maximumVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL)
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, maximumVolume, 0)
val audioSource = MediaRecorder.AudioSource.MIC
val mediaRecorder = MediaRecorder()
mediaRecorder.apply {
setAudioSource(audioSource)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
setAudioChannels(audioChannels)
setAudioSamplingRate(audioSamplingRate)
setAudioEncodingBitRate(audioEncodingBitRate)
setOutputFile(path)
prepare()
start()
This is not an issue. You set the MediaRecorder to use MIC as input, so if you MUTE the microphone it's obliviously that the input signat is lost/muted. When you use "downlink" word I expected to see a different input source as VOICECALL or DOWNLINK instead of MIC. Trying to record a voicecall using the MIC it's wrong in my opinion because: (1) you have to set max volume to speaker and redirect the voicecall through it (2) while recording a voicecall from the MIC the caller hears ALL what it happens around your device and all what you're saying to other people (3) this method records much noise and echoes. The right way is to record from VOICECALL but most of new devices (using newer Android version) prevents to record from this source and allows it only at System Apps. ACR uses a workaround by calling hidden API methods, but this method could stop stop work at any time due to Android updates.
Im trying to build an app that can enable or disable the headphone jack on my Android 4.4 device . Once disabled nothing should be able to take control or activate it, even calling. I can root the device for this.
Any help would be appreciated.
As written here: How to mute audio in headset but let it play on speaker programmatically?
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(false);
And then play the sound through the AudioManager.STREAM_SYSTEM stream.
When the sound's finished playing be sure to return the audio manager to its previous state or it'll stay on loudspeaker.
I have tested my app: it starts playing a song by getting incoming call on external speaker with enough volume to make person on another side to listen what we play on our side.
But when I answer a call, the playing song stops. I want the song to be playing during call so the person on the other side can hear it.
I would appreciate any suggestion from anyone if they has also faced this problem or know a solution.
That's because while you're in a call, media playback routing will follow the voice call routing. And the default output routing for voice calls if you don't have any accessories attached is to use the earpiece.
You could try waiting for the phone state to switch to MODE_IN_CALL, and then use setSpeakerPhoneOn to change the output routing to use the loudspeaker. Note that this will also route the voice call audio to the loudspeaker, not just the media audio.
EDIT: You could try using the stream type ENFORCED_AUDIBLE (integer value 7) for your media playback. However, it might not work across all devices / all Android versions.
I have a Samsung vibrant and I am connecting to to my car using bluetooth. In the samsung music app there is a button to route the audio to via bluetooth or via phone.
Anyone know how there were able create this functionality. I looked at the sdk and I see
ROUTE_BLUETOOTH_A2DP
This constant is deprecated. Do not
set audio routing directly, use
setSpeakerphoneOn(),
setBluetoothScoOn() methods instead.
Routing audio output to bluetooth A2DP
Constant Value: 16 (0x00000010)
but as you can see it is listed as "deprecated", i see the option for setBluetoothScoOn, but not an equivalent to for setting a2dp on.
My end goal would be to create a widget that allows me to turn on and off outing to the a2dp. So I can turn it on when I want to stream music and turn it off when I want to use navigator, but listen to music or the radio at the same time.
Here's what you are looking for: (not sure if this was available at the time of your question...)
setBluetoothA2dpOn();
But it's also deprecated.
My guess would be to use this instead:
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
And to stop routing:
audioManager.setBluetoothScoOn(false);
audioManager.stopBluetoothSco();
audioManager is a instance of the AudioManager.
Here's a reference to AudioManager: AudioManager
Hope this helps, cheers