引入头文件
#import <AVKit/AVKit.h>
下面是不加请求头信息的播放防盗链视频代码,结果无法播放
// 防盗链视频
NSURL *videoURL = [NSURL URLWithString:@"https://test.mp4"];
// 播放视频
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc]init];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
playerViewController.view.frame = CGRectMake(100, 100, 200, 100);
[playerViewController.player play];
下面是加请求头信息的播放防盗链视频代码,结果可以播放
// 防盗链视频
NSURL *videoURL = [NSURL URLWithString:@"https://test.mp4"];
// 增加请求头信息
NSMutableDictionary * headers = [NSMutableDictionary dictionary];
// 防盗链域名白名单
[headers setObject:@"xxx.xxx.com"forKey:@"Referer"];
AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:videoURL options:@{@"AVURLAssetHTTPHeaderFieldsKey" :headers}];
// 初始化playerItem
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:urlAsset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
// 播放视频
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc]init];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
playerViewController.view.frame = CGRectMake(100, 100, 200, 100);
[playerViewController.player play];