Android 5.0+ AudioManager setMode not working - android

i am working on AudioManager which is a Android SystemService.
with Android System 5.0+ , i encounter a problem which AudioManager the setMode method is not working .
i through a test ,
Android M, Lollipop.. 5.0+ version , AudioManager setMode is not working .
example :
public void initAudioImageIcon(boolean initLoad) {
boolean isAudioHeaderMode = IMSharedPreferences.getBooleanExtra(this, IMSPConstant.SP_NAME_MESSAGE,
IMSPConstant.SP_KEY_AUDIO_HEADER_MODE);
if (isAudioHeaderMode) {
mAudioHanderMode.setVisibility(View.VISIBLE);
// audioManager.setMode(AudioManager.MODE_IN_CALL) , but android system 5.0+ no any change, getMode() == AudioManager.MODE_NORMAL
setAudioMode(AudioManager.MODE_IN_CALL);
audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
if (!initLoad) {
showAudioModePrompt(this.getText(R.string.im_audio_in_call), 1000);
}
} else {
mAudioHanderMode.setVisibility(View.GONE);
setAudioMode(AudioManager.MODE_NORMAL);
if (!initLoad) {
showAudioModePrompt(this.getText(R.string.im_audio_in_speeker), 1000);
}
}
}
but Android 3.0+,4.0+ is ok ,only 5.0+ .
so ,i don`t know where happen mistakes.

With audio mode set to :
setMode(AudioManager.MODE_IN_COMMUNICATION);
setSpeakerphoneOn(false);
while my audio stream is set to STREAM_MUSIC I can easily route audio to earpiece. I have tested it myself in AOSP Lollipop code.
Here in the question you have never mentioned about your stream type. Do set your stream to STREAM_MUSIC or STREAM_VOICE_CALL and the code should work for you too.

In android Lollipop setAudioMode(AudioManager.MODE_IN_CALL) is restricted. It can be used only by system application with MODIFY_PHONE_STATE permission. However you can use MODE_IN_COMMUNICATION and MODE_NORMAL in normal applications.

Related

audioManager not working on Android 11 and 12

I am making an internal calls application, when I call a number, the native android dialer opens and then instead of following the call in the native dialer, I follow the call in the app. The problem is that from the app it has no effect when I want to activate or deactivate the native dialer speaker. The native dialer is in the background, and the app is in the foreground, but for some reason when I try to activate the speaker from the app, to follow the call from here, it doesn't work. This only happens in versions of Android 11 and 12, however in versions of Android 8,9 and 10 it works perfectly, I can manipulate the speaker from the app, without going to the native dialer.
This is my code to activate and desactivate the speaker:
private fun enableSpeaker() {
audioManager?.let {
if (!it.isSpeakerphoneOn) {
audioManager!!.mode = AudioManager.MODE_IN_COMMUNICATION
audioManager!!.isSpeakerphoneOn = true
}
}
}
private fun disableSpeaker() {
audioManager?.let {
if (it.isSpeakerphoneOn) {
//audioManager.setMicrophoneMute(false)
audioManager!!.mode = AudioManager.MODE_NORMAL
audioManager!!.isSpeakerphoneOn = false
}
}
}
For Android 12 and above, use audioManager.setCommunicationDevice instead of audioManager.isSpeakerPhoneOn = true
More info here : https://developer.android.com/reference/kotlin/android/media/AudioManager#setCommunicationDevice(android.media.AudioDeviceInfo)

Call recorder not working in android 10 (Q)

Call recorder is recording blank for the duration in Android 10 (Pixel 3A). It was working fine for all phones till Android 8 and in Android 9 most phones were recording only one side voice (however it was working fine in pixel 3A)
Is there any way to record calls in Android 10?
The below code doesn't seem to be working anymore.
int audioSource = MediaRecorder.AudioSource.VOICE_CALL;
mediaRecorder.setAudioSource(audioSource);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setAudioEncodingBitRate(32);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setOutputFile(MediaUri);
mediaRecorder.prepare();
mediaRecorder.start();
If SDK 28 or below is used call recording happens for the whole duration but without any voice. If SDK 29 is used call recording fails at the beginning saying check available audio from callback.
I hope its a bug in Google Android 10 and some patch will fix it.
It's possible using Accessibility Service.
Remote call recorder and BoldBeast both record perfectly both side voice in Android 10 (Pixel 3A) without having to root or being a system app. Both of them use Accessibility service.
Detailed info here in this link.
As per Google's new permission policy no other third party apps (Except system apps) can record calls from Android 9 Pie.
This change will not affect previous recordings or call recording in general.
I am using Realme2pro device having version android 10 it is working for me by using Accessibility try this code
To implement Accessibility go through this link
public class Accessibility extends AccessibilityService {
Service mService = null;
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d("MyAccessibilityService", "Shrinidhi: onAccessibilityEvent");
}
#Override
public void onInterrupt() {
}
#Override
protected void onServiceConnected() {
Log.d("MyAccessibilityService", "Shrinidhi: onServiceConnected");
}
#Override
public void onCreate() {
this.mService = this;
Log.d("MyAccessibilityService", "Shrinidhi: onCreate");
}
}
and add AudioSource to recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(file.getAbsolutePath());
Hope it work :)

Android - some devices not playing audio through earpiece

There seems to be an issue while trying to play audio files on earpiece in some android devices. Expected behaviour is it should play the audio file in earpiece but in some devices like Samsung galaxy note 2 ( Android version 4.4.2) and Sony Xperia XA ( Android version 6.0) it plays them through speakers.
However in devices like Motorola moto g (android version 6.0) and nexus 5X (android version 7.0) it works fine.
The app also has the android.permission.MODIFY_AUDIO_SETTINGS permission.
Please find the details of this issue below.
Before playing an audio I set the following properties:
audioManager = (AudioManager)configService.getContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(isEnabled);
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
player.setDataSource(path);
I have tried setting mode to MODE_IN_CALL, but that didn't work either:
audioManager.setMode(android.media.AudioManager.MODE_IN_CALL);
I have also tried solutions from other posts asking similar questions but they didn't help much.
It would be helpful to know whether this issue is device specific and how to fix the same.
Thanks for the help.
Finally i found the solution. When registering for intent ACTION_HEADSET_PLUG in BroadcastReceiver, it was broadcasting sticky intent for the headset unplugged event which was saved from the past event. This was causing the callback to fire which played the audio through speaker only.
Refer to the following stack overflow post for further details: link
To fix the issue i used isInitialStickyBroadcast() method to filter out the past events.
public void onReceive(Context context, Intent intent) {
switch (intent.getIntExtra("state", -1)) {
case 0:
if (!isInitialStickyBroadcast()) {
// headset unplugged
}
break;
case 1:
// headset plugged in
break;
default:
break;
}
}
I've been using ACTION_HEADSET_PLUG but had the same problem with playing sound through earpiece on some devices 6.0 +. My solution is to use AudioAttributes for devices with version higher then LOLLIPOP :
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes mAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.build();
mMediaPlayer = MediaPlayer.create(this, R.raw.connection, mAttributes, 1);
} else {
mMediaPlayer = MediaPlayer.create(this, R.raw.connection);
}

Why changing ringer mode in android does not require permissions?

I wrote a code to change the ringer mode:
private void changeRingerMode() {
AudioManager audMangr;
audMangr = (AudioManager) getBaseContext().getSystemService(
Context.AUDIO_SERVICE);
audMangr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
I am surprised that this one does not require any permission, whereas changing the brightness requires one. I tested it on a ginger bread phone.
Any idea why this works this way?

AudioSource.VOICE_CALL not working in android 4.0 but working in android 2.3

VOICE_CALL, VOICE_DOWNLINK ,VOICE_UPLINK
not working on android 4.0 but working on android 2.3 (Actual Device),I have uploaded a dummy project to record all outgoing call so that you can see it for your self
http://www.mediafire.com/?img6dg5y9ri5c7rrtcajwc5ycgpo2nf
you just have to change audioSource = MediaRecorder.AudioSource.MIC; to audioSource = MediaRecorder.AudioSource.VOICE_CALL; on line 118 in TService.java
If you come across any error, tell me. Any suggestion related to it will be accepted.
After a lot of search I Found that Some Manufactures have closed the access to such function because call recording is not allowed in some countries. If anyone finds such question and get the solution some other way then post it over here it may be helpful to many because many people are have the same issue.
Try to use MediaRecorder.AudioSource.VOICE_RECOGNITION. I had the same problem - ASUS Transformer uses microphone near the back camera by default and audio is very silent in this case. VOICE_CALL doesn't work on this tablet and I have tried VOICE_RECOGNITION - in that case it uses front microphone and audio volume is OK.
OK, in my case this code (thank you eyal!) worked for Samsung Galaxy Note 6:
String manufacturer = Build.MANUFACTURER;
if (manufacturer.toLowerCase().contains("samsung")) {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
} else {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
}
you try to add this,it may be
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
mMediaRecorder.start();
}
}, 1000);

Categories

Resources