AVMetricMediaResourceRequestEvent returns error but no URLSession metrics for failed HLS playlist/segment requests

Hello,

I am using AVMetrics to monitor HLS playback requests from AVPlayer, specifically AVMetricHLSPlaylistRequestEvent and AVMetricHLSMediaSegmentRequestEvent.

These events provide an AVMetricMediaResourceRequestEvent. For successful requests, I can read URLSession metrics. However, when a request fails, the event contains an error but no URLSession metrics.

I reproduced this by intercepting HLS playlist and segment requests with Charles Proxy and forcing failures on both the simulator and a physical device.

Is this expected behavior? If so, is there any supported way to get timing details for failed HLS requests?

I am using code like this:

for try await event in playerItem.metrics(forType: AVMetricHLSPlaylistRequestEvent.self) {
    // ...
}

for try await event in playerItem.metrics(forType: AVMetricHLSMediaSegmentRequestEvent.self) {
    // ...
}

Also, the example shown in the WWDC session does not compile for me (XCode 26.2). I get the following error:

Pack expansion requires that '' and 'AVMetricEvent' have the same shape

let playerItem: AVPlayerItem = ...

let ltkuMetrics = item.metrics(forType: AVMetricPlayerItemLikelyToKeepUpEvent.self)
let summaryMetrics = item.metrics(forType: AVMetricPlayerItemPlaybackSummaryEvent.self)

for await (metricEvent, publisher) in ltkuMetrics.chronologicalMerge(with: summaryMetrics) {
    // send metricEvent to server
}

Thanks for the post. I have to say I wish you could provide a focused sample project instead so developers can could download and go over that.

So to go over a few things in the documentation that has a relation to your implementation, I think, The metrics property on AVMetricMediaResourceRequestEvent is directly backed by URLSessionTaskMetrics.

https://developer.apple.com/documentation/avfoundation/avmetricmediaresourcerequestevent

By design, URLSession only generates complete task metrics when an HTTP transaction completes. https://developer.apple.com/documentation/foundation/urlsessiontaskmetrics

When you use A Proxy to force a failure the request fails at the transport layer before a full HTTP transaction can finish. Because the transaction never completes, URLSession does not generate the metrics object, and AVFoundation has nothing to attach to the event.

Or at least this is how I understand the server returns a clean HTTP 404 or 500, the URLSessionTaskMetrics will usually be present because the network transaction completed. If the request times out or the connection is dropped, it will be nil in this case? Is that what you are seeing?

Just trying to clarify so developers here can help you.

Thanks

Albert
  Worldwide Developer Relations.

AVMetricMediaResourceRequestEvent returns error but no URLSession metrics for failed HLS playlist/segment requests
 
 
Q