Skip to content

Instantly share code, notes, and snippets.

Created April 6, 2016 17:44
Show Gist options
  • Save anonymous/6d00bb2e8524fb78bb50f118a6180b8a to your computer and use it in GitHub Desktop.
Save anonymous/6d00bb2e8524fb78bb50f118a6180b8a to your computer and use it in GitHub Desktop.
Miwok app: OnAudioFocusChangeListener declaration from NumberActivity
/**
* This listener gets triggered whenever the audio focus changes
* (i.e., we gain or lose audio focus because of another app or device).
*/
private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// The AUDIOFOCUS_LOSS_TRANSIENT case means that we've lost audio focus for a
// short amount of time. The AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK case means that
// our app is allowed to continue playing sound but at a lower volume. We'll treat
// both cases the same way because our app is playing short sound files.
// Pause playback and reset player to the start of the file. That way, we can
// play the word from the beginning when we resume playback.
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// The AUDIOFOCUS_GAIN case means we have regained focus and can resume playback.
mMediaPlayer.start();
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
// The AUDIOFOCUS_LOSS case means we've lost audio focus and
// Stop playback and clean up resources
releaseMediaPlayer();
}
}
};
@cabudies
Copy link

cabudies commented Jun 3, 2017

I don't understand. I had the same code however, I simply made AudioManager and AudioManager.OnAudioFocusChangeListener class variables and whenever I am trying to access them inside the onCreate() method, I am getting this message on Debugger 'No instance found:'audioManager'" Do I need to initialize them as you did?

@danieru13
Copy link

@cabudies Since AudioManager.OnAudioFocusChangeListener it has no implementation so you have to implement it yourself or it won't work

@yeswanth599
Copy link

Hi All,

After initializing the AudioManager reference variable , to get the AUDIO_SERVICE S i am using below code. i am getting the Context unable to recognize.
mAudioManager is private global variable.
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); could you please help me.

Thanks & Regards,
Yeswanth.

@KajcsaErno
Copy link

This task was kind of hard, but at the end somehow I nailed it.

@Pav2712
Copy link

Pav2712 commented Jan 21, 2018

The screenshot in the video was not showing @OverRide and was not showing 'Private' before the line ...

AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener =
new AudioManager.OnAudioFocusChangeListener() {...

EDIT:.... however, even though this is in the final code sample, it doesn't seem to have brought up any errors by leaving it out

NB: In the video from 07:23 to 07:33 there is a quick deletion of a couple of lines of code from the sample. Personally I missed this and only deleted it from the final code given in the link. This gets rid of the 2nd error. Maybe just me but if anyone else missed it, thought I'd share, it was very quick.

@AJSer027
Copy link

AJSer027 commented Feb 8, 2018

@yeswanth599 try adding a context
mAudioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);

@Maxwe1
Copy link

Maxwe1 commented Mar 5, 2018

Done it

@louayeldin
Copy link

I was a little bit hard for me to understand the way how it works, but at the end, everything is clear now. Thank you so much

@abmjladan
Copy link

good

@tooptooptoop
Copy link

good

@Einston77
Copy link

It was challenging. This solution is very simple compared to mine. I created a new class put all my code for audiofocus in there. Then I created an instance of this new class to handle audio events. This way there is very little changes made to my existing code and no unnecessary code duplication.

@nikhilsutar123
Copy link

I don't understand. I had the same code however, I simply made AudioManager and AudioManager.OnAudioFocusChangeListener class variables and whenever I am trying to access them inside the onCreate() method, I am getting this message on Debugger 'No instance found:'audioManager'" Do I need to initialize them as you did?

Define it globally. It worked for me.

@abouzarj
Copy link

abouzarj commented May 24, 2019

I did use a switch statement for different audio focus states

@nikitha2
Copy link

Hi All,

After initializing the AudioManager reference variable , to get the AUDIO_SERVICE S i am using below code. i am getting the Context unable to recognize.
mAudioManager is private global variable.
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); could you please help me.

Thanks & Regards,
Yeswanth.

try using mAudioManager=(AudioManager) contextActivity.getSystemService(Context.AUDIO_SERVICE);

@kwanwoo9
Copy link

kwanwoo9 commented Mar 6, 2021

Does someone have a problem with deprecations, how did you solve it?

@jiomail000
Copy link

mAudioManager.registerMediaButtonEventReciever(); ????

@aljebraschool
Copy link

Nice code up there!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment