1.使用类PlayView来实现,代码如下:
#import <UIKit/UIKit.h>
@interface PlayView : UIView
/**播放的URL*/
- (void)playWith:(NSURL *)url;
@end
#import "PlayView.h"
#import <AVFoundation/AVFoundation.h>
/**工具视图的高度*/
#define Tool_View_Height 40.0f
@interface PlayView ()
/**安放常用控件的view*/
@property (nonatomic, strong)UIView *toolView;
/**播放器*/
@property (nonatomic, strong)AVPlayer *player;
/**进度条*/
@property (nonatomic, strong)UISlider *progress;
/**播放暂停按钮*/
@property (nonatomic, strong)UIButton *playOrPauseBtn;
/**播放的状态*/
@property (nonatomic, assign)BOOL isPlay;
/**同步屏幕刷新计时器*/
@property (nonatomic, strong)CADisplayLink *link;
/**当前的播放状态*/
@property (nonatomic, assign)AVPlayerItemStatus status;
/**当前缓冲的时间*/
@property (nonatomic, assign)NSTimeInterval loadedTime;
/**菊花图*/
@property (nonatomic, strong)UIActivityIndicatorView *loadingView;
@end
@implementation PlayView
- (instancetype)initWithFrame:(CGRect)frame {
if ([super initWithFrame:frame]) {
self.backgroundColor = [UIColor blackColor];
[self addSubview:self.loadingView];
self.loadingView.hidden = YES;
}
return self;
}
- (void)dealloc {
//移除计时器
[self.link invalidate];
[self.player.currentItem removeObserver:self forKeyPath:@"status"];
[self.player.currentItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
}
#pragma mark - 设置播放网址
- (void)playWith:(NSURL *)url {
AVPlayerItem *item = [[AVPlayerItem alloc]initWi