ios - 为 AVURLAssets 设置 HTTP header 字段

标签 ios objective-c http-headers avplayer avasset

我正在尝试加载一个需要 header 中的 token 的视频文件。我在 Stackverflow 上了解到,这可能是通过查看以下 question 来实现的。问题是我不相信 AVURLAssetHTTPHeaderFieldsKey 是公开的,或者我错误地编写了我的 Assets 变量。我想要做的是将 token 添加到标题中,以便 AVPlayer 加载视频文件。到目前为止,这是我的代码,它只显示播放器,但没有加载我猜的视频,因为我的标题设置不正确:

NSURL *videoURL = [NSURL URLWithString:cell.media[@"redirectionUrl"]];

        NSMutableDictionary * headers = [NSMutableDictionary dictionary];
        [headers setObject:[CMUser currentUser].token forKey:@"Authorization"];

        AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:@{@"AVURLAssetHTTPHeaderFieldsKey" : headers}];


        AVPlayerItem * item = [AVPlayerItem playerItemWithAsset:asset];

        AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:item];

        AVPlayerViewController *playerViewController = [AVPlayerViewController new];

        playerViewController.player = player;

        playerViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;

        [self presentViewController:playerViewController animated:YES completion:nil];  

最佳答案

在您的代码中一切正常。这可能会在授权 token 中发出。

Please check following code it's completely same as yours. *In Swift 4.0


    //MARK:- setting player
    fileprivate func setPlayRemoteUrl() {

        if playUrl == nil || playUrl == "" {
            return
        }
        removeAllObserver()
        resettingObject()

        let headerFields: [String:String] = ["User-Agent":"6y2zxABAb8oqeNec"]
        let asset: AVURLAsset = AVURLAsset.init(url: URL(string: self.playUrl)!, options: ["AVURLAssetHTTPHeaderFieldsKey": headerFields])
        self.playerItem = AVPlayerItem(asset: asset)
        self.player = AVPlayer.init(playerItem: playerItem)

        playerLayer = AVPlayerLayer(player: player)
        playerLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
        playerLayer?.contentsScale = UIScreen.main.scale
        layer.insertSublayer(playerLayer!, at: 0)
        setAllObserver()
    }

关于ios - 为 AVURLAssets 设置 HTTP header 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51797343/

相关文章:

objective-c - 删除Core Data对象时出错: NSManagedObjectContext cannot delete objects in other contexts

ios - 调整 super View 大小时自动调整 subview 大小

objective-c - 将图像文件路径保存到 Core Data

ruby-on-rails - 在 HTTparty 中传递 header 和查询参数

ios - 有效地发送数据而不会出现频带饱和 (iOS)

iphone - 如何在 iOS 5 中移除 UITabbaritem 的渐变/投影

http-headers - 如何为不同的浏览器设置正确的 Content-Type header ?

Golang 错误的http请求头

ios - 我正在尝试查找文件大小。但是我的长度等于零

ios - NSAttributedString 背景色和圆角