1

I'm attempting to load a video file that requires a token in the header. I learned on Stackverflow that this may be possible from looking at the following question. The problem is that I don't believe AVURLAssetHTTPHeaderFieldsKey is public or I have written my asset variable incorrectly. What I'm trying to do is add the token to the headers in order for the AVPlayer to load the video file. Here is my code so far which only shows the player but doesn't load the video I'm guessing because my headers aren't set correctly:

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];  
Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61
  • 1
    nothing appears to be wrong with your code, try getting the video from the postman first. If you get it from postman then your code may have some issues, also after presenting the controller in the completion block trying playing by calling "playerViewController.player.play()" – Shahzaib Qureshi Aug 11 '18 at 07:18

1 Answers1

5

Everything is fine in your code. That may issue in Authorization 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()
    }
Ayaz Rafai
  • 279
  • 2
  • 10
  • "AVURLAssetHTTPHeaderFieldsKey" is private key, using this app will be rejected from App Store when trying to upload. – Mritunjay Feb 23 '19 at 12:23
  • 3
    It wouldn't. You can't use private APIs such as function calls, but it's ok to use keys. None of my apps was rejected for such reason. – kelin Mar 22 '19 at 05:04