當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。