Reuse audiofocus request object

This commit is contained in:
ByteHamster 2020-10-24 23:41:44 +02:00
parent 69a3c56d24
commit f536774f2e
1 changed files with 16 additions and 29 deletions

View File

@ -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);
}
/**