load method
Loads a Cast MediaInformation JSON object; URLs must reach the receiver.
Resolves only on a final MEDIA_STATUS update: intermediate LOADING acks are ignored and LOAD_FAILED or LOAD_CANCELLED reject the future.
Implementation
Future<void> load(
Map<String, dynamic> media, {
bool autoplay = true,
List<int>? activeTrackIds,
}) async {
final previous = _pendingLoad;
if (previous != null && !previous.isCompleted) {
throw StateError('A media load is already pending');
}
final id = ++_nextRequest;
final completer = Completer<Map<String, dynamic>>();
_pendingLoad = completer;
_pendingLoadRequestId = id;
_pendingLoadSessionId = null;
_loadPriorSessions = Set.of(_knownSessions);
_loadWatchTimer = Timer(_deadline, () {
_failLoad(TimeoutException('Cast load timed out', _deadline));
});
try {
_send(_media, {
'type': 'LOAD',
'requestId': id,
'media': media,
'autoplay': autoplay,
'activeTrackIds': ?activeTrackIds,
});
await completer.future;
} finally {
_loadWatchTimer?.cancel();
_loadWatchTimer = null;
if (identical(_pendingLoad, completer)) {
_pendingLoad = null;
_pendingLoadRequestId = null;
_pendingLoadSessionId = null;
}
}
}