美文网首页
iOS 播放防盗链视频

iOS 播放防盗链视频

作者: Shadowoff | 来源:发表于2021-07-09 15:33 被阅读0次

引入头文件

#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];




相关文章

网友评论

      本文标题:iOS 播放防盗链视频

      本文链接:https://www.haomeiwen.com/subject/zmufpltx.html