android.media.AudioFocusRequest Java Examples
The following examples show how to use
android.media.AudioFocusRequest.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AudioManagerCompat.java From Jockey with Apache License 2.0 | 7 votes |
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; }
Example #2
Source File: AppRTCAudioManager.java From sealrtc-android with MIT License | 7 votes |
private AudioFocusRequest getAudioFocusRequest() { AudioFocusRequest request = null; AudioAttributes audioAttributes = null; if (android.os.Build.VERSION.SDK_INT >= VERSION_CODES.O) { audioAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) .build(); request = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) .setWillPauseWhenDucked(true) .setAudioAttributes(audioAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener(audioFocusChangeListener) .build(); } return request; }
Example #3
Source File: AudioFocusManager.java From MediaSDK with Apache License 2.0 | 6 votes |
@RequiresApi(26) private int requestAudioFocusV26() { if (audioFocusRequest == null || rebuildAudioFocusRequest) { AudioFocusRequest.Builder builder = audioFocusRequest == null ? new AudioFocusRequest.Builder(focusGain) : new AudioFocusRequest.Builder(audioFocusRequest); boolean willPauseWhenDucked = willPauseWhenDucked(); audioFocusRequest = builder .setAudioAttributes(Assertions.checkNotNull(audioAttributes).getAudioAttributesV21()) .setWillPauseWhenDucked(willPauseWhenDucked) .setOnAudioFocusChangeListener(focusListener) .build(); rebuildAudioFocusRequest = false; } return audioManager.requestAudioFocus(audioFocusRequest); }
Example #4
Source File: AudioFocusManager.java From Telegram with GNU General Public License v2.0 | 6 votes |
@RequiresApi(26) private int requestAudioFocusV26() { if (audioFocusRequest == null || rebuildAudioFocusRequest) { AudioFocusRequest.Builder builder = audioFocusRequest == null ? new AudioFocusRequest.Builder(focusGain) : new AudioFocusRequest.Builder(audioFocusRequest); boolean willPauseWhenDucked = willPauseWhenDucked(); audioFocusRequest = builder .setAudioAttributes(Assertions.checkNotNull(audioAttributes).getAudioAttributesV21()) .setWillPauseWhenDucked(willPauseWhenDucked) .setOnAudioFocusChangeListener(focusListener) .build(); rebuildAudioFocusRequest = false; } return audioManager.requestAudioFocus(audioFocusRequest); }
Example #5
Source File: AudioFocusManager.java From Telegram-FOSS with GNU General Public License v2.0 | 6 votes |
@RequiresApi(26) private int requestAudioFocusV26() { if (audioFocusRequest == null || rebuildAudioFocusRequest) { AudioFocusRequest.Builder builder = audioFocusRequest == null ? new AudioFocusRequest.Builder(focusGain) : new AudioFocusRequest.Builder(audioFocusRequest); boolean willPauseWhenDucked = willPauseWhenDucked(); audioFocusRequest = builder .setAudioAttributes(Assertions.checkNotNull(audioAttributes).getAudioAttributesV21()) .setWillPauseWhenDucked(willPauseWhenDucked) .setOnAudioFocusChangeListener(focusListener) .build(); rebuildAudioFocusRequest = false; } return audioManager.requestAudioFocus(audioFocusRequest); }
Example #6
Source File: TtsService.java From android-app with GNU General Public License v3.0 | 6 votes |
private int requestAudioFocus() { int audioFocusResult; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { audioFocusResult = audioManager.requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } else { if (audioFocusRequest == null) { audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setOnAudioFocusChangeListener(audioFocusChangeListener) .setWillPauseWhenDucked(true) .setAudioAttributes( new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build() ) .build(); } audioFocusResult = audioManager.requestAudioFocus(audioFocusRequest); } return audioFocusResult; }
Example #7
Source File: Util.java From Popeens-DSub with GNU General Public License v3.0 | 6 votes |
@TargetApi(8) public static void requestAudioFocus(final Context context, final AudioManager audioManager) { if(Build.VERSION.SDK_INT >= 26) { if(audioFocusRequest == null) { AudioAttributes playbackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .build(); audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setAudioAttributes(playbackAttributes) .setOnAudioFocusChangeListener(getAudioFocusChangeListener(context, audioManager)) .setWillPauseWhenDucked(true) .build(); audioManager.requestAudioFocus(audioFocusRequest); } } else if (Build.VERSION.SDK_INT >= 8 && focusListener == null) { audioManager.requestAudioFocus(focusListener = getAudioFocusChangeListener(context, audioManager), AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
Example #8
Source File: AudioSinkActivity.java From video-quickstart-android with MIT License | 6 votes |
private void requestAudioFocus() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { AudioAttributes playbackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build(); AudioFocusRequest focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) .setAudioAttributes(playbackAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener( i -> { }) .build(); audioManager.requestAudioFocus(focusRequest); } else { audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); } }
Example #9
Source File: VideoInviteActivity.java From video-quickstart-android with MIT License | 6 votes |
private void requestAudioFocus() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { AudioAttributes playbackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build(); AudioFocusRequest focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) .setAudioAttributes(playbackAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener( i -> { }) .build(); audioManager.requestAudioFocus(focusRequest); } else { audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); } }
Example #10
Source File: AudioManager.java From QuranAndroid with GNU General Public License v3.0 | 6 votes |
private void requestAudioFocus(android.media.AudioManager audioManager) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { AudioAttributes playbackAttributes = new AudioAttributes.Builder() .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .setUsage(AudioAttributes.USAGE_MEDIA) .build(); AudioFocusRequest focusRequest = new AudioFocusRequest.Builder(android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) .setAudioAttributes(playbackAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener( new android.media.AudioManager.OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int i) { } }) .build(); audioManager.requestAudioFocus(focusRequest); } else { audioManager.requestAudioFocus(null, android.media.AudioManager.STREAM_VOICE_CALL, android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); } }
Example #11
Source File: AudioManagerCompat.java From mollyim-android with GNU General Public License v3.0 | 6 votes |
@Override public void requestCallAudioFocus() { if (audioFocusRequest != null) { Log.w(TAG, "Already requested audio focus. Ignoring..."); return; } audioFocusRequest = new AudioFocusRequest.Builder(AUDIOFOCUS_GAIN) .setAudioAttributes(AUDIO_ATTRIBUTES) .setOnAudioFocusChangeListener(onAudioFocusChangeListener) .build(); int result = audioManager.requestAudioFocus(audioFocusRequest); if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.w(TAG, "Audio focus not granted. Result code: " + result); } }
Example #12
Source File: TwilioVideoActivity.java From cordova-plugin-twilio-video with MIT License | 6 votes |
private void requestAudioFocus() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { AudioAttributes playbackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build(); AudioFocusRequest focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) .setAudioAttributes(playbackAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener( new AudioManager.OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int i) { } }) .build(); audioManager.requestAudioFocus(focusRequest); } else { audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); } }
Example #13
Source File: TwilioVoiceModule.java From react-native-twilio-programmable-voice with MIT License | 5 votes |
private void setAudioFocus() { if (audioManager == null) { return; } originalAudioMode = audioManager.getMode(); // Request audio focus before making any device switch if (Build.VERSION.SDK_INT >= 26) { AudioAttributes playbackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build(); focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE) .setAudioAttributes(playbackAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener(new AudioManager.OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int i) { } }) .build(); audioManager.requestAudioFocus(focusRequest); } else { audioManager.requestAudioFocus( null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE ); } /* * Start by setting MODE_IN_COMMUNICATION as default audio mode. It is * required to be in this mode when playout and/or recording starts for * best possible VoIP performance. Some devices have difficulties with speaker mode * if this is not set. */ audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); }
Example #14
Source File: ReadAloudService.java From a with GNU General Public License v3.0 | 5 votes |
@RequiresApi(api = Build.VERSION_CODES.O) private void initFocusRequest() { AudioAttributes mPlaybackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .build(); mFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setAudioAttributes(mPlaybackAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener(audioFocusChangeListener) .build(); }
Example #15
Source File: AudioFocusRequestCompat.java From carstream-android-auto with Apache License 2.0 | 5 votes |
@RequiresApi(Build.VERSION_CODES.O) /* package */ AudioFocusRequest getAudioFocusRequest() { return new AudioFocusRequest.Builder(mFocusGain) .setAudioAttributes(getAudioAttributes()) .setAcceptsDelayedFocusGain(mAcceptsDelayedFocusGain) .setWillPauseWhenDucked(mPauseOnDuck) .setOnAudioFocusChangeListener(mOnAudioFocusChangeListener, mFocusChangeHandler) .build(); }
Example #16
Source File: VideoView.java From ExoMedia with Apache License 2.0 | 5 votes |
/** * Requests to obtain the audio focus * * @return True if the focus was granted */ public boolean requestFocus() { if (!handleAudioFocus || currentFocus == AudioManager.AUDIOFOCUS_GAIN) { return true; } if (audioManager == null) { return false; } int status; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { AudioFocusRequest.Builder afBuilder = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN); AudioAttributes.Builder aaBuilder = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC); lastFocusRequest = afBuilder.setAudioAttributes(aaBuilder.build()).build(); status = audioManager.requestAudioFocus(lastFocusRequest); } else { status = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } if (AudioManager.AUDIOFOCUS_REQUEST_GRANTED == status) { currentFocus = AudioManager.AUDIOFOCUS_GAIN; return true; } startRequested = true; return false; }
Example #17
Source File: AudioManagerCompat.java From Jockey with Apache License 2.0 | 5 votes |
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); } }
Example #18
Source File: MusicControlAudioFocusListener.java From react-native-music-control with MIT License | 5 votes |
public void requestAudioFocus() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setOnAudioFocusChangeListener(this).build(); mAudioManager.requestAudioFocus(mFocusRequest); } else { mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
Example #19
Source File: AudioFocusManager.java From HaoReader with GNU General Public License v3.0 | 5 votes |
@RequiresApi(api = Build.VERSION_CODES.O) private void initFocusRequest() { AudioAttributes audioAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .build(); mFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setAudioAttributes(audioAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener(this) .build(); }
Example #20
Source File: CustomDeviceActivity.java From voice-quickstart-android with MIT License | 5 votes |
private void setAudioFocus(boolean setFocus) { if (audioManager != null) { if (setFocus) { savedAudioMode = audioManager.getMode(); // Request audio focus before making any device switch. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { AudioAttributes playbackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build(); AudioFocusRequest focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) .setAudioAttributes(playbackAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener(i -> { }) .build(); audioManager.requestAudioFocus(focusRequest); } else { audioManager.requestAudioFocus( focusChange -> { }, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); } /* * Start by setting MODE_IN_COMMUNICATION as default audio mode. It is * required to be in this mode when playout and/or recording starts for * best possible VoIP performance. Some devices have difficulties with speaker mode * if this is not set. */ audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); } else { audioManager.setMode(savedAudioMode); audioManager.abandonAudioFocus(null); } } }
Example #21
Source File: ReadAloudService.java From MyBookshelf with GNU General Public License v3.0 | 5 votes |
@RequiresApi(api = Build.VERSION_CODES.O) private void initFocusRequest() { AudioAttributes mPlaybackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .build(); mFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setAudioAttributes(mPlaybackAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener(audioFocusChangeListener) .build(); }
Example #22
Source File: TwilioVoicePlugin.java From cordova-plugin-twiliovoicesdk with MIT License | 5 votes |
private void setAudioFocus(boolean setFocus) { if (audioManager != null) { if (setFocus) { savedAudioMode = audioManager.getMode(); // Request audio focus before making any device switch. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { AudioAttributes playbackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build(); AudioFocusRequest focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) .setAudioAttributes(playbackAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener(new AudioManager.OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int i) { } }) .build(); audioManager.requestAudioFocus(focusRequest); } else { audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); } /* * Start by setting MODE_IN_COMMUNICATION as default audio mode. It is * required to be in this mode when playout and/or recording starts for * best possible VoIP performance. Some devices have difficulties with speaker mode * if this is not set. */ audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); } else { audioManager.setMode(savedAudioMode); audioManager.abandonAudioFocus(null); } } }
Example #23
Source File: AppRTCAudioManager.java From sealrtc-android with MIT License | 5 votes |
private AudioFocusRequest requestAudioFocus(AudioManager audioManager) { AudioFocusRequest request = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { request = getAudioFocusRequest(); audioManager.requestAudioFocus(request); } else { // Request audio focus before making any device switch. audioManager.requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); } return request; }
Example #24
Source File: CustomTwilioVideoView.java From react-native-twilio-video-webrtc with MIT License | 4 votes |
private void setAudioFocus(boolean focus) { if (focus) { previousAudioMode = audioManager.getMode(); // Request audio focus before making any device switch. if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { audioManager.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); } else { playbackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build(); audioFocusRequest = new AudioFocusRequest .Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) .setAudioAttributes(playbackAttributes) .setAcceptsDelayedFocusGain(true) .setOnAudioFocusChangeListener(this, handler) .build(); audioManager.requestAudioFocus(audioFocusRequest); } /* * Use MODE_IN_COMMUNICATION as the default audio mode. It is required * to be in this mode when playout and/or recording starts for the best * possible VoIP performance. Some devices have difficulties with * speaker mode if this is not set. */ audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); audioManager.setSpeakerphoneOn(!audioManager.isWiredHeadsetOn()); getContext().registerReceiver(myNoisyAudioStreamReceiver, intentFilter); } else { if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { audioManager.abandonAudioFocus(this); } else if (audioFocusRequest != null) { audioManager.abandonAudioFocusRequest(audioFocusRequest); } audioManager.setSpeakerphoneOn(false); audioManager.setMode(previousAudioMode); try { if (myNoisyAudioStreamReceiver != null) { getContext().unregisterReceiver(myNoisyAudioStreamReceiver); } myNoisyAudioStreamReceiver = null; } catch (Exception e) { // already registered e.printStackTrace(); } } }
Example #25
Source File: Api26AudioFocusDelegate.java From graphhopper-navigation-android with MIT License | 4 votes |
Api26AudioFocusDelegate(AudioManager audioManager) { this.audioManager = audioManager; audioFocusRequest = new AudioFocusRequest.Builder(FOCUS_GAIN).build(); }
Example #26
Source File: RNJWPlayerView.java From react-native-jw-media-player with MIT License | 4 votes |
public void requestAudioFocus() { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ if (hasAudioFocus) { return; } if (audioManager != null) { AudioAttributes playbackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) // CONTENT_TYPE_SPEECH .build(); focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setAudioAttributes(playbackAttributes) .setAcceptsDelayedFocusGain(true) // .setWillPauseWhenDucked(true) .setOnAudioFocusChangeListener(this) .build(); int res = audioManager.requestAudioFocus(focusRequest); synchronized(focusLock) { if (res == AudioManager.AUDIOFOCUS_REQUEST_FAILED) { playbackNowAuthorized = false; } else if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { playbackNowAuthorized = true; hasAudioFocus = true; } else if (res == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) { playbackDelayed = true; playbackNowAuthorized = false; } } Log.e(TAG, "audioRequest: " + res); } } else { int result = 0; if (audioManager != null) { if (hasAudioFocus) { return; } result = audioManager.requestAudioFocus(this, // Use the music stream. AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); } if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { hasAudioFocus = true; } Log.e(TAG, "audioRequest: " + result); } }
Example #27
Source File: PlayerService.java From AndroidAudioExample with MIT License | 4 votes |
@Override public void onCreate() { super.onCreate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_DEFAULT_CHANNEL_ID, getString(R.string.notification_channel_name), NotificationManagerCompat.IMPORTANCE_DEFAULT); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(notificationChannel); AudioAttributes audioAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .build(); audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setOnAudioFocusChangeListener(audioFocusChangeListener) .setAcceptsDelayedFocusGain(false) .setWillPauseWhenDucked(true) .setAudioAttributes(audioAttributes) .build(); } audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); mediaSession = new MediaSessionCompat(this, "PlayerService"); mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); mediaSession.setCallback(mediaSessionCallback); Context appContext = getApplicationContext(); Intent activityIntent = new Intent(appContext, MainActivity.class); mediaSession.setSessionActivity(PendingIntent.getActivity(appContext, 0, activityIntent, 0)); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null, appContext, MediaButtonReceiver.class); mediaSession.setMediaButtonReceiver(PendingIntent.getBroadcast(appContext, 0, mediaButtonIntent, 0)); exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultRenderersFactory(this), new DefaultTrackSelector(), new DefaultLoadControl()); exoPlayer.addListener(exoPlayerListener); DataSource.Factory httpDataSourceFactory = new OkHttpDataSourceFactory(new OkHttpClient(), Util.getUserAgent(this, getString(R.string.app_name))); Cache cache = new SimpleCache(new File(this.getCacheDir().getAbsolutePath() + "/exoplayer"), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 100)); // 100 Mb max this.dataSourceFactory = new CacheDataSourceFactory(cache, httpDataSourceFactory, CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR); this.extractorsFactory = new DefaultExtractorsFactory(); }