freezer/lib/api/audio_sources/url_audio_source.dart

39 lines
1.2 KiB
Dart

import 'package:freezer/api/deezer.dart';
import 'package:freezer/api/definitions.dart';
import 'package:just_audio/just_audio.dart';
import 'package:http/http.dart' as http;
class UrlAudioSource extends StreamAudioSource {
final Uri uri;
final StreamInfoCallback? onStreamObtained;
UrlAudioSource(this.uri, {this.onStreamObtained});
@override
Future<StreamAudioResponse> request([int? start, int? end]) async {
final req = http.Request('GET', uri)
..headers.addAll({
'User-Agent': DeezerAPI.userAgent,
'Accept-Language': '*',
'Accept': '*/*',
if (start != null || end != null)
"Range":
"bytes=${start == null ? '' : start.toString()}-${end == null ? '' : end.toString()}"
});
final res = await req.send();
onStreamObtained?.call(StreamQualityInfo(
format: Format.MP3,
source: Source.stream,
quality: null,
size: res.contentLength! + (start ?? 0)));
return StreamAudioResponse(
sourceLength: res.contentLength! + (start ?? 0),
contentLength: res.contentLength!,
offset: start,
stream: res.stream,
contentType: res.headers['content-type']!);
}
}