Skip to content

Commit

Permalink
FEAT: Crossfade support
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Oct 27, 2023
1 parent e225098 commit 1dda153
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 110 deletions.
48 changes: 30 additions & 18 deletions lib/controller/audio_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,14 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
WaveformController.inst.generateWaveform(tr);

Future<void> setPls() async {
final dur = await setAudioSource(tr.toAudioSource(currentIndex, currentQueue.length));
final dur = await setAudioSource(
tr.toAudioSource(currentIndex, currentQueue.length),
startPlaying: startPlaying,
);
if (tr.duration == 0) {
tr.duration = dur?.inSeconds ?? 0;
refreshNotification(currentItem);
}
refreshNotification(currentItem);
}

try {
Expand All @@ -503,7 +506,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
_playErrorRemainingSecondsToSkip.value--;
if (_playErrorRemainingSecondsToSkip.value <= 0) {
NamidaNavigator.inst.closeDialog();
Player.inst.next();
skipToNext();
timer.cancel();
}
},
Expand All @@ -527,12 +530,12 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
// -- The whole idea of pausing and playing is due to the bug where [headset buttons/android next gesture]
// -- sometimes don't get detected.
await Future.wait([
onPauseRaw(),
// onPauseRaw(),
tryRestoringLastPosition(tr),
]);

if (startPlaying) {
setVolume(settings.playerVolume.value);
setVolume(userPlayerVolume);
await _waitForAllBuffers();
await _playAudioThenVideo();
}
Expand Down Expand Up @@ -611,6 +614,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
final cachedAudio = stream?.getCachedFile(videoId);
if (cachedAudio != null && useCache) {
await setAudioSource(AudioSource.file(cachedAudio.path, tag: mediaItem));
refreshNotification();
} else if (stream != null && stream.url != null) {
if (wasPlaying) {
await Future.wait([
Expand All @@ -630,6 +634,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
},
),
);
refreshNotification();
}

try {
Expand Down Expand Up @@ -725,7 +730,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {

Future<void> plsplsplsPlay(bool waitForBuffer, bool wasPlayingFromCache, bool sourceChanged) async {
if (startPlaying) {
setVolume(settings.playerVolume.value);
setVolume(userPlayerVolume);
if (waitForBuffer) await _waitForAllBuffers();
await _playAudioThenVideo();
settings.wakelockMode.value.toggleOn(currentVideoStream.value != null || currentCachedVideo.value != null);
Expand Down Expand Up @@ -862,6 +867,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
),
),
]);
refreshNotification();
}
} catch (e) {
if (item != currentVideo) return; // race avoidance when playing multiple videos
Expand Down Expand Up @@ -954,6 +960,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
langaugeName: cachedAudio.langaugeName,
file: cachedAudio.file,
);
refreshNotification();
return (audioDetails, cachedVideo);
} else if (cachedAudio != null && canPlayAudioOnly) {
// -- play audio only
Expand All @@ -966,6 +973,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
langaugeName: cachedAudio.langaugeName,
file: cachedAudio.file,
);
refreshNotification();
return (audioDetails, null);
}
return (null, null);
Expand Down Expand Up @@ -1080,6 +1088,15 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
);
}

@override
bool get enableCrossFade => settings.enableCrossFade.value;

@override
int get defaultCrossFadeMilliseconds => settings.crossFadeDurationMS.value;

@override
int get defaultCrossFadeTriggerStartOffsetSeconds => settings.crossFadeAutoTriggerSeconds.value;

@override
bool get displayFavouriteButtonInNotification => settings.displayFavouriteButtonInNotification.value;

Expand Down Expand Up @@ -1138,14 +1155,10 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
}

@override
Future<void> play() async {
await onPlay();
}
Future<void> play() async => await onPlay();

@override
Future<void> pause() async {
await onPause();
}
Future<void> pause() async => await onPause();

