loadTracks method
Load tracks by identifier
identifier can be:
- YouTube URL: https://www.youtube.com/watch?v=...
- Search query: ytsearch:imagine dragons
- Spotify URL: https://open.spotify.com/track/...
- SoundCloud URL: https://soundcloud.com/...
Implementation
Future<LoadResult> loadTracks(String identifier) async {
LavalinkLogger.debug('Loading tracks: $identifier', tag: 'REST');
try {
final uri = Uri.parse('$baseUrl/v4/loadtracks')
.replace(queryParameters: {'identifier': identifier});
final request = await _httpClient.getUrl(uri);
_addHeaders(request);
final response = await request.close();
final responseBody = await _readResponse(response);
if (response.statusCode != 200) {
throw LavalinkException(
'Failed to load tracks: ${response.statusCode}',
statusCode: response.statusCode,
);
}
final json = jsonDecode(responseBody) as Map<String, dynamic>;
final result = LoadResult.fromJson(json);
LavalinkLogger.debug(
'Loaded ${result.tracks?.length ?? 0} tracks',
tag: 'REST',
);
return result;
} on TimeoutException {
throw LavalinkTimeoutException('Request timed out');
} on SocketException catch (e) {
throw LavalinkConnectionException(
'Failed to connect to Lavalink server',
cause: e,
);
} catch (e) {
if (e is LavalinkException) rethrow;
throw LavalinkException('Unexpected error: $e', cause: e);
}
}