K J
K J

Reputation: 4743

AudioManager.setStreamMute not working in android 5 Lollipop

I'm developing a spam call filtering app and I'm trying to silence ringer for incoming (spam) call. The problem is none of AudioManager.setStreamMute nor AudioManager.setRingerMode is working in Lollipop. Here is my code snippet:

public class CallReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);

    if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

      AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
      audioManager.setStreamMute(AudioManager.STREAM_RING, false);
      Log.i(TAG, "unmute");

    } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

      AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
      audioManager.setStreamMute(AudioManager.STREAM_RING, true);
      Log.i(TAG, "mute");

    }
  }

When there's an incoming call, the mute part always gets executed but it sometimes succeeds and sometimes fails to mute the ringer. I can't find any rule. And audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT) doesn't work either. This seems work fine when tested on emulators < 5, so I guess it's somehow related to Lollipop not having silent mode but interruptions filter. Commercial spam call filters are woking fine, so can somebody let me know how I could silence incoming calls with Lollipop?

Upvotes: 0

Views: 1792

Answers (1)

Infinity
Infinity

Reputation: 91

I've got the same issue for android 5. AudioManager.setStreamMute with the value false to unmute is never working.

I tried AudioManager.setStreamSolo and it worked for me.

//to mute ringtone
Global.app().getAudioManager().setStreamSolo(AudioManager.STREAM_MUSIC, true);

//unmute ringtone
Global.app().getAudioManager().setStreamSolo(AudioManager.STREAM_MUSIC, false);

It mutes all other streams except one you want to play. In may case I needed to play my own audio instead of ringtone.

But you can try to play a silence audio to hack if you need absolute silence.

Upvotes: 1

Related Questions