Skip to content

Commit

Permalink
Merge pull request #357 from lamarios/fix/2023-10-29-fixes
Browse files Browse the repository at this point in the history
Fix/2023 10 29 fixes
  • Loading branch information
lamarios authored Oct 29, 2023
2 parents 1c28711 + fa67214 commit 4740e41
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 86 deletions.
187 changes: 112 additions & 75 deletions lib/player/states/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -317,38 +317,40 @@ class PlayerCubit extends Cubit<PlayerState> {
}
}

playNext() {
EasyThrottle.throttle(skipToVideoThrottleName, const Duration(seconds: 1), () async {
if (settings.state.playerRepeatMode == PlayerRepeat.repeatOne) {
seek(Duration.zero);
play();
} else if (state.videos.isNotEmpty || state.offlineVideos.isNotEmpty) {
//moving current video to played list
String? currentVideoId = state.currentlyPlaying?.videoId ?? state.offlineCurrentlyPlaying?.videoId;
if (currentVideoId != null) {
// state.playedVideos.remove(currentVideoId);
state.playedVideos.add(currentVideoId);
}
_playNextNow() async {
if (settings.state.playerRepeatMode == PlayerRepeat.repeatOne) {
seek(Duration.zero);
play();
} else if (state.videos.isNotEmpty || state.offlineVideos.isNotEmpty) {
//moving current video to played list
String? currentVideoId = state.currentlyPlaying?.videoId ?? state.offlineCurrentlyPlaying?.videoId;
if (currentVideoId != null) {
// state.playedVideos.remove(currentVideoId);
state.playedVideos.add(currentVideoId);
}

if (state.playQueue.isNotEmpty) {
String toPlay = state.playQueue.removeFirst();
if (state.videos.isNotEmpty) {
await switchToVideo(state.videos.firstWhere((element) => element.videoId == toPlay));
} else {
await switchToOfflineVideo(state.offlineVideos.firstWhere((element) => element.videoId == toPlay));
}
} else if (settings.state.playerRepeatMode == PlayerRepeat.repeatAll) {
state.playedVideos = [];
state.playQueue = ListQueue.from([]);
if (state.videos.isNotEmpty) {
await switchToVideo(state.videos[0]);
} else {
await switchToOfflineVideo(state.offlineVideos[0]);
}
generatePlayQueue();
if (state.playQueue.isNotEmpty) {
String toPlay = state.playQueue.removeFirst();
if (state.videos.isNotEmpty) {
await switchToVideo(state.videos.firstWhere((element) => element.videoId == toPlay));
} else {
await switchToOfflineVideo(state.offlineVideos.firstWhere((element) => element.videoId == toPlay));
}
} else if (settings.state.playerRepeatMode == PlayerRepeat.repeatAll) {
state.playedVideos = [];
state.playQueue = ListQueue.from([]);
if (state.videos.isNotEmpty) {
await switchToVideo(state.videos[0]);
} else {
await switchToOfflineVideo(state.offlineVideos[0]);
}
generatePlayQueue();
}
});
}
}

playNext() {
EasyThrottle.throttle(skipToVideoThrottleName, const Duration(seconds: 1), _playNextNow);
}

playPrevious() {
Expand Down Expand Up @@ -415,56 +417,96 @@ class PlayerCubit extends Cubit<PlayerState> {
}
}

_switchToVideo(IdedVideo video, {Duration? startAt}) async {
// we move the existing video to the stack of played video
/// skip to queue video of index
/// if we're not shuffling, we also rebuild the playnext and played previously queue
skipToVideo(int index) {
if (index < 0 || index >= state.videos.length) {
return;
}

bool isOffline = video is DownloadedVideo;
// we want to switch to audio mode as soon as we can to prevent problems when switching from audio to video or the other way
if (isOffline) {
setAudio(video.audioOnly);
if (settings.state.playerShuffleMode) {
} else {
List<String> played = [];
List<String> playNext = [];
for (int i = 0; i < state.videos.length; i++) {
var v = state.videos[i];
if (i < index) {
played.add(v.videoId);
} else if (i > index) {
playNext.add(v.videoId);
}
}
emit(state.copyWith(playedVideos: played, playQueue: ListQueue.from(playNext)));
}
switchToVideo(state.videos[index]);
}

/// Switches to a video without changing the queue
_switchToVideo(IdedVideo video, {Duration? startAt}) async {
var state = this.state.copyWith();
try {
// we move the existing video to the stack of played video

state.mediaEvent = MediaEvent(state: MediaState.loading);
bool isOffline = video is DownloadedVideo;
// we want to switch to audio mode as soon as we can to prevent problems when switching from audio to video or the other way
if (isOffline) {
setAudio(video.audioOnly);
}

if (isOffline) {
state.videos = [];
state.currentlyPlaying = null;
} else {
state.offlineVideos = [];
state.offlineCurrentlyPlaying = null;
}

List<IdedVideo> toCheck = isOffline ? state.offlineVideos : state.videos;
state.mediaEvent = MediaEvent(state: MediaState.loading);

emit(state);
state = this.state.copyWith();
if (isOffline) {
state.videos = [];
state.currentlyPlaying = null;
} else {
state.offlineVideos = [];
state.offlineCurrentlyPlaying = null;
}

List<IdedVideo> toCheck = isOffline ? state.offlineVideos : state.videos;

emit(state);
state = this.state.copyWith();

if (!isOffline) {
late Video v;
if (video is Video) {
v = video;
if (!isOffline) {
late Video v;
if (video is Video) {
v = video;
} else {
v = await service.getVideo(video.videoId);
}
state.currentlyPlaying = v;
state.mediaCommand = MediaCommand(MediaCommandType.switchVideo, value: SwitchVideoValue(video: v, startAt: startAt));
} else {
v = await service.getVideo(video.videoId);
state.offlineCurrentlyPlaying = video;
state.mediaCommand = MediaCommand(MediaCommandType.switchToOfflineVideo, value: video);
}
state.currentlyPlaying = v;
state.mediaCommand = MediaCommand(MediaCommandType.switchVideo, value: SwitchVideoValue(video: v, startAt: startAt));
} else {
state.offlineCurrentlyPlaying = video;
state.mediaCommand = MediaCommand(MediaCommandType.switchToOfflineVideo, value: video);
}

state.position = Duration.zero;
state.forwardStep = defaultStep;
state.rewindStep = defaultStep;
state.position = Duration.zero;
state.forwardStep = defaultStep;
state.rewindStep = defaultStep;

emit(state);
emit(state);

setSponsorBlock();
await setSponsorBlock();

if (!isTv) {
mediaHandler.skipToQueueItem(currentIndex);
if (!isTv) {
mediaHandler.skipToQueueItem(currentIndex);
}
} catch (err) {
emit(state);
if(state.videos.length == 1) {
// if we can't get video details, we need to stop everything
log.severe("Couldn't play video '${video.videoId}', stopping player to avoid app crash");
hide();
}else{
// if we have more than 1 video
log.severe("Couldn't play video '${video.videoId}', removing it from the queue");

removeVideoFromQueue(video.videoId);
_playNextNow();
}
}
}

Expand Down Expand Up @@ -560,7 +602,7 @@ class PlayerCubit extends Cubit<PlayerState> {
var movedItem = listToUpdate.removeAt(oldItemIndex);
listToUpdate.insert(newItemIndex, movedItem);
log.fine('Reordered list: $oldItemIndex new index: ${listToUpdate.indexOf(movedItem)}');
if(newItemIndex <= currentIndex){
if (newItemIndex <= currentIndex) {
state.playedVideos.add(listToUpdate[newItemIndex].videoId);
}
/*
Expand Down Expand Up @@ -589,7 +631,7 @@ class PlayerCubit extends Cubit<PlayerState> {
}

setSponsorBlock() async {
var state = this.state.copyWith();
List<Pair<int>> newSegments = [];
if (state.currentlyPlaying != null) {
List<SponsorSegmentType> types = SponsorSegmentType.values.where((e) => db.getSettings(e.settingsName())?.value == 'true').toList();

Expand All @@ -602,15 +644,11 @@ class PlayerCubit extends Cubit<PlayerState> {
return segment;
}));

state.sponsorSegments = segments;
newSegments = segments;
log.fine('we found ${segments.length} segments to skip');
} else {
state.sponsorSegments = [];
}
} else {
state.sponsorSegments = [];
}
emit(state);
emit(state.copyWith(sponsorSegments: newSegments));
}

seek(Duration duration) {
Expand Down Expand Up @@ -749,9 +787,8 @@ class PlayerCubit extends Cubit<PlayerState> {
videos.shuffle();
}

state.playQueue = ListQueue.from(videos);

// set up the queue
ListQueue<String> playQueue = ListQueue.from(videos);
emit(state.copyWith(playQueue: playQueue));
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/player/views/components/video_queue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class VideoQueue extends StatelessWidget {
))
],
child: CompactVideo(
onTap: isPlaying ? () {} : () => controller.switchToVideo(video),
onTap: isPlaying ? () {} : () => controller.skipToVideo(index),
video: video,
highlighted: isPlaying,
),
Expand Down Expand Up @@ -65,7 +65,7 @@ class VideoQueue extends StatelessWidget {
))
],
child: CompactVideo(
onTap: isPlaying ? () {} : () => controller.switchToOfflineVideo(v),
onTap: isPlaying ? () {} : () => controller.skipToVideo(index),
offlineVideo: v,
highlighted: isPlaying,
trailing: [
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/video_post_processing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import 'package:logging/logging.dart';
import '../videos/models/base_video.dart';

var log = Logger('Video post process');
const privateVideoString = "[Private video]";

Future<List<BaseVideo>> postProcessVideos(List<BaseVideo> toProcess) async {
try {
int start = DateTime.now().millisecondsSinceEpoch;
List<BaseVideo> videos = toProcess ?? [];
List<BaseVideo> videos = toProcess.where((element) => element.title != privateVideoString).toList() ?? [];

videos = await VideoFilter.filterVideos(videos);
videos = await DeArrow.processVideos(videos);

Expand Down
13 changes: 6 additions & 7 deletions lib/videos/models/dearrow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ class DeArrow {
if (!doThumbnails) return;

if (cache.url != null) {

bool isThumbnailAvailable = await service.testDeArrowThumbnail(cache.url);
if (isThumbnailAvailable) {
video.deArrowThumbnailUrl = cache.url;
// if we've set both things from cache, we stop otherwise we go through normal process
return;
}
// bool isThumbnailAvailable = await service.testDeArrowThumbnail(cache.url);
// if (isThumbnailAvailable) {
video.deArrowThumbnailUrl = cache.url;
// if we've set both things from cache, we stop otherwise we go through normal process
return;
// }
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.17.0+4034
version: 1.17.1+4035

environment:
sdk: '>=3.0.0 <4.0.0'
Expand Down

0 comments on commit 4740e41

Please sign in to comment.