Skip to content

Commit

Permalink
fix(mkPlayer): remove method and wrong active index on modifying play…
Browse files Browse the repository at this point in the history
…list
  • Loading branch information
Kingkor Roy Tirtho committed May 15, 2023
1 parent eaf65b6 commit 3bafa7b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 109 deletions.
31 changes: 16 additions & 15 deletions lib/models/spotube_track.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ enum SpotubeTrackMatchAlgorithm {
authenticPopular,
}

typedef SkipSegment = ({int start, int end});

class SpotubeTrack extends Track {
final PipedStreamResponse ytTrack;
final String ytUri;
final List<Map<String, int>> skipSegments;
final List<SkipSegment> skipSegments;
final List<PipedSearchItemStream> siblings;

SpotubeTrack(
Expand Down Expand Up @@ -82,7 +84,7 @@ class SpotubeTrack extends Track {
}
}

static Future<List<Map<String, int>>> getSkipSegments(
static Future<List<SkipSegment>> getSkipSegments(
String id,
UserPreferences preferences,
) async {
Expand All @@ -107,23 +109,23 @@ class SpotubeTrack extends Track {
));

if (res.body == "Not Found") {
return List.castFrom<dynamic, Map<String, int>>([]);
return List.castFrom<dynamic, SkipSegment>([]);
}

final data = jsonDecode(res.body) as List;
final segments = data.map((obj) {
return Map.castFrom<String, dynamic, String, int>({
"start": obj["segment"].first.toInt(),
"end": obj["segment"].last.toInt(),
});
return (
start: obj["segment"].first.toInt(),
end: obj["segment"].last.toInt(),
);
}).toList();
getLogger(SpotubeTrack).v(
"[SponsorBlock] successfully fetched skip segments for $id",
);
return List.castFrom<dynamic, Map<String, int>>(segments);
return List.castFrom<dynamic, SkipSegment>(segments);
} catch (e, stack) {
Catcher.reportCheckedError(e, stack);
return List.castFrom<dynamic, Map<String, int>>([]);
return List.castFrom<dynamic, SkipSegment>([]);
}
}

