使用系统框架
#import <AVFoundation/AVFoundation.h>
使用类
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@property (nonatomic, strong) AVPlayerItem * item;
@property (nonatomic, strong) AVURLAsset *asset;
创建一个视频播放器
1.自定义视频播放器继承自UIView
2.创建一个AVPlayerLayer对象playerLayer
#pragma mark --- 懒加载
- (AVPlayerLayer *)playerLayer{
if (_playerLayer == nil) {
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
_playerLayer.backgroundColor = [UIColor blueColor].CGColor;
}
return _playerLayer;
}
- (AVPlayer *)player{
if (_player == nil) {
_player = [[AVPlayer alloc] init];
}
return _player;
}
3.将playerLayer添加到自定义播放器的layer上
[self.layer addSublayer:self.playerLayer];
4.在自定义播放器上添加播放/暂停按钮、进度条、播放时间、全屏按钮等
5.实现播放视频、全屏显示等一系列功能。
#pragma mark --- 播放、暂停
- (void)playOrPause:(UIButton*)btn{
NSLog(@"播放....");
btn.selected = !btn.selected;
[self addTimeObsever];
if (@available(iOS 10.0, *)) {
if (self.player.timeControlStatus == AVPlayerTimeControlStatusPaused) {
[_playBtn setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
[self.player play];
} else if (self.player.timeControlStatus == AVPlayerTimeControlStatusPlaying) {
[_playBtn setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
[self.player pause];
}
} else {
// Fallback on earlier versions
if (btn.selected == YES) {
[_playBtn setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
[self.player play];
}else{
[_playBtn setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
[self.player pause];
}
}
}
- (void)playWithVideoURL:(NSURL *)videoURL{
AVURLAsset * asset = [AVURLAsset assetWithURL:videoURL];
AVPlayerItem * item = [AVPlayerItem playerItemWithAsset:asset];
[self.player replaceCurrentItemWithPlayerItem:item];
[item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:NULL];
_item = item;
_asset = asset;
}
#pragma mark --- 对播放器的状态进行监听
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
NSTimeInterval totalTime = CMTimeGetSeconds(_asset.duration);
if ([keyPath isEqualToString:@"status"]) { // 检测播放器状态
AVPlayerStatus status = [[change objectForKey:@"new"] intValue];
if (status == AVPlayerStatusFailed) {
NSLog(@"资源不存在...");
}else if (status == AVPlayerStatusUnknown){
NSLog(@"发生未知错误...");
}else if (status == AVPlayerStatusReadyToPlay){
NSLog(@"达到播放状态...");
_slider.maximumValue = totalTime;
_allTimeLbl.text = [self stringWithTime:totalTime];
}
}
}
#pragma mark --- 屏幕发生旋转调用的方法
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
[super traitCollectionDidChange:previousTraitCollection];
if (self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular) { // 转至竖屏
[self.videoView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.top.equalTo(self.view.mas_top).offset(100);
make.height.equalTo(@(200));
}];
} else if (self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) { // 转至横屏
[self.videoView mas_remakeConstraints:^(MASConstraintMaker *make) {
if (kSystemVersionFloat > 11.0) {
make.left.bottom.right.top.equalTo(self.view.mas_safeAreaLayoutGuide);
}else{
make.left.bottom.right.top.equalTo(self.view);
}
}];
}
}
注意
1.如果你在点击全屏按钮的时候并没有实现全屏,那可能是因为你设置的屏幕只能是竖屏
这种情况需要我们再点击全屏的时候让屏幕能够旋转
我们需要实现AppDelegate中的一个代理方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
// 设置是否支持横屏
if (self.isShouAutoRotate) {
NSLog(@"支持各个屏幕方向...");
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
我们可以去设置isShouAutoRotate这个属性,通过设置该属性控制屏幕是否能进行旋转。
详细看demohttps://gitee.com/liangsenliangsen/watch_videos.git
****本篇文章到这里就结束了,愿大家加班不多工资多,男同胞都有女朋友,女同胞都有男朋友。😊