美文网首页
AVPlayer播放视频的示例:m3u8及普通视频

AVPlayer播放视频的示例:m3u8及普通视频

作者: biyu6 | 来源:发表于2019-01-18 10:26 被阅读0次

今天开始来捋一捋音视频播放
一、配置:
1.导入:CoreMedia.framework、MediaPlayer.framework、AVFoundation.framework
2.引入:#import <AVFoundation/AVFoundation.h>、#import<MediaPlayer/MediaPlayer.h>、#import<CoreMedia/CoreMedia.h>
3.在info.plist中添加支持Http的请求:App Transport Security Settings 中 Allow Arbitrary Loads 为YES
二、代码:

- (void)viewDidLoad {
  [super viewDidLoad];
  self.view.backgroundColor = [UIColor whiteColor];
  
  //阿里云上的点播视频
//    NSString *urlStr = @"http://aliyunvideo.wujike.com.cn/40c5ac89f0f249b78274bd18e0f83dd8/7263f41d841c4ad68d3ee11674c44f04-e0f19d9b16859ba19c9aed1218a73641-sd.mp4";
  //战旗上的直播视频:(获取方法:https://jingyan.baidu.com/article/ff411625ba601d12e5823753.html)
  NSString *urlStr = @"https://alhls-cdn.zhanqi.tv/zqlive/272471_0m4e4.m3u8";
  
  [self testM3u8VideoWithUrl:urlStr];
}
- (void)testM3u8VideoWithUrl:(NSString *)urlStr{
  //处理播放地址
  NSURL *liveURL = [NSURL URLWithString:urlStr];
  AVAsset *liveAsset = [AVURLAsset URLAssetWithURL:liveURL options:nil];
  //设置播放项
  AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:liveAsset];
  //将播放项导入播放器中
  AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
  //将播放器添加到显示视图中
  AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
  playerLayer.frame=self.view.layer.bounds;
  playerLayer.videoGravity=AVLayerVideoGravityResizeAspect;
  [self.view.layer addSublayer:playerLayer];
  //开始播放
  [player play];
}

示例.png

PS:
获取m3u8链接的方法:https://jingyan.baidu.com/article/ff411625ba601d12e5823753.html

播放器界面及m3u8后续操作,以后再慢慢搞

相关文章

网友评论

      本文标题:AVPlayer播放视频的示例:m3u8及普通视频

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