Add Custom Header Field in Request of Avplayer

Add custom header field in request of AVPlayer

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]];
//...

Is it possible to configure custom headers for AVPlayerItem URLRequest?

when you are creating AVURLAsset you can add an option AVURLAssetHTTPHeaderFieldsKey and provide your HTTP headers. The code should look similar to:

let asset = AVURLAsset(url: url, options:["AVURLAssetHTTPHeaderFieldsKey": yourHttpHeaders])
let item = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: item)


Related Topics



Leave a reply



Submit