本文整理汇总了Java中android.media.AudioManager.AUDIOFOCUS_GAIN属性的典型用法代码示例。如果您正苦于以下问题:Java AudioManager.AUDIOFOCUS_GAIN属性的具体用法?Java AudioManager.AUDIOFOCUS_GAIN怎么用?Java AudioManager.AUDIOFOCUS_GAIN使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类android.media.AudioManager
的用法示例。
在下文中一共展示了AudioManager.AUDIOFOCUS_GAIN属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onAudioFocusChange
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
break;
case AudioManager.AUDIOFOCUS_LOSS:
releaseAllVideos();
Log.d(TAG, "AUDIOFOCUS_LOSS [" + this.hashCode() + "]");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
try {
if (JCMediaManager.instance().mediaPlayer != null &&
JCMediaManager.instance().mediaPlayer.isPlaying()) {
JCMediaManager.instance().mediaPlayer.pause();
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT [" + this.hashCode() + "]");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
break;
}
}
示例2: onAudioFocusChange
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
break;
case AudioManager.AUDIOFOCUS_LOSS:
onPause();
Log.d(TAG, "AUDIOFOCUS_LOSS [" + this.hashCode() + "]");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
try {
if (MediaManager.instance().mediaPlayer != null &&
MediaManager.instance().mediaPlayer.isPlaying()) {
MediaManager.instance().mediaPlayer.pause();
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT [" + this.hashCode() + "]");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
break;
}
}
示例3: onAudioFocusChange
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
break;
case AudioManager.AUDIOFOCUS_LOSS:
releaseAllVideos();
Log.d(TAG, "AUDIOFOCUS_LOSS [" + this.hashCode() + "]");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
if (JCMediaManager.instance().mediaPlayer != null &&
JCMediaManager.instance().mediaPlayer.isPlaying()) {
JCMediaManager.instance().mediaPlayer.pause();
}
Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT [" + this.hashCode() + "]");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
break;
}
}
示例4: onAudioFocusChange
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
abandonAudioFocus();
pause();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
if (mGlue.isMediaPlaying()) {
pause();
mPauseTransient = true;
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
mPlayer.mute(true);
break;
case AudioManager.AUDIOFOCUS_GAIN:
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
if (mPauseTransient) {
play();
}
mPlayer.mute(false);
break;
}
}
示例5: setAudioFocusChangeListener
/**
* Utility method to generate a listener for changed on Focus. Only available in API Level 8
* @param player the player which focus will be watched for
* @return a listener object with associated callbacks for each state.
*/
public static Object setAudioFocusChangeListener(final Player player) {
Object afChangeListener = (android.media.AudioManager.OnAudioFocusChangeListener) new android
.media.AudioManager.OnAudioFocusChangeListener() {
private boolean playbackFlag = false;
/**
* This callback method is triggered when audio focus changes. This is necessary because
* several apps can be managing audio at the same time and for them to interact correctly,
* they should be using an AudioManager: http://developer.android.com/training/managing-audio/audio-focus.html
* @param focusChange the type of focus change
*/
@Override
public void onAudioFocusChange(int focusChange) {
switch(focusChange){
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// Focus loss transient: Pause playback
if (player != null && player.playerState == Player.State.PLAYING) {
player.pause();
playbackFlag = true;
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
// Focus loss permanent: focus taken by other players
playbackFlag = false;
player.OtherPlayerStarted();
break;
case AudioManager.AUDIOFOCUS_GAIN:
// Focus gain: Resume playback
if (player != null && playbackFlag && player.playerState == Player.State.PAUSED_BY_EVENT) {
player.Start();
playbackFlag = false;
}
break;
}
}
};
return afChangeListener;
}
示例6: onAudioFocusChange
@Override
public void onAudioFocusChange(int focusChange) {
if (currentFocus == focusChange) {
return;
}
currentFocus = focusChange;
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
if (startRequested || pausedForLoss) {
start();
startRequested = false;
pausedForLoss = false;
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
if (isPlaying()) {
pausedForLoss = true;
pause();
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
if (isPlaying()) {
pausedForLoss = true;
pause();
}
break;
}
}
示例7: onAudioFocusChange
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
mediaPlayer.start();
return;
default:
releaseMediaPlayer();
return;
}
}
示例8: onAudioFocusChange
/**
* Called by AudioManager on audio focus changes. Implementation of {@link
* AudioManager.OnAudioFocusChangeListener}.
*/
@Override
public void onAudioFocusChange(int focusChange) {
boolean gotFullFocus = false;
boolean canDuck = false;
if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
gotFullFocus = true;
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS
|| focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
|| focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// We have lost focus. If we can duck (low playback volume), we can keep playing.
// Otherwise, we need to pause the playback.
canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
}
if (gotFullFocus || canDuck) {
if (mMediaPlayer != null) {
if (mPlayOnFocusGain) {
mPlayOnFocusGain = false;
mMediaPlayer.start();
mState = PlaybackStateCompat.STATE_PLAYING;
updatePlaybackState();
}
float volume = canDuck ? 0.2f : 1.0f;
mMediaPlayer.setVolume(volume, volume);
}
} else if (mState == PlaybackStateCompat.STATE_PLAYING) {
mMediaPlayer.pause();
mState = PlaybackStateCompat.STATE_PAUSED;
updatePlaybackState();
}
}
示例9: get
public static AudioPlayerManager get(Context context){
if(instance ==null){
instance = new AudioPlayerManager();
instance.context = context;
instance.am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
instance.listener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT){
//Log.e("dd"," AUDIOFOCUS_LOSS_TRANSIENT ---------------------");
if(instance.state == State.playing){
instance.stateBeforeFocusChange = State.playing;
}
instance.pause();
// Pause playback
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
//Log.e("dd"," AUDIOFOCUS_GAIN ---------------------");
if(instance.stateBeforeFocusChange == State.playing){
instance.resume();
}
// Resume playback
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
//Log.e("dd"," AUDIOFOCUS_LOSS ---------------------");
//am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
instance.stop();
// Stop playback
}
}
};
instance.registerHeadsetPlugReceiver();
}
return instance;
}
示例10: onAudioFocusChange
/**
* Called by AudioManager on audio focus changes. Implementation of {@link
* AudioManager.OnAudioFocusChangeListener}
*/
@Override
public void onAudioFocusChange(int focusChange) {
boolean gotFullFocus = false;
boolean canDuck = false;
if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
gotFullFocus = true;
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS
|| focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
|| focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// We have lost focus. If we can duck (low playback volume), we can keep playing.
// Otherwise, we need to pause the playback.
canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
}
if (gotFullFocus || canDuck) {
if (mMediaPlayer != null) {
if (mPlayOnFocusGain) {
mPlayOnFocusGain = false;
mMediaPlayer.start();
mState = PlaybackStateCompat.STATE_PLAYING;
updatePlaybackState();
}
float volume = canDuck ? 0.2f : 1.0f;
mMediaPlayer.setVolume(volume, volume);
}
} else if (mState == PlaybackStateCompat.STATE_PLAYING) {
mMediaPlayer.pause();
mState = PlaybackStateCompat.STATE_PAUSED;
updatePlaybackState();
}
}
示例11: onAudioFocusChange
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
mediaSessionCallback.onPlay(); // Не очень красиво
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
mediaSessionCallback.onPause();
break;
default:
mediaSessionCallback.onPause();
break;
}
}
示例12: onAudioFocusChange
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// Permanent loss of audio focus
// Pause playback immediately
mediaPlayer.pause();
mediaPlayer.seekTo(0);
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
relaseMediaResource();
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
mediaPlayer.start();
}
}
示例13: onAudioFocusChange
public void onAudioFocusChange(int focusChange) {
if (mPlayer == null) {
// this activity has handed its MediaPlayer off to the next activity
// (e.g. portrait/landscape switch) and should abandon its focus
mAudioManager.abandonAudioFocus(this);
return;
}
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
mPausedByTransientLossOfFocus = false;
mPlayer.pause();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
if (mPlayer.isPlaying()) {
mPausedByTransientLossOfFocus = true;
mPlayer.pause();
}
break;
case AudioManager.AUDIOFOCUS_GAIN:
if (mPausedByTransientLossOfFocus) {
mPausedByTransientLossOfFocus = false;
start();
}
break;
}
updatePlayPause();
}
示例14: onAudioFocusChange
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
mMediaPlayer.start();
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
releaseMediaPlayer();
}
}
示例15: onAudioFocusChange
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
}
else if (focusChange == AudioManager.AUDIOFOCUS_GAIN){
mMediaPlayer.start();
}
else if(focusChange == AudioManager.AUDIOFOCUS_LOSS){
releaseMediaPlayer();
}
}