Skip to content

Commit 41de66d

Browse files
committed
Player: add audio delay switch
1 parent d739383 commit 41de66d

File tree

6 files changed

+45
-15
lines changed

6 files changed

+45
-15
lines changed

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/app/models/playback/controllers/HQDialogController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private void addAudioDelayCategory() {
134134
if (getPlayer() == null) {
135135
return;
136136
}
137-
addCategoryInt(AppDialogUtil.createAudioShiftCategory(getContext(),
137+
addCategoryInt(AppDialogUtil.createAudioDelayCategory(getContext(),
138138
() -> getPlayer().restartEngine()));
139139
}
140140

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/app/presenters/settings/PlayerSettingsPresenter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void show() {
6464
appendVideoZoomCategory(settingsPresenter);
6565
appendVideoSpeedCategory(settingsPresenter);
6666
appendAudioLanguageCategory(settingsPresenter);
67-
appendAudioShiftCategory(settingsPresenter);
67+
appendAudioDelayCategory(settingsPresenter);
6868
appendMasterVolumeCategory(settingsPresenter);
6969
appendOKButtonCategory(settingsPresenter);
7070
appendUIAutoHideCategory(settingsPresenter);
@@ -149,8 +149,8 @@ private void appendAudioLanguageCategory(AppDialogPresenter settingsPresenter) {
149149
settingsPresenter.appendCategory(category);
150150
}
151151

152-
private void appendAudioShiftCategory(AppDialogPresenter settingsPresenter) {
153-
OptionCategory category = AppDialogUtil.createAudioShiftCategory(getContext());
152+
private void appendAudioDelayCategory(AppDialogPresenter settingsPresenter) {
153+
OptionCategory category = AppDialogUtil.createAudioDelayCategory(getContext());
154154
settingsPresenter.appendCategory(category);
155155
}
156156

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/exoplayer/versions/renderer/CustomOverridesRenderersFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected void buildAudioRenderers(Context context, @ExtensionRendererMode int e
126126
super.buildAudioRenderers(context, extensionRendererMode, mediaCodecSelector, drmSessionManager, playClearSamplesWithoutKeys,
127127
enableDecoderFallback, audioProcessors, eventHandler, eventListener, out);
128128

129-
if (mPlayerData.getAudioDelayMs() == 0 && !mPlayerTweaksData.isAudioSyncFixEnabled()) {
129+
if ((mPlayerData.getAudioDelayMs() == 0 || !mPlayerData.isAudioDelayEnabled()) && !mPlayerTweaksData.isAudioSyncFixEnabled()) {
130130
// Improve performance a bit by eliminating calculations presented in custom renderer.
131131

132132
return;
@@ -136,7 +136,7 @@ protected void buildAudioRenderers(Context context, @ExtensionRendererMode int e
136136
new DelayMediaCodecAudioRenderer(context, mediaCodecSelector, drmSessionManager, playClearSamplesWithoutKeys, enableDecoderFallback,
137137
eventHandler, eventListener, new DefaultAudioSink(AudioCapabilities.getCapabilities(context), audioProcessors));
138138

139-
audioRenderer.setAudioDelayMs(mPlayerData.getAudioDelayMs());
139+
audioRenderer.setAudioDelayMs(mPlayerData.isAudioDelayEnabled() ? mPlayerData.getAudioDelayMs() : 0);
140140
audioRenderer.enableAudioSyncFix(mPlayerTweaksData.isAudioSyncFixEnabled());
141141

142142
replaceAudioRenderer(out, audioRenderer);

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/prefs/PlayerData.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public class PlayerData extends DataChangeBase implements PlayerConstants, Profi
100100
private List<String> mLastAudioLanguages;
101101
private final Runnable mPersistStateInt = this::persistStateInt;
102102
private boolean mIsLegacyCodecsForced;
103+
private boolean mIsAudioDelayEnabled;
103104

104105
private static class SpeedItem {
105106
public String channelId;
@@ -609,6 +610,15 @@ public void setSpeedPerChannelEnabled(boolean enable) {
609610
persistState();
610611
}
611612

613+
public boolean isAudioDelayEnabled() {
614+
return mIsAudioDelayEnabled;
615+
}
616+
617+
public void setAudioDelayEnabled(boolean enabled) {
618+
mIsAudioDelayEnabled = enabled;
619+
persistState();
620+
}
621+
612622
public int getAudioDelayMs() {
613623
return mAudioDelayMs;
614624
}
@@ -836,6 +846,7 @@ private void restoreState() {
836846
mIsSkipShortsEnabled = Helpers.parseBoolean(split, 59, false);
837847
mLastAudioLanguages = Helpers.parseStrList(split, 60);
838848
mIsVideoFlipEnabled = Helpers.parseBoolean(split, 61, false);
849+
mIsAudioDelayEnabled = Helpers.parseBoolean(split, 62, false);
839850

840851
if (speeds != null) {
841852
for (String speedSpec : speeds) {
@@ -872,7 +883,8 @@ private void persistStateInt() {
872883
mIsSeekConfirmPlayEnabled, mSeekIncrementMs, null, mSubtitleScale, mPlayerVolume, mIsTooltipsEnabled, mSubtitlePosition,
873884
mIsNumberKeySeekEnabled, mIsSkip24RateEnabled, mAfrPauseMs, mIsLiveChatEnabled, mLastSubtitleFormats, mLastSpeed, mRotationAngle,
874885
mZoomPercents, mPlaybackMode, mAudioLanguage, mSubtitleLanguage, mEnabledSubtitlesPerChannel, mIsSubtitlesPerChannelEnabled,
875-
mIsSpeedPerChannelEnabled, Helpers.mergeArray(mSpeeds.values().toArray()), mPitch, mIsSkipShortsEnabled, mLastAudioLanguages, mIsVideoFlipEnabled
886+
mIsSpeedPerChannelEnabled, Helpers.mergeArray(mSpeeds.values().toArray()), mPitch, mIsSkipShortsEnabled, mLastAudioLanguages,
887+
mIsVideoFlipEnabled, mIsAudioDelayEnabled
876888
));
877889
}
878890

common/src/main/java/com/liskovsoft/smartyoutubetv2/common/utils/AppDialogUtil.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,17 +401,27 @@ public static OptionCategory createAudioLanguageCategory(Context context, Runnab
401401
return OptionCategory.from(AUDIO_LANGUAGE_ID, OptionCategory.TYPE_RADIO_LIST, title, options);
402402
}
403403

404-
public static OptionCategory createAudioShiftCategory(Context context) {
405-
return createAudioShiftCategory(context, () -> {});
404+
public static OptionCategory createAudioDelayCategory(Context context) {
405+
return createAudioDelayCategory(context, () -> {});
406406
}
407407

408-
public static OptionCategory createAudioShiftCategory(Context context, Runnable onSetCallback) {
408+
public static OptionCategory createAudioDelayCategory(Context context, Runnable onSetCallback) {
409409
PlayerData playerData = PlayerData.instance(context);
410410
String title = context.getResources().getQuantityString(R.plurals.seconds, 10, context.getString(R.string.audio_shift));
411411

412-
OptionItem optionItem = UiOptionItem.from(title, item -> {
412+
List<OptionItem> items = new ArrayList<>();
413+
414+
OptionItem enableSwitch = UiOptionItem.from(context.getString(R.string.enable), item -> {
415+
if (playerData.getAudioDelayMs() != 0) {
416+
playerData.setAudioDelayEnabled(item.isSelected());
417+
onSetCallback.run();
418+
}
419+
}, playerData.isAudioDelayEnabled());
420+
421+
OptionItem audioDelay = UiOptionItem.from(title, item -> {
413422
AppDialogPresenter.instance(context).closeDialog();
414-
float delaySec = playerData.getAudioDelayMs() / 1_000f;
423+
int delayMs = playerData.getAudioDelayMs();
424+
float delaySec = delayMs / 1_000f;
415425
SimpleEditDialog.show(
416426
context,
417427
title,
@@ -424,18 +434,26 @@ public static OptionCategory createAudioShiftCategory(Context context, Runnable
424434
float newDelaySec = Helpers.parseFloat(newValue);
425435
int newDelayMs = (int) (newDelaySec * 1_000);
426436

437+
if (newDelayMs == delayMs) {
438+
return false;
439+
}
440+
427441
if (newDelayMs == 0 && newDelaySec != 0) { // lower than 1ms
428442
return false;
429443
}
430444

431445
playerData.setAudioDelayMs(newDelayMs);
446+
playerData.setAudioDelayEnabled(newDelayMs != 0);
432447
onSetCallback.run();
433448
return true;
434449
}
435450
);
436-
});
451+
}, playerData.isAudioDelayEnabled());
452+
453+
items.add(enableSwitch);
454+
items.add(audioDelay);
437455

438-
return OptionCategory.from(AUDIO_DELAY_ID, OptionCategory.TYPE_SINGLE_BUTTON, title, optionItem);
456+
return OptionCategory.from(AUDIO_DELAY_ID, OptionCategory.TYPE_CHECKBOX_LIST, title, items);
439457
}
440458

441459
public static OptionCategory createAudioVolumeCategory(Context context) {

common/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
<string name="wait_data_loading">Please wait while data is loading…</string>
170170
<string name="auto_frame_rate_pause">Auto Frame Rate (pause when switching)</string>
171171
<string name="auto_frame_rate_applying">Applying auto frame rate %sx%s\@%s</string>
172-
<string name="audio_shift">Audio shift</string>
172+
<string name="audio_shift">Audio delay</string>
173173
<string name="player_remember_speed">Remember speed</string>
174174
<string name="mark_channel_as_watched">Mark as watched</string>
175175
<string name="channel_marked_as_watched">Channel has been marked as watched</string>

0 commit comments

Comments
 (0)