If you think the Android project K6nele listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package ee.ioc.phon.android.speak;
//www.java2s.comimport android.content.Context;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
publicclass AudioPauser {
privatefinal AudioManager mAudioManager;
privatefinal OnAudioFocusChangeListener mAfChangeListener;
privateint mCurrentVolume;
privateboolean isPausing = false;
public AudioPauser(Context context) {
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAfChangeListener = new OnAudioFocusChangeListener() {
publicvoid onAudioFocusChange(int focusChange) {
Log.i("onAudioFocusChange" + focusChange);
}
};
}
/**
* Requests audio focus with the goal of pausing any existing audio player.
* Additionally mutes the music stream, since some audio players might
* ignore the focus request.
* In other words, during the pause no sound will be heard,
* but whether the audio resumes from the same position after the pause
* depends on the audio player.
* <p/>
* TODO: we do this only if the music is active (not sure it always works)
*/publicvoid pause() {
if (!isPausing && mAudioManager.isMusicActive()) {
mCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int result = mAudioManager.requestAudioFocus(mAfChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
Log.i("AUDIOFOCUS_REQUEST_GRANTED");
}
// TODO: improve this: cuts off the beep
//mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
isPausing = true;
Log.i("AudioPauser: pause: " + mCurrentVolume);
}
}
/**
* Abandons audio focus and restores the audio volume.
*/publicvoid resume() {
if (isPausing) {
mAudioManager.abandonAudioFocus(mAfChangeListener);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mCurrentVolume, 0);
isPausing = false;
Log.i("AudioPauser: resume: " + mCurrentVolume);
}
}
}