Frank
Frank

Reputation: 987

How to add custom HTTP header field to AVPlayer requests for HLS

We are using AVPlayer to play an HLS stream, but we have a new requirement that we need to supply a custom HTTP header field to identify the client from the backend team. Is it possible to add custom HTTP header field to AVPlayer requests without resorting to hacking HLS playlist file to use a custom protocol?

Upvotes: 6

Views: 4005

Answers (2)

Dvyz
Dvyz

Reputation: 489

I found this answer somewhere from the web. I am using it in my test app, but since this is an undocumented feature, there is a risk that apple may reject. But technically, this is an option.

NSMutableDictionary* headers = [NSMutableDictionary dictionary];
[headers setObject:@"SOF" forKey:@"X-REQ-HEADER-TEST"];

AVURLAsset* asset = [AVURLAsset URLAssetWithURL:myUrl options:@{@"AVURLAssetHTTPHeaderFieldsKey": headers}];
AVPlayerItem* item = [AVPlayerItem playerItemWithAsset:asset]; 
[avPlayerObj replaceCurrentItemWithPlayerItem:item];

Usage of AVURLAssetHTTPHeaderFieldsKey is the one in question.

Upvotes: 6

Kevin James Hunt
Kevin James Hunt

Reputation: 333

I spent weeks looking for a way to do this officially. For anyone else looking for an approach that would work for both requests and responses for the playlist and chunk requests, the only way I was able to find that worked was by passing the playback request through a reverse proxy on the device itself, which allows you to intercept the request, add headers, send it to the real server, and then extract the headers from the response before returning it to the AVPlayer.

I made a simple example project (with lots of comments and documentation) here: https://github.com/kevinjameshunt/AVPlayer-HTTP-Headers-Example

Upvotes: 1

Related Questions