Anbu Mani
Anbu Mani

Reputation: 81

How to play protected video content in iOS using AVPlayer

I have used the below implementation to play a protected video content from the media server, but it shows play icon with cross line.

After login app need to sync the cookies to the media assets to play the protected video after authentication.
By using AVURLAsset, we are streaming the protected video but its not working.

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
NSDictionary *opt = @{
                           AVURLAssetReferenceRestrictionsKey: @(AVAssetReferenceRestrictionForbidRemoteReferenceToLocal),
                           AVURLAssetHTTPCookiesKey: cookies
                      };
AVAsset *asset = [AVURLAsset URLAssetWithURL:linkUrl//Media protected URL(http://www.example.com/media/video/media_mp4)
                                         options:opt];

AVPlayerItem * item = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:item];
AVPlayerViewController *playerVC = [[AVPlayerViewController alloc]init];
playerVC.player = player;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self presentViewController:playerVC animated:NO completion:nil];        
});

Upvotes: 1

Views: 3307

Answers (1)

Anbu Mani
Anbu Mani

Reputation: 81

After searching a lot and finally it worked for me. I got the app cookies from NSHTTPCookieStorage class. Then using this I created a dictionary with key value pairs as below, @{AVURLAssetHTTPCookiesKey : cookies}

Then I set this dictionary to the options in the AVURLAsset URLAssetWithURL:linkUrl options:

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];

AVURLAsset * asset = [AVURLAsset URLAssetWithURL:linkUrl options:@{AVURLAssetHTTPCookiesKey : cookies}];

AVPlayerItem * item = [AVPlayerItem playerItemWithAsset:asset];

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

AVPlayerViewController *playerVC = [[AVPlayerViewController alloc]init];

playerVC.player = player;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    [self presentViewController:playerVC animated:NO completion:nil];

});

Upvotes: 4

Related Questions