今天开始来捋一捋音视频播放
一、配置:
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后续操作,以后再慢慢搞
网友评论