request method

  1. @override
Future<StreamAudioResponse> request([
  1. int? start,
  2. int? end
])
override

Used by the player to request a byte range of encoded audio data in small chunks, from byte position start inclusive (or from the beginning of the audio data if not specified) to end exclusive (or the end of the audio data if not specified). If the returned future completes with an error, a 500 response will be sent back to the player.

Implementation

@override
Future<StreamAudioResponse> request([int? start, int? end]) async {
  final cacheFile = await this.cacheFile;
  if (cacheFile.existsSync()) {
    final sourceLength = cacheFile.lengthSync();
    return StreamAudioResponse(
      rangeRequestsSupported: true,
      sourceLength: start != null ? sourceLength : null,
      contentLength: (end ?? sourceLength) - (start ?? 0),
      offset: start,
      contentType: await _readCachedMimeType(),
      stream: cacheFile.openRead(start, end).asBroadcastStream(),
    );
  }
  final byteRangeRequest = _StreamingByteRangeRequest(start, end);
  _requests.add(byteRangeRequest);
  _response ??=
      _fetch().catchError((dynamic error, StackTrace? stackTrace) async {
    // So that we can restart later
    _response = null;
    // Cancel any pending request
    for (final req in _requests) {
      req.fail(error, stackTrace);
    }
    return Future<HttpClientResponse>.error(error as Object, stackTrace);
  });
  return byteRangeRequest.future.then((response) {
    response.stream.listen((event) {}, onError: (Object e, StackTrace st) {
      // So that we can restart later
      _response = null;
      // Cancel any pending request
      for (final req in _requests) {
        req.fail(e, st);
      }
    });
    return response;
  });
}