Future<void> togglePlayPause() async {
if (isPlaying) {
Expand All @@ -1157,8 +1170,6 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {

@override
Future<void> seek(Duration position) async {
final wasPlaying = isPlaying;

Future<void> plsSeek() async => await onSeek(position);

Future<void> plsPause() async {
Expand All @@ -1170,10 +1181,12 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {

await currentItem?._execute(
selectable: (finalItem) async {
await plsPause();
// await plsPause();
await plsSeek();
refreshVideoPosition(false);
},
youtubeID: (finalItem) async {
final wasPlaying = isPlaying;
await plsPause();
if (_nextSeekCanSetAudioCache) {
// -- try putting cache version if it was cached
Expand All @@ -1183,11 +1196,10 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
_isCurrentAudioFromCache = true;
}
await plsSeek();
await _waitForAllBuffers();
if (wasPlaying) await _playAudioThenVideo();
},
);

await _waitForAllBuffers();
if (wasPlaying) await _playAudioThenVideo();
}

@override
Expand Down
21 changes: 21 additions & 0 deletions lib/controller/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ class SettingsController {
final RxInt playerPlayFadeDurInMilli = 300.obs;
final RxInt playerPauseFadeDurInMilli = 300.obs;
final RxInt minTrackDurationToRestoreLastPosInMinutes = 5.obs;
final RxBool enableCrossFade = true.obs;
final RxInt lastPlayedTrackIndex = 0.obs;
final RxInt crossFadeDurationMS = 500.obs;
final RxInt crossFadeAutoTriggerSeconds = 5.obs;
final RxBool displayFavouriteButtonInNotification = false.obs;
final RxBool enableSearchCleanup = true.obs;
final RxBool enableBottomNavBar = true.obs;
Expand Down Expand Up @@ -386,7 +389,10 @@ class SettingsController {
playerPlayFadeDurInMilli.value = json['playerPlayFadeDurInMilli'] ?? playerPlayFadeDurInMilli.value;
playerPauseFadeDurInMilli.value = json['playerPauseFadeDurInMilli'] as int? ?? playerPauseFadeDurInMilli.value;
minTrackDurationToRestoreLastPosInMinutes.value = json['minTrackDurationToRestoreLastPosInMinutes'] ?? minTrackDurationToRestoreLastPosInMinutes.value;
enableCrossFade.value = json['enableCrossFade'] ?? enableCrossFade.value;
lastPlayedTrackIndex.value = json['lastPlayedTrackIndex'] ?? lastPlayedTrackIndex.value;
crossFadeDurationMS.value = json['crossFadeDurationMS'] ?? crossFadeDurationMS.value;
crossFadeAutoTriggerSeconds.value = json['crossFadeAutoTriggerSeconds'] ?? crossFadeAutoTriggerSeconds.value;
displayFavouriteButtonInNotification.value = json['displayFavouriteButtonInNotification'] ?? displayFavouriteButtonInNotification.value;
enableSearchCleanup.value = json['enableSearchCleanup'] ?? enableSearchCleanup.value;
enableBottomNavBar.value = json['enableBottomNavBar'] ?? enableBottomNavBar.value;
Expand Down Expand Up @@ -584,7 +590,10 @@ class SettingsController {
'playerPlayFadeDurInMilli': playerPlayFadeDurInMilli.value,
'playerPauseFadeDurInMilli': playerPauseFadeDurInMilli.value,
'minTrackDurationToRestoreLastPosInMinutes': minTrackDurationToRestoreLastPosInMinutes.value,
'enableCrossFade': enableCrossFade.value,
'lastPlayedTrackIndex': lastPlayedTrackIndex.value,
'crossFadeDurationMS': crossFadeDurationMS.value,
'crossFadeAutoTriggerSeconds': crossFadeAutoTriggerSeconds.value,
'displayFavouriteButtonInNotification': displayFavouriteButtonInNotification.value,
'enableSearchCleanup': enableSearchCleanup.value,
'enableBottomNavBar': enableBottomNavBar.value,
Expand Down Expand Up @@ -753,7 +762,10 @@ class SettingsController {
int? playerPlayFadeDurInMilli,
int? playerPauseFadeDurInMilli,
int? minTrackDurationToRestoreLastPosInMinutes,
bool? enableCrossFade,
int? lastPlayedTrackIndex,
int? crossFadeDurationMS,
int? crossFadeAutoTriggerSeconds,
bool? displayFavouriteButtonInNotification,
bool? enableSearchCleanup,
bool? enableBottomNavBar,
Expand Down Expand Up @@ -1142,9 +1154,18 @@ class SettingsController {
if (minTrackDurationToRestoreLastPosInMinutes != null) {
this.minTrackDurationToRestoreLastPosInMinutes.value = minTrackDurationToRestoreLastPosInMinutes;
}
if (enableCrossFade != null) {
this.enableCrossFade.value = enableCrossFade;
}
if (lastPlayedTrackIndex != null) {
this.lastPlayedTrackIndex.value = lastPlayedTrackIndex;
}
if (crossFadeDurationMS != null) {
this.crossFadeDurationMS.value = crossFadeDurationMS;
}
if (crossFadeAutoTriggerSeconds != null) {
this.crossFadeAutoTriggerSeconds.value = crossFadeAutoTriggerSeconds;
}
if (displayFavouriteButtonInNotification != null) {
this.displayFavouriteButtonInNotification.value = displayFavouriteButtonInNotification;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/core/translations/keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ class LanguageKeys {
late String CREATE;
late String CREATED_BACKUP_SUCCESSFULLY_SUB;
late String CREATED_BACKUP_SUCCESSFULLY;
late String CROSSFADE_DURATION;
late String CROSSFADE_TRIGGER_SECONDS;
late String CROSSFADE_TRIGGER_SECONDS_DISABLED;
late String CUSTOM;
late String CUSTOMIZATIONS_SUBTITLE;
late String CUSTOMIZATIONS;
Expand Down Expand Up @@ -159,6 +162,7 @@ class LanguageKeys {
late String ENABLE_BLUR_EFFECT;
late String ENABLE_BOTTOM_NAV_BAR_SUBTITLE;
late String ENABLE_BOTTOM_NAV_BAR;
late String ENABLE_CROSSFADE_EFFECT;
late String ENABLE_FADE_EFFECT_ON_PLAY_PAUSE;
late String ENABLE_FOLDERS_HIERARCHY;
late String ENABLE_GLOW_EFFECT;
Expand Down Expand Up @@ -245,6 +249,8 @@ class LanguageKeys {
late String IF_NOT_PLAYING;
late String IGNORES;
late String IGNORE_BATTERY_OPTIMIZATIONS_SUBTITLE;
late String IMMERSIVE_MODE;
late String IMMERSIVE_MODE_SUBTITLE;
late String IMPORT_ALL;
late String IMPORT_LAST_FM_HISTORY_GUIDE;
late String IMPORT_LAST_FM_HISTORY;
Expand Down
11 changes: 11 additions & 0 deletions lib/core/translations/language.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ class Language extends LanguageKeys {
CREATE = getKey("CREATE");
CREATED_BACKUP_SUCCESSFULLY_SUB = getKey("CREATED_BACKUP_SUCCESSFULLY_SUB");
CREATED_BACKUP_SUCCESSFULLY = getKey("CREATED_BACKUP_SUCCESSFULLY");
CROSSFADE_DURATION = getKey("CROSSFADE_DURATION");
CROSSFADE_TRIGGER_SECONDS = getKey("CROSSFADE_TRIGGER_SECONDS");
CROSSFADE_TRIGGER_SECONDS_DISABLED = getKey("CROSSFADE_TRIGGER_SECONDS_DISABLED");
CUSTOM = getKey("CUSTOM");
CUSTOMIZATIONS_SUBTITLE = getKey("CUSTOMIZATIONS_SUBTITLE");
CUSTOMIZATIONS = getKey("CUSTOMIZATIONS");
Expand Down Expand Up @@ -226,6 +229,7 @@ class Language extends LanguageKeys {
ENABLE_BLUR_EFFECT = getKey("ENABLE_BLUR_EFFECT");
ENABLE_BOTTOM_NAV_BAR_SUBTITLE = getKey("ENABLE_BOTTOM_NAV_BAR_SUBTITLE");
ENABLE_BOTTOM_NAV_BAR = getKey("ENABLE_BOTTOM_NAV_BAR");
ENABLE_CROSSFADE_EFFECT = getKey("ENABLE_CROSSFADE_EFFECT");
ENABLE_FADE_EFFECT_ON_PLAY_PAUSE = getKey("ENABLE_FADE_EFFECT_ON_PLAY_PAUSE");
ENABLE_FOLDERS_HIERARCHY = getKey("ENABLE_FOLDERS_HIERARCHY");
ENABLE_GLOW_EFFECT = getKey("ENABLE_GLOW_EFFECT");
Expand Down Expand Up @@ -755,6 +759,13 @@ class Language extends LanguageKeys {













Expand Down
6 changes: 4 additions & 2 deletions lib/ui/widgets/custom_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ class NamidaExpansionTile extends StatelessWidget {
final bool initiallyExpanded;
final Widget? trailing;
final ValueChanged? onExpansionChanged;
final bool normalRightPadding;

const NamidaExpansionTile({
super.key,
Expand All @@ -862,6 +863,7 @@ class NamidaExpansionTile extends StatelessWidget {
this.initiallyExpanded = false,
this.trailing,
this.onExpansionChanged,
this.normalRightPadding = false,
});

@override
Expand All @@ -873,7 +875,7 @@ class NamidaExpansionTile extends StatelessWidget {
initiallyExpanded: initiallyExpanded,
onExpansionChanged: onExpansionChanged,
expandedAlignment: Alignment.centerLeft,
tilePadding: const EdgeInsets.only(left: 16.0, right: 12.0),
tilePadding: EdgeInsets.only(left: 16.0, right: normalRightPadding ? 16.0 : 12.0),
leading: leading ??
Icon(
icon,
Expand Down Expand Up @@ -1216,7 +1218,7 @@ class NamidaWheelSlider<T> extends StatelessWidget {
this.perspective = 0.01,
required this.totalCount,
required this.initValue,
required this.itemSize,
this.itemSize = 5.0,
this.squeeze = 1.0,
this.isInfinite = false,
required this.onValueChanged,
Expand Down
Loading

0 comments on commit 1dda153

Please sign in to comment.