Skip to content

Commit

Permalink
feat: simplify audio URL fetching and caching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gokadzev committed Feb 15, 2025
1 parent 8c37514 commit 1076b7f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
9 changes: 2 additions & 7 deletions lib/API/musify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -664,10 +664,6 @@ Future<String> getSong(String songId, bool isLive) async {

unawaited(updateRecentlyPlayed(songId));

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

if (cachedUrl != null) {
return cachedUrl;
}
Expand All @@ -676,7 +672,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 +686,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
4 changes: 4 additions & 0 deletions lib/services/audio_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ class MusifyAudioHandler extends BaseAudioHandler {

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

final cacheKey = 'song_${song['ytid']}_${audioQualitySetting.value}_url';
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 1076b7f

Please sign in to comment.