Skip to content

Commit

Permalink
core: better player error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Feb 18, 2024
1 parent 250fb05 commit 57b3cea
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 76 deletions.
74 changes: 39 additions & 35 deletions lib/base/audio_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
bool get isCurrentAudioFromCache => _isCurrentAudioFromCache;
bool _isCurrentAudioFromCache = false;

// Completer<void>? _audioShouldBeLoading;

Future<void> setAudioOnlyPlayback(bool audioOnly) async {
settings.save(ytIsAudioOnlyMode: audioOnly);
if (audioOnly) {
Expand Down Expand Up @@ -507,42 +505,44 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {

try {
duration = await setPls();
} catch (e) {
} on Exception catch (e) {
if (duration != null && currentPositionMS > 0) return;
if (item.track == currentTrack.track) {
// -- playing music from root folders still require `all_file_access`
// -- this is a fix for not playing some external files reported by some users.
final hadPermissionBefore = await Permission.manageExternalStorage.isGranted;
if (hadPermissionBefore) {
pause();
cancelPlayErrorSkipTimer();
_playErrorRemainingSecondsToSkip.value = 7;

_playErrorSkipTimer = Timer.periodic(
const Duration(seconds: 1),
(timer) {
_playErrorRemainingSecondsToSkip.value--;
if (_playErrorRemainingSecondsToSkip.value <= 0) {
NamidaNavigator.inst.closeDialog();
skipToNext();
timer.cancel();
}
},
);
NamidaDialogs.inst.showTrackDialog(
tr,
isFromPlayerQueue: true,
errorPlayingTrack: true,
source: QueueSource.playerQueue,
);
if (item.track != currentTrack.track) return;
printy(e, isError: true);
// -- playing music from root folders still require `all_file_access`
// -- this is a fix for not playing some external files reported by some users.
final hadPermissionBefore = await Permission.manageExternalStorage.isGranted;
if (hadPermissionBefore) {
pause();
cancelPlayErrorSkipTimer();
_playErrorRemainingSecondsToSkip.value = 7;

_playErrorSkipTimer = Timer.periodic(
const Duration(seconds: 1),
(timer) {
_playErrorRemainingSecondsToSkip.value--;
if (_playErrorRemainingSecondsToSkip.value <= 0) {
NamidaNavigator.inst.closeDialog();
skipToNext();
timer.cancel();
}
},
);
NamidaDialogs.inst.showTrackDialog(
tr,
isFromPlayerQueue: true,
errorPlayingTrack: e,
source: QueueSource.playerQueue,
);
return;
} else {
final hasPermission = await requestManageStoragePermission();
if (hasPermission) {
duration = await setPls();
} else {
final hasPermission = await requestManageStoragePermission();
if (hasPermission) await setPls();
return;
}
}

printy(e, isError: true);
return;
}

if (initialVideo == null) VideoController.inst.updateCurrentVideo(tr, returnEarly: false);
Expand Down Expand Up @@ -788,6 +788,8 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
whatToAwait: () async => await playerStoppingSeikoo.future,
startPlaying: startPlaying,
);
if (item != currentVideo) return;

currentCachedAudio.value = playedFromCacheDetails.audio;
currentCachedVideo.value = playedFromCacheDetails.video;

Expand All @@ -799,6 +801,8 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
heyIhandledAudioPlaying = false;
}

if (item != currentVideo) return;

if (ConnectivityController.inst.hasConnection) {
try {
YoutubeVideo? streams;
Expand Down Expand Up @@ -1337,7 +1341,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
if (cachedAudioPath != null) {
File(cachedAudioPath).setLastAccessedTry(DateTime.now());
}
return await setAudioSource(
return setAudioSource(
source,
preload: preload,
initialIndex: initialIndex,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/dialogs/common_dialogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class NamidaDialogs {
int? index,
bool comingFromQueue = false,
bool isFromPlayerQueue = false,
bool errorPlayingTrack = false,
Exception? errorPlayingTrack,
required QueueSource source,
String? additionalHero,
}) async {
Expand Down
93 changes: 54 additions & 39 deletions lib/ui/dialogs/general_popup_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,27 @@ Future<void> showGeneralPopupDialog(
bool useTrackTileCacheHeight = false,
bool isCircle = false,
bool isFromPlayerQueue = false,
bool errorPlayingTrack = false,
Exception? errorPlayingTrack,
String? artistToAddFrom,
(String, String)? albumToAddFrom,
String? heroTag,
String? additionalHero,
}) async {
final tracksExisting = <Track>[];
tracks.loop((t, index) {
final existingTrack = t.path.toTrackOrNull();
if (existingTrack != null) tracksExisting.add(existingTrack);
});
if (errorPlayingTrack != null) {
// -- fill using real-time checks if there was an error.
tracks.loop((t, index) {
if (File(t.path).existsSync()) tracksExisting.add(t);
});
} else {
tracks.loop((t, index) {
final existingTrack = t.path.toTrackOrNull();
if (existingTrack != null) tracksExisting.add(existingTrack);
});
}

final isSingle = tracks.length == 1;
forceSingleArtwork ??= isSingle;
final doesTracksExist = !errorPlayingTrack && tracksExisting.isNotEmpty;

final trackToExtractColorFrom = tracks.isEmpty
? null
Expand Down Expand Up @@ -758,14 +764,27 @@ Future<void> showGeneralPopupDialog(
),

/// if the track doesnt exist
!doesTracksExist
errorPlayingTrack != null || tracksExisting.isEmpty
? Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
'${errorPlayingTrack ? lang.ERROR_PLAYING_TRACK : lang.TRACK_NOT_FOUND}.\n${lang.PROMPT_TO_CHANGE_TRACK_PATH}',
style: theme.textTheme.displayMedium,
child: DecoratedBox(
decoration: BoxDecoration(
border: Border.all(
color: Colors.red.withOpacity(0.3),
),
borderRadius: BorderRadius.circular(12.0.multipliedRadius),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
tracksExisting.isEmpty
? "${lang.TRACK_NOT_FOUND}.\n${lang.PROMPT_TO_CHANGE_TRACK_PATH}"
: "${lang.ERROR_PLAYING_TRACK}.\n${errorPlayingTrack.toString()}",
style: theme.textTheme.displayMedium,
),
),
),
),
if (isSingle) ...[
Expand Down Expand Up @@ -806,35 +825,31 @@ Future<void> showGeneralPopupDialog(
await pickDirectoryToUpdateTrack();
},
),
if (errorPlayingTrack)
() {
void onSkip() {
cancelSkipTimer();
NamidaNavigator.inst.closeDialog();
Player.inst.next();
}

return Obx(
() {
final remainingSecondsToSkip = Player.inst.playErrorRemainingSecondsToSkip;
return SmallListTile(
title: lang.SKIP,
subtitle: remainingSecondsToSkip <= 0 ? null : '$remainingSecondsToSkip ${lang.SECONDS}',
color: colorDelightened.value,
compact: true,
icon: Broken.next,
trailing: remainingSecondsToSkip <= 0
? null
: NamidaIconButton(
icon: Broken.close_circle,
iconColor: Get.context?.defaultIconColor(colorDelightened.value, theme.textTheme.displayMedium?.color),
onPressed: cancelSkipTimer,
),
onTap: onSkip,
);
},
);
}()
if (errorPlayingTrack != null)
Obx(
() {
final remainingSecondsToSkip = Player.inst.playErrorRemainingSecondsToSkip;
return SmallListTile(
title: lang.SKIP,
subtitle: remainingSecondsToSkip <= 0 ? null : '$remainingSecondsToSkip ${lang.SECONDS}',
color: colorDelightened.value,
compact: true,
icon: Broken.next,
trailing: remainingSecondsToSkip <= 0
? null
: NamidaIconButton(
icon: Broken.close_circle,
iconColor: Get.context?.defaultIconColor(colorDelightened.value, theme.textTheme.displayMedium?.color),
onPressed: cancelSkipTimer,
),
onTap: () {
cancelSkipTimer();
NamidaNavigator.inst.closeDialog();
Player.inst.next();
},
);
},
),
],
advancedStuffListTile,
if (removeFromPlaylistListTile != null) removeFromPlaylistListTile,
Expand Down
1 change: 1 addition & 0 deletions lib/ui/pages/equalizer_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class EqualizerPageState extends State<EqualizerPage> with WidgetsBindingObserve
void _resetPreset({bool writeSettings = true}) {
_activePresetCustom.value = true;
_activePreset.value = '';
_equalizer.setPreset(null);
if (writeSettings && settings.equalizer.preset != null) {
settings.equalizer.save(preset: null, resetPreset: true);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ dependencies:
basic_audio_handler:
git:
url: https://github.com/namidaco/basic_audio_handler
ref: 2cd4ec054703aa1cdd7a4ddc61021c07aaa457c4
ref: b033a802fda0c796a45c6030d5625daad616c0fc
on_audio_query:
git:
url: https://github.com/MSOB7YY/on_audio_query
Expand Down

0 comments on commit 57b3cea

Please sign in to comment.