当前位置: 首页>>代码示例>>Java>>正文


Java OnAudioFocusChangeListener类代码示例

本文整理汇总了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;
}
 
开发者ID:marverenic,项目名称:Jockey,代码行数:20,代码来源:AudioManagerCompat.java

示例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);
		}
	};
}
 
开发者ID:spectralmind,项目名称:sonarflow-android,代码行数:23,代码来源:PlayerService.java

示例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);
    }
}
 
开发者ID:marverenic,项目名称:Jockey,代码行数:12,代码来源:AudioManagerCompat.java

示例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;
}
 
开发者ID:y20k,项目名称:transistor,代码行数:27,代码来源:AudioFocusHelper.java

示例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 );
	}
}
 
开发者ID:learnNcode,项目名称:2DScroller,代码行数:38,代码来源:TwoDScrollerListview.java

示例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);
        }
    };
}
 
开发者ID:vaibhavs4424,项目名称:AI-Powered-Intelligent-Banking-Platform,代码行数:11,代码来源:AudioPauser.java

示例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);
 	}
 }
 
开发者ID:nvllsvm,项目名称:Audinaut,代码行数:39,代码来源:Util.java

示例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);
}
 
开发者ID:MatthewTamlin,项目名称:AndroidUtilities,代码行数:16,代码来源:TestAudioFocusHelper.java

示例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;
}
 
开发者ID:y20k,项目名称:transistor,代码行数:14,代码来源:AudioFocusRequestCompat.java

示例10: setOnAudioFocusChangeListener

import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
@NonNull
public Builder setOnAudioFocusChangeListener(@NonNull OnAudioFocusChangeListener listener,
                                             @NonNull Handler handler) {
    mOnAudioFocusChangeListener = listener;
    mFocusChangeHandler = handler;
    return this;
}
 
开发者ID:y20k,项目名称:transistor,代码行数:8,代码来源:AudioFocusRequestCompat.java

示例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);
   	}
   }
 
开发者ID:popeen,项目名称:Popeens-DSub,代码行数:40,代码来源:Util.java

示例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);
   	}
   }
 
开发者ID:MadMarty,项目名称:madsonic-5.5,代码行数:38,代码来源:Util.java

示例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);
	}
}
 
开发者ID:ultrasonic,项目名称:ultrasonic,代码行数:54,代码来源:Util.java

示例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;
            }
        }
    };
}
 
开发者ID:jiaZengShen,项目名称:vlc_android_win,代码行数:51,代码来源:PlaybackService.java

示例15: getOnAudioFocusChangeListener

import android.media.AudioManager.OnAudioFocusChangeListener; //导入依赖的package包/类
OnAudioFocusChangeListener getOnAudioFocusChangeListener() {
    return mOnAudioFocusChangeListener;
}
 
开发者ID:y20k,项目名称:transistor,代码行数:4,代码来源:AudioFocusRequestCompat.java


注:本文中的android.media.AudioManager.OnAudioFocusChangeListener类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。