Expand Down Expand Up @@ -155,7 +157,7 @@ class SpotubeTrack extends Track {
PipedStreamResponse ytVideo;
PipedAudioStream ytStream;
List<PipedSearchItemStream> siblings = [];
List<Map<String, int>> skipSegments = [];
List<SkipSegment> skipSegments = [];
if (cachedTrack != null) {
final responses = await Future.wait(
[
Expand All @@ -167,7 +169,7 @@ class SpotubeTrack extends Track {
],
);
ytVideo = responses.first as PipedStreamResponse;
skipSegments = responses.last as List<Map<String, int>>;
skipSegments = responses.last as List<SkipSegment>;
ytStream = getStreamInfo(ytVideo, preferences.audioQuality);
} else {
final videos = await PipedSpotube.client
Expand All @@ -187,7 +189,7 @@ class SpotubeTrack extends Track {
],
);
ytVideo = responses.first as PipedStreamResponse;
skipSegments = responses.last as List<Map<String, int>>;
skipSegments = responses.last as List<SkipSegment>;
ytStream = getStreamInfo(ytVideo, preferences.audioQuality);
}

Expand Down Expand Up @@ -239,7 +241,7 @@ class SpotubeTrack extends Track {
) async {
if (siblings.none((element) => element.id == video.id)) return null;

final [PipedStreamResponse ytVideo, List<Map<String, int>> skipSegments] =
final [PipedStreamResponse ytVideo, List<SkipSegment> skipSegments] =
await Future.wait<dynamic>(
[
PipedSpotube.client.streams(video.id),
Expand Down Expand Up @@ -329,8 +331,7 @@ class SpotubeTrack extends Track {
track: Track.fromJson(map),
ytTrack: PipedStreamResponse.fromJson(map["ytTrack"]),
ytUri: map["ytUri"],
skipSegments:
List.castFrom<dynamic, Map<String, int>>(map["skipSegments"]),
skipSegments: List.castFrom<dynamic, SkipSegment>(map["skipSegments"]),
siblings: List.castFrom<dynamic, Map<String, dynamic>>(map["siblings"])
.map((sibling) => PipedSearchItemStream.fromJson(sibling))
.toList(),
Expand Down
2 changes: 2 additions & 0 deletions lib/provider/proxy_playlist/next_fetcher_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ mixin NextFetcher on StateNotifier<ProxyPlaylist> {
return source.startsWith('https://youtube.com/unplayable.m4a?id=');
}

bool isPlayable(String source) => !isUnPlayable(source);

/// Returns [Track.id] from [isUnPlayable] source that is not playable
String getIdFromUnPlayable(String source) {
return source.replaceFirst('https://youtube.com/unplayable.m4a?id=', '');
Expand Down
30 changes: 13 additions & 17 deletions lib/provider/proxy_playlist/proxy_playlist_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>
audioPlayer.percentCompletedStream(99).listen((_) async {
final nextSource =
audioPlayer.sources.elementAtOrNull(audioPlayer.currentIndex + 1);
if (nextSource == null || !isUnPlayable(nextSource)) return;
if (nextSource == null || isPlayable(nextSource)) return;
await audioPlayer.pause();
});

Expand All @@ -118,20 +118,17 @@ class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>
if (activeTrack.skipSegments.isNotEmpty == true &&
preferences.skipSponsorSegments) {
for (final segment in activeTrack.skipSegments) {
if (pos.inSeconds < segment["start"]! ||
pos.inSeconds >= segment["end"]!) continue;
await audioPlayer.seek(Duration(seconds: segment["end"]!));
if (pos.inSeconds < segment.start || pos.inSeconds >= segment.end) {
continue;
}
await audioPlayer.seek(Duration(seconds: segment.end));
}
}
});
}();
}

Future<SpotubeTrack?> ensureNthSourcePlayable(
int n, {
bool softReplace = false,
bool exclusive = false,
}) async {
Future<SpotubeTrack?> ensureNthSourcePlayable(int n) async {
final sources = audioPlayer.sources;
if (n < 0 || n > sources.length - 1) return null;
final nthSource = sources.elementAtOrNull(n);
Expand All @@ -150,13 +147,10 @@ class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>

if (nthSource == nthFetchedTrack.ytUri) return null;

if (!softReplace) {
await audioPlayer.replaceSource(
nthSource,
nthFetchedTrack.ytUri,
exclusive: exclusive,
);
}
await audioPlayer.replaceSource(
nthSource,
nthFetchedTrack.ytUri,
);

return nthFetchedTrack;
}
Expand Down Expand Up @@ -214,7 +208,9 @@ class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>
await SpotubeTrack.fetchFromTrack(tracks[initialIndex], preferences);

state = state.copyWith(
tracks: mergeTracks([addableTrack], tracks), active: initialIndex);
tracks: mergeTracks([addableTrack], tracks),
active: initialIndex,
);

await audioPlayer.openPlaylist(
state.tracks.map(makeAppropriateSource).toList(),
Expand Down
40 changes: 15 additions & 25 deletions lib/services/audio_player/audio_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -458,35 +458,25 @@ class SpotubeAudioPlayer {
final oldSourceIndex = sources.indexOf(oldSource);
if (oldSourceIndex == -1) return;

// if (mkSupportedPlatform) {
// final sourcesCp = sources.toList();
// sourcesCp[oldSourceIndex] = newSource;

// await _mkPlayer!.open(
// mk.Playlist(
// sourcesCp.map(mk.Media.new).toList(),
// index: currentIndex,
// ),
// play: false,
// );
// if (exclusive) await jumpTo(oldSourceIndex);
// } else {
await addTrack(newSource);
await removeTrack(oldSourceIndex);
if (mkSupportedPlatform) {
_mkPlayer!.replace(oldSource, newSource);
} else {
await addTrack(newSource);
await removeTrack(oldSourceIndex);

int newSourceIndex = sources.indexOf(newSource);
while (newSourceIndex == -1) {
await Future.delayed(const Duration(milliseconds: 100));
newSourceIndex = sources.indexOf(newSource);
}
await moveTrack(newSourceIndex, oldSourceIndex);
newSourceIndex = sources.indexOf(newSource);
while (newSourceIndex != oldSourceIndex) {
await Future.delayed(const Duration(milliseconds: 100));
int newSourceIndex = sources.indexOf(newSource);
while (newSourceIndex == -1) {
await Future.delayed(const Duration(milliseconds: 100));
newSourceIndex = sources.indexOf(newSource);
}
await moveTrack(newSourceIndex, oldSourceIndex);
newSourceIndex = sources.indexOf(newSource);
while (newSourceIndex != oldSourceIndex) {
await Future.delayed(const Duration(milliseconds: 100));
await moveTrack(newSourceIndex, oldSourceIndex);
newSourceIndex = sources.indexOf(newSource);
}
}
// }
}

Future<void> clearPlaylist() async {
Expand Down
Loading

0 comments on commit 3bafa7b

Please sign in to comment.