Check if volume boost effect is supported on the device (#6808)

This commit is contained in:
Matej Drobnič 2023-12-29 17:15:21 +01:00 committed by GitHub
parent 58081fe5bf
commit f476086114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 5 deletions

View File

@ -0,0 +1,27 @@
package de.danoeh.antennapod.preferences;
import android.content.Context;
import android.util.AttributeSet;
import java.util.Arrays;
import de.danoeh.antennapod.model.feed.VolumeAdaptionSetting;
public class VolumeAdaptationPreference extends MaterialListPreference {
public VolumeAdaptationPreference(Context context) {
super(context);
}
public VolumeAdaptationPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public CharSequence[] getEntries() {
if (VolumeAdaptionSetting.isBoostSupported()) {
return super.getEntries();
} else {
return Arrays.copyOfRange(super.getEntries(), 0, 3);
}
}
}

View File

@ -48,7 +48,7 @@
android:summary="@string/global_default"
android:title="@string/auto_delete_label" />
<de.danoeh.antennapod.preferences.MaterialListPreference
<de.danoeh.antennapod.preferences.VolumeAdaptationPreference
android:defaultValue="off"
android:entries="@array/spnVolumeAdaptationItems"
android:entryValues="@array/spnVolumeAdaptationValues"

View File

@ -7,6 +7,7 @@ import android.text.TextUtils;
import android.util.Log;
import android.view.SurfaceHolder;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Consumer;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.DefaultLoadControl;
@ -37,6 +38,7 @@ import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.model.feed.VolumeAdaptionSetting;
import de.danoeh.antennapod.storage.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
import de.danoeh.antennapod.core.service.download.HttpCredentialEncoder;
@ -69,7 +71,8 @@ public class ExoPlayerWrapper {
private PlaybackParameters playbackParameters;
private DefaultTrackSelector trackSelector;
private LoudnessEnhancer loudnessEnhancer;
@Nullable
private LoudnessEnhancer loudnessEnhancer = null;
ExoPlayerWrapper(Context context) {
this.context = context;
@ -247,11 +250,15 @@ public class ExoPlayerWrapper {
public void setVolume(float v, float v1) {
if (v > 1) {
exoPlayer.setVolume(1f);
loudnessEnhancer.setEnabled(true);
loudnessEnhancer.setTargetGain((int) (1000 * (v - 1)));
if (loudnessEnhancer != null) {
loudnessEnhancer.setEnabled(true);
loudnessEnhancer.setTargetGain((int) (1000 * (v - 1)));
}
} else {
exoPlayer.setVolume(v);
loudnessEnhancer.setEnabled(false);
if (loudnessEnhancer != null) {
loudnessEnhancer.setEnabled(false);
}
}
}
@ -354,6 +361,10 @@ public class ExoPlayerWrapper {
}
private void initLoudnessEnhancer(int audioStreamId) {
if (!VolumeAdaptionSetting.isBoostSupported()) {
return;
}
LoudnessEnhancer newEnhancer = new LoudnessEnhancer(audioStreamId);
LoudnessEnhancer oldEnhancer = this.loudnessEnhancer;
if (oldEnhancer != null) {

View File

@ -1,6 +1,9 @@
package de.danoeh.antennapod.core.feed;
import de.danoeh.antennapod.model.feed.VolumeAdaptionSetting;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
@ -11,6 +14,16 @@ import static org.junit.Assert.assertTrue;
public class VolumeAdaptionSettingTest {
@Before
public void setUp() throws Exception {
VolumeAdaptionSetting.setBoostSupported(false);
}
@After
public void tearDown() throws Exception {
VolumeAdaptionSetting.setBoostSupported(null);
}
@Test
public void mapOffToInteger() {
VolumeAdaptionSetting setting = VolumeAdaptionSetting.OFF;

View File

@ -1,5 +1,10 @@
package de.danoeh.antennapod.model.feed;
import android.media.audiofx.AudioEffect;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
public enum VolumeAdaptionSetting {
OFF(0, 1.0f),
LIGHT_REDUCTION(1, 0.5f),
@ -32,4 +37,29 @@ public enum VolumeAdaptionSetting {
public float getAdaptionFactor() {
return adaptionFactor;
}
@Nullable
private static Boolean boostSupported = null;
public static boolean isBoostSupported() {
if (boostSupported != null) {
return boostSupported;
}
final AudioEffect.Descriptor[] audioEffects = AudioEffect.queryEffects();
if (audioEffects != null) {
for (AudioEffect.Descriptor effect : audioEffects) {
if (effect.type.equals(AudioEffect.EFFECT_TYPE_LOUDNESS_ENHANCER)) {
boostSupported = true;
return boostSupported;
}
}
}
boostSupported = false;
return boostSupported;
}
@VisibleForTesting
public static void setBoostSupported(@Nullable Boolean boostSupported) {
VolumeAdaptionSetting.boostSupported = boostSupported;
}
}