Reputation: 53
I encounter a problem using the AudioFocusChangeListener, but still cannot listen to the audio focus change. Could someone help me find the reasons? Here is what I do:
In my FM app, when FM starts in startFM, I place requestAudioFocus()
to get resources:
int result = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_FM,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
Log.d(LOGTAG, "Successfull to request audioFocus listener");
} else {
Log.d(LOGTAG, "Failure to request focus listener");
}
When quitting the App in onDestroy()
, I used abandonAudioFocus()
to release resouces.
The member variable mAudioFocusListener
is as follows:
private OnAudioFocusChangeListener mAudioFocusListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS, turning FM off");
if (isFmOn()) {
fmOff();
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
if (isFmOn()) {
Log.d(LOGTAG, "AudioFocus: FM is on, turning off");
mute();
stopFM();
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT");
if (isFmOn()) {
Log.d(LOGTAG, "AudioFocus: FM is on, turning off");
mute();
stopFM();
}
break;
case AudioManager.AUDIOFOCUS_GAIN:
Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_GAIN");
if (isFmOn()) {
Log.d(LOGTAG, "AudioFocus: FM is off, turning back on");
unMute();
startFM();
}
break;
default:
Log.e(LOGTAG, "Unknown audio focus change code " + focusChange);
}
}
};
According to the log, it prints "Successfull to request audioFocus listener", but doesn't print any AudioForcus
change log when alarm is on or other audio-changed events. Could anyone figure out what is wrong with it?
On the other hand, I place log in AudioService.java and AudioManger.java, where in AudioService.java, requestAudioFocus(): if (!mFocusStack.empty() && (mFocusStack.peek().mFocusDispatcher != null)) {
try {
mFocusStack.peek().mFocusDispatcher.dispatchAudioFocusChange(
-1 * focusChangeHint, // loss and gain codes are inverse of each other
mFocusStack.peek().mClientId); // This doesn't excute
} catch (RemoteException e) {
Log.e(TAG, " Failure to signal loss of focus due to "+ e);
e.printStackTrace();
}
}
in AudioManager.java, Here doesn't excute either: private IAudioFocusDispatcher mAudioFocusDispatcher = new IAudioFocusDispatcher.Stub() {
public void dispatchAudioFocusChange(int focusChange, String id) {
Message m = mAudioFocusEventHandlerDelegate.getHandler().obtainMessage(focusChange, id);
mAudioFocusEventHandlerDelegate.getHandler().sendMessage(m);
}
That's all my question. Could anybody tell me why this listener cannot listen to alarm events and other audiofocus-changed events? By the way, how could I successfully listen to the events? Maybe I missed some step or used a incorrect way? Many thanks.
Upvotes: 2
Views: 4104
Reputation: 3729
DeerCoder,
I believe that if you are requesting the audio focus for the type AudioManager.STREAM_MUSIC, you would only receive events affecting this specific audio stream.
mAudioManager.requestAudioFocus(new MyService(), AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
The alarms and notifications are managed on separate audio stream such as AudioManager.STREAM_NOTIFICATION, AudioManager.STREAM_ALARM, AudioManager.STREAM_RING, etc...
Upvotes: 0
Reputation: 794
Try this:
private class MyService extends Service implements AudioManager.OnAudioFocusChangeListener {
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
Log.e(TAG, "AudioFocus: received AUDIOFOCUS_LOSS, turning FM off");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.e(TAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.e(TAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT");
break;
case AudioManager.AUDIOFOCUS_GAIN:
Log.v(TAG, "AudioFocus: received AUDIOFOCUS_GAIN");
break;
default:
Log.e(TAG, "Unknown audio focus change code " + focusChange);
}
}
public IBinder onBind(Intent intent) {
return null;
}
}
and...
mAudioManager.requestAudioFocus(new MyService(), AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
Its works for me.
Upvotes: 1