本文整理汇总了Java中android.media.AudioManager.OnAudioFocusChangeListener类的典型用法代码示例。如果您正苦于以下问题:Java OnAudioFocusChangeListener类的具体用法?Java OnAudioFocusChangeListener怎么用?Java OnAudioFocusChangeListener使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OnAudioFocusChangeListener类属于android.media.AudioManager包,在下文中一共展示了OnAudioFocusChangeListener类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: requestAudioFocus
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
public boolean requestAudioFocus(OnAudioFocusChangeListener focusChangeListener,
int streamType, int audioFocusGain) {
int r;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
r = mAudioManager.requestAudioFocus(
new AudioFocusRequest.Builder(audioFocusGain)
.setAudioAttributes(
new AudioAttributes.Builder()
.setLegacyStreamType(streamType)
.build())
.setOnAudioFocusChangeListener(focusChangeListener)
.build());
} else {
//noinspection deprecation
r = mAudioManager.requestAudioFocus(focusChangeListener, streamType, audioFocusGain);
}
return r == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}
示例2: PlayerService
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
public PlayerService() {
musicPlayer = new MusicPlayer();
musicPlayer.addListener(new MusicPlayer.Listener() {
@Override
public void onCurrentTrackChanged() {
updateScrobbling();
}
@Override
public void onStateChanged() {
sentStateBroadcast();
if(musicPlayer.getState() != PlayerState.Preparing)
updateNotification();
}
});
audioFocusListener = new OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
handleAudioFocusChange(focusChange);
}
};
}
示例3: abandonAudioFocus
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
public void abandonAudioFocus(OnAudioFocusChangeListener focusChangeListener) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mAudioManager.abandonAudioFocusRequest(
new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(focusChangeListener)
.build());
} else {
//noinspection deprecation
mAudioManager.abandonAudioFocus(focusChangeListener);
}
}
示例4: requestAudioFocus
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
@Override
public boolean requestAudioFocus(AudioFocusRequestCompat audioFocusRequestCompat) {
// Save the focus request.
mAudioFocusRequestCompat = audioFocusRequestCompat;
// Check for possible problems...
if (audioFocusRequestCompat.acceptsDelayedFocusGain()) {
final String message = "Cannot request delayed focus on API " +
Build.VERSION.SDK_INT;
// Make an exception to allow the developer to more easily find this code path.
@SuppressWarnings("ThrowableNotThrown")
final Throwable stackTrace = new UnsupportedOperationException(message)
.fillInStackTrace();
Log.w(TAG, "Cannot request delayed focus", stackTrace);
}
final OnAudioFocusChangeListener listener =
mAudioFocusRequestCompat.getOnAudioFocusChangeListener();
final int streamType =
mAudioFocusRequestCompat.getAudioAttributesCompat().getLegacyStreamType();
final int focusGain = mAudioFocusRequestCompat.getFocusGain();
return mAudioManager.requestAudioFocus(listener, streamType, focusGain) ==
AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}
示例5: playSound
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
/**
* To play sound.
*
* @param context
* {@link Context}
*
* @param soundID
* sound id.
*
* @param soundPool
* {@link SoundPool} instance.
*
*/
public void playSound(Context context, int soundID, SoundPool soundPool){
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){
// Lower the volume
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Raise it back to normal
}
}
};
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = audioManager.requestAudioFocus(afChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
soundPool.play(soundID, 10, 10, 1, 0,1f );
}
}
示例6: AudioPauser
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
public AudioPauser(Context context, boolean isMuteStream) {
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mIsMuteStream = isMuteStream;
mAfChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
Log.i("onAudioFocusChange" + focusChange);
}
};
}
示例7: requestAudioFocus
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
public static void requestAudioFocus(final Context context) {
if (focusListener == null) {
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(focusListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
DownloadService downloadService = (DownloadService)context;
if((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
if(downloadService.getPlayerState() == PlayerState.STARTED) {
Log.i(TAG, "Temporary loss of focus");
SharedPreferences prefs = getPreferences(context);
int lossPref = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1"));
if(lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
lowerFocus = true;
downloadService.setVolume(0.1f);
} else if(lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) {
pauseFocus = true;
downloadService.pause(true);
}
}
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
if(pauseFocus) {
pauseFocus = false;
downloadService.start();
}
if(lowerFocus) {
lowerFocus = false;
downloadService.setVolume(1.0f);
}
} else if(focusChange == AudioManager.AUDIOFOCUS_LOSS) {
Log.i(TAG, "Permanently lost focus");
focusListener = null;
downloadService.pause();
audioManager.abandonAudioFocus(this);
}
}
}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
}
示例8: setup
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
@Before
public void setup() {
context = InstrumentationRegistry.getTargetContext();
// Mockito cannot mock this interface, so just use an instance that does nothing when called
listener = new OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int i) {
// Do nothing
}
};
assertThat("Precondition 1 failed. Context is null.", context != null);
assertThat("Precondition 2 failed. Listener is null.", listener != null);
}
示例9: AudioFocusRequestCompat
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
private AudioFocusRequestCompat(int focusGain,
OnAudioFocusChangeListener onAudioFocusChangeListener,
Handler focusChangeHandler,
AudioAttributesCompat audioFocusRequestCompat,
boolean pauseOnDuck,
boolean acceptsDelayedFocusGain) {
mFocusGain = focusGain;
mOnAudioFocusChangeListener = onAudioFocusChangeListener;
mFocusChangeHandler = focusChangeHandler;
mAudioAttributesCompat = audioFocusRequestCompat;
mPauseOnDuck = pauseOnDuck;
mAcceptsDelayedFocusGain = acceptsDelayedFocusGain;
}
示例10: setOnAudioFocusChangeListener
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
@NonNull
public Builder setOnAudioFocusChangeListener(@NonNull OnAudioFocusChangeListener listener,
@NonNull Handler handler) {
mOnAudioFocusChangeListener = listener;
mFocusChangeHandler = handler;
return this;
}
示例11: requestAudioFocus
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
@TargetApi(8)
public static void requestAudioFocus(final Context context) {
if (Build.VERSION.SDK_INT >= 8 && focusListener == null) {
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(focusListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
DownloadService downloadService = (DownloadService)context;
if((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isRemoteEnabled()) {
if(downloadService.getPlayerState() == PlayerState.STARTED) {
Log.i(TAG, "Temporary loss of focus");
SharedPreferences prefs = getPreferences(context);
int lossPref = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1"));
if(lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
lowerFocus = true;
downloadService.setVolume(0.1f);
} else if(lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) {
pauseFocus = true;
downloadService.pause(true);
}
}
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
if(pauseFocus) {
pauseFocus = false;
downloadService.start();
}
if(lowerFocus) {
lowerFocus = false;
downloadService.setVolume(1.0f);
}
} else if(focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isRemoteEnabled()) {
Log.i(TAG, "Permanently lost focus");
focusListener = null;
downloadService.pause();
audioManager.abandonAudioFocus(this);
}
}
}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
}
示例12: requestAudioFocus
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
@TargetApi(8)
public static void requestAudioFocus(final Context context) {
if (Build.VERSION.SDK_INT >= 8 && !hasFocus) {
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
hasFocus = true;
audioManager.requestAudioFocus(new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
DownloadServiceImpl downloadService = (DownloadServiceImpl)context;
if((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isJukeboxEnabled()) {
if(downloadService.getPlayerState() == PlayerState.STARTED) {
SharedPreferences prefs = getPreferences(context);
int lossPref = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_AUDIO_FOCUS, "1"));
if(lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
lowerFocus = true;
downloadService.setVolume(0.1f);
} else if(lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) {
pauseFocus = true;
downloadService.pause();
}
}
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
if(pauseFocus) {
pauseFocus = false;
downloadService.start();
} else if(lowerFocus) {
lowerFocus = false;
downloadService.setVolume(1.0f);
}
} else if(focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isJukeboxEnabled()) {
hasFocus = false;
downloadService.pause();
audioManager.abandonAudioFocus(this);
}
}
}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
}
示例13: requestAudioFocus
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
public static void requestAudioFocus(final Context context)
{
if (!hasFocus)
{
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
hasFocus = true;
audioManager.requestAudioFocus(new OnAudioFocusChangeListener()
{
@Override
public void onAudioFocusChange(int focusChange)
{
DownloadService downloadService = (DownloadService) context;
if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isJukeboxEnabled())
{
if (downloadService.getPlayerState() == PlayerState.STARTED)
{
SharedPreferences preferences = getPreferences(context);
int lossPref = Integer.parseInt(preferences.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1"));
if (lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK))
{
lowerFocus = true;
downloadService.setVolume(0.1f);
}
else if (lossPref == 0 || (lossPref == 1))
{
pauseFocus = true;
downloadService.pause();
}
}
}
else if (focusChange == AudioManager.AUDIOFOCUS_GAIN)
{
if (pauseFocus)
{
pauseFocus = false;
downloadService.start();
}
else if (lowerFocus)
{
lowerFocus = false;
downloadService.setVolume(1.0f);
}
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isJukeboxEnabled())
{
hasFocus = false;
downloadService.pause();
audioManager.abandonAudioFocus(this);
}
}
}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
}
示例14: createOnAudioFocusChangeListener
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.FROYO)
private OnAudioFocusChangeListener createOnAudioFocusChangeListener() {
return new OnAudioFocusChangeListener() {
private boolean mLossTransient = false;
private boolean mLossTransientCanDuck = false;
@Override
public void onAudioFocusChange(int focusChange) {
/*
* Pause playback during alerts and notifications
*/
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
Log.i(TAG, "AUDIOFOCUS_LOSS");
// Stop playback
changeAudioFocus(false);
stop();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.i(TAG, "AUDIOFOCUS_LOSS_TRANSIENT");
// Pause playback
if (mMediaPlayer.isPlaying()) {
mLossTransient = true;
mMediaPlayer.pause();
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.i(TAG, "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
// Lower the volume
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.setVolume(36);
mLossTransientCanDuck = true;
}
break;
case AudioManager.AUDIOFOCUS_GAIN:
Log.i(TAG, "AUDIOFOCUS_GAIN: " + mLossTransientCanDuck + ", " + mLossTransient);
// Resume playback
if (mLossTransientCanDuck) {
mMediaPlayer.setVolume(100);
mLossTransientCanDuck = false;
}
if (mLossTransient) {
mMediaPlayer.play();
mLossTransient = false;
}
break;
}
}
};
}
示例15: getOnAudioFocusChangeListener
import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
OnAudioFocusChangeListener getOnAudioFocusChangeListener() {
return mOnAudioFocusChangeListener;
}