在AVPlayer的请求中添加自定义标头字段 [英] Add custom header field in request of AVPlayer

查看:350
本文介绍了在AVPlayer的请求中添加自定义标头字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AVPlayer时,是否可以将带有http请求的标头发送到音频文件?我需要能够在服务器收到时检查标题的内容,以限制对所请求文件的访问。

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.

推荐答案

您需要通过通用HTTP连接机制自行请求数据,例如 NSURLConnection 。如果 NSHTTPURLResponse 的标题通过了您的测试,那么您应该将其保存到 NSCachesDirectory 并将URL传递给这个资源到 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]];
//...

这篇关于在AVPlayer的请求中添加自定义标头字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