14

In Android I need to get notified when the current audio mode gets changed.

I can get this value through getMode() but this is polling.

I don't want to poll every few seconds.

What are my options?

(Please note that I'm not asking about ringerMode)

Hagai L
  • 1,593
  • 1
  • 18
  • 40

3 Answers3

4

You can use AudioFocus for devices running Android OS version 2.2 and higher, preassuming your app is also playing audio.

You can register for audio focus changes and then listen onAudioFocusChange(int focusChange) method. By this way you will be notified of audio mode changes as well as other audio focus changes. I hope this helps.

You can also use PhoneStateListener to listen phone states changes and corresponding audio modes.

CanC
  • 3,295
  • 1
  • 14
  • 15
  • can you give an example of how this can be implemented? – SnG Apr 22 '20 at 19:28
  • You can find the implementation with explanations about the audio focus here: https://developer.android.com/guide/topics/media-apps/audio-focus But it can be used only when we request the audio focus for your app. – Ayer Ribeiro de Souza Netto Feb 05 '21 at 14:41
3

http://developer.android.com/reference/android/media/AudioManager.html

interface AudioManager.OnAudioFocusChangeListener

Interface definition for a callback to be invoked when the audio focus of the system is updated.

setting this would give you use of onAudioFocusChange method. This would detect Mode changes among other things.

Community
  • 1
  • 1
Ilan Kutsman
  • 469
  • 3
  • 9
-2

Try this in kotlin

In call i am using the timer for update the call time .I use the code inside the timer

 val am = context!!.getSystemService(Context.AUDIO_SERVICE) as AudioManager
                        val mode = am.mode
    
                        if (AudioManager.MODE_IN_CALL == mode) {
                            // device is in a telephony call
                        } else if (AudioManager.MODE_IN_COMMUNICATION == mode) {
                            Log.e("MODE -","IN_COMMUNICATION")
                            // device is in communiation mode, i.e. in a VoIP or video call
                        } else if (AudioManager.MODE_RINGTONE == mode) {
                            // device is in ringing mode, some incoming is being signalled
                            if(AttendMeetingActivity.meetingManager!=null){
                                AttendMeetingActivity.meetingManager!!.onIncomingCall()
                            }else if( IncomingCallActivity.callControlManager!=null){
                                IncomingCallActivity!!.callControlManager!!.doHangUpCall()
                            }
                        } else {
                            // device is in normal mode, no incoming and no audio being played
                        }
Sreejesh K Nair
  • 563
  • 1
  • 6
  • 16