0

I have recently encountered something that caused me to pull out half of my hair off.

I have tried to access the audioManager and to set a new mode like this:

mAudioManager.setMode(AudioManager.MODE_NORMAL);

With some reason which, in that time, i didnt understand the audioManager refused to set the new mode.

Only after a hours of debugging and trying everything that i had in my sleeves, I discovered that I`m accessing the audio manager from another part in my app, but with an Activity context, and not with the Application context, which was what i used before.

It seems that when acquiring the AudioManager, you "steal" its access with the new Context from the other context that it is assigned to.

You`ll find the fix in the answer.

If you found this helpful, please consider voting for this this who inspired me.

Community
  • 1
  • 1
gor
  • 1,046
  • 1
  • 14
  • 28
  • 1
    There is nothing wrong with answering your own question, but write the answer as an actual answer. In its current state your question will not survive the review process. It will only get down voted and closed. If you want to get the information out there, then write both a proper question and answer. – Xaver Kapeller Sep 15 '14 at 11:23
  • 1
    Will do, though the main point was sharing the knowledge and not to gaining points. – gor Sep 16 '14 at 06:08
  • Yes and there is nothing wrong with that, but you cannot share knowledge that way on Stack Overflow. As I have been trying to explain to you: You need to write both a proper question and answer. If just one of them is not deemed acceptable then this question will eventually be deleted. And btw: all questions without answers will be deleted automatically after 30 days. – Xaver Kapeller Sep 16 '14 at 07:26
  • good to know. i`ll fix it. – gor Sep 16 '14 at 14:13

1 Answers1

0

well, in order to get the AudioManager you`ll need a Context. like that:

AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

Just make sure you are passing the same context all the time.

In my case i used have tried to use both application and Activity context, so one way to implement that is by calling to the application inside your Activity (or what ever), like this:

AudioManager am = (AudioManager) getApplication().getSystemService(getApplication().AUDIO_SERVICE);

Hope i helped for some lost folks in this ocean.

gor
  • 1,046
  • 1
  • 14
  • 28