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]];
//...
You can use AVURLAssetHTTPHeaderFieldsKey
of AVURLAsset
's init option to modify request headers.
For example:
NSMutableDictionary * headers = [NSMutableDictionary dictionary];
[headers setObject:@"Your UA" forKey:@"User-Agent"];
AVURLAsset * asset = [AVURLAsset URLAssetWithURL:URL options:@{@"AVURLAssetHTTPHeaderFieldsKey" : headers}];
AVPlayerItem * item = [AVPlayerItem playerItemWithAsset:asset];
self.player = [[AVPlayer alloc] initWithPlayerItem:item];
Note: I found this key in sources of WebKit, but this is a Private option key, So your app may reject by AppStore if you use this.
Answer in Swift, AVURLAssetHTTPHeaderFieldsKey
option will work like a charm.
let headers: [String: String] = [
"custome_header": "custome value"
]
let asset = AVURLAsset(url: URL, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
let playerItem = AVPlayerItem(asset: asset)
player = AVPlayer(playerItem: item)