diff options
author | ByteHamster <ByteHamster@users.noreply.github.com> | 2020-10-25 18:57:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-25 18:57:57 +0100 |
commit | aa3a350e38df8b98249543b2e63f95af9f6e6da5 (patch) | |
tree | dc3c65dcc514b0a9e661e90a442b35e8db6370f7 | |
parent | 2ae11e1d18a8d4d2765743bfec3485d72663c4af (diff) | |
parent | f536774f2e96d3e3001bb13b4776bb6ed7ad76ff (diff) | |
download | AntennaPod-aa3a350e38df8b98249543b2e63f95af9f6e6da5.zip |
Merge pull request #4592 from ByteHamster/audiofocus-reuse
Reuse audiofocus request object
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java | 45 |
1 files changed, 16 insertions, 29 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java index fbdc9a52b..ae5d62872 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java @@ -2,9 +2,7 @@ package de.danoeh.antennapod.core.service.playback; import android.content.Context; import android.media.AudioAttributes; -import android.media.AudioFocusRequest; import android.media.AudioManager; -import android.os.Build; import android.os.PowerManager; import androidx.annotation.NonNull; import android.telephony.TelephonyManager; @@ -12,11 +10,13 @@ import android.util.Log; import android.util.Pair; import android.view.SurfaceHolder; +import androidx.media.AudioAttributesCompat; +import androidx.media.AudioFocusRequestCompat; +import androidx.media.AudioManagerCompat; import org.antennapod.audio.MediaPlayer; import java.io.File; import java.io.IOException; -import java.util.EnumSet; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; @@ -57,6 +57,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { private final AtomicBoolean startWhenPrepared; private volatile boolean pausedBecauseOfTransientAudiofocusLoss; private volatile Pair<Integer, Integer> videoSize; + private final AudioFocusRequestCompat audioFocusRequest; /** * Some asynchronous calls might change the state of the MediaPlayer object. Therefore calls in other threads @@ -154,6 +155,16 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { pausedBecauseOfTransientAudiofocusLoss = false; mediaType = MediaType.UNKNOWN; videoSize = null; + + AudioAttributesCompat audioAttributes = new AudioAttributesCompat.Builder() + .setUsage(AudioAttributesCompat.USAGE_MEDIA) + .setContentType(AudioAttributesCompat.CONTENT_TYPE_SPEECH) + .build(); + audioFocusRequest = new AudioFocusRequestCompat.Builder(AudioManagerCompat.AUDIOFOCUS_GAIN) + .setAudioAttributes(audioAttributes) + .setOnAudioFocusChangeListener(audioFocusChangeListener) + .setWillPauseWhenDucked(true) + .build(); } /** @@ -287,25 +298,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { private void resumeSync() { if (playerStatus == PlayerStatus.PAUSED || playerStatus == PlayerStatus.PREPARED) { - int focusGained; - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - AudioAttributes audioAttributes = new AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_MEDIA) - .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) - .build(); - AudioFocusRequest audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) - .setAudioAttributes(audioAttributes) - .setOnAudioFocusChangeListener(audioFocusChangeListener) - .setAcceptsDelayedFocusGain(true) - .setWillPauseWhenDucked(true) - .build(); - focusGained = audioManager.requestAudioFocus(audioFocusRequest); - } else { - focusGained = audioManager.requestAudioFocus( - audioFocusChangeListener, AudioManager.STREAM_MUSIC, - AudioManager.AUDIOFOCUS_GAIN); - } + int focusGained = AudioManagerCompat.requestAudioFocus(audioManager, audioFocusRequest); if (focusGained == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.d(TAG, "Audiofocus successfully requested"); @@ -373,13 +366,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { } private void abandonAudioFocus() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - AudioFocusRequest.Builder builder = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) - .setOnAudioFocusChangeListener(audioFocusChangeListener); - audioManager.abandonAudioFocusRequest(builder.build()); - } else { - audioManager.abandonAudioFocus(audioFocusChangeListener); - } + AudioManagerCompat.abandonAudioFocusRequest(audioManager, audioFocusRequest); } /** |