Skip to content

Commit

Permalink
feat: refactor song fetching logic to simplify audio URL retrieval an…
Browse files Browse the repository at this point in the history
…d cache handling
  • Loading branch information
gokadzev committed Feb 15, 2025
1 parent 8c37514 commit 280f495
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
11 changes: 2 additions & 9 deletions lib/API/musify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,6 @@ Future<String> getSong(String songId, bool isLive) async {
cachingDuration: _cacheDuration,
);

unawaited(updateRecentlyPlayed(songId));

if (playNextSongAutomatically.value) {
getSimilarSong(songId);
}

if (cachedUrl != null) {
return cachedUrl;
}
Expand All @@ -676,7 +670,7 @@ Future<String> getSong(String songId, bool isLive) async {
return await getLiveStreamUrl(songId);
}

return await getAudioUrl(songId, cacheKey);
return await getAudioUrl(songId);
} catch (e, stackTrace) {
logger.log('Error while getting song streaming URL', e, stackTrace);
rethrow;
Expand All @@ -690,12 +684,11 @@ Future<String> getLiveStreamUrl(String songId) async {
return streamInfo;
}

Future<String> getAudioUrl(String songId, String cacheKey) async {
Future<String> getAudioUrl(String songId) async {
final manifest = await _yt.videos.streamsClient.getManifest(songId);
final audioQuality = selectAudioQuality(manifest.audioOnly.sortByBitrate());
final audioUrl = audioQuality.url.toString();

addOrUpdateData('cache', cacheKey, audioUrl);
return audioUrl;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/services/audio_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ class MusifyAudioHandler extends BaseAudioHandler {

await audioPlayer.setAudioSource(audioSource);
await audioPlayer.play();

final cacheKey = 'song_${song['ytid']}_${audioQualitySetting.value}_url';
unawaited(updateRecentlyPlayed(song['ytid']));
if (!isOffline) addOrUpdateData('cache', cacheKey, songUrl);
if (playNextSongAutomatically.value) getSimilarSong(song['ytid']);
} catch (e, stackTrace) {
logger.log('Error playing song', e, stackTrace);
}
Expand Down

0 comments on commit 280f495

Please sign in to comment.