Add custom header field in request of AVPlayer

avfoundation, avplayer, ios

Solution

You'll need to request the data yourself via a generic HTTP connection mechanism such as `NSURLConnection`. If the `NSHTTPURLResponse`'s headers pass your test, then you should save it into the `NSCachesDirectory` and pass off the URL to this resource to the `AVPlayer` like so:

NSData *data = //your downloaded data.
NSString *filePath = //generate random path under NSCachesDirectory
[data writeToFile:filePath atomically:YES];

AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:filePath]];
//...

Problem

Is it possible to send headers with an http request to an audio file when using AVPlayer? I need to be able to inspect the content of the header when received by the server in order to restrict access to the file being requested.

Original source

Related problems