簡(jiǎn)述
https://github.com/qigge/ZQPlayer
ZQPlayer 是一個(gè)基于AVPlayer封裝的視頻、音頻播放器,視頻播放效果圖如下

豎屏效果

橫屏效果
安裝
1.手動(dòng)添加:
1.將ZQPlayer文件夾添加到工程目錄中
2.導(dǎo)入ZQPlayerMaskView.h (視頻播放)
2.CocoaPods:
1.在Podfile中添加
pod 'ZQPlayer'2.執(zhí)行pod install或pod update
3.導(dǎo)入ZQPlayerMaskView.h (視頻播放)
ZQPlayer 使用
視頻播放使用
初始化
_playerMaskView = [[ZQPlayerMaskView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.width*0.56)];
_playerMaskView.delegate = self;
_playerMaskView.isWiFi = YES; // 是否允許自動(dòng)加載,
[self.view addSubview:_playerMaskView];
// 網(wǎng)絡(luò)視頻
NSString *videoUrl = @"http://183.60.197.29/17/q/t/v/w/qtvwspkejwgqjqeywjfowzdjcjvjzs/hc.yinyuetai.com/A0780162B98038FBED45554E85720E53.mp4?sc=e9bad1bb86f52b6f&br=781&vid=3192743&aid=38959&area=KR&vst=2&ptp=mv&rd=yinyuetai.com";
// 本地視頻
// NSString *videoUrl = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
[_playerMaskView playWithVideoUrl:videoUrl];
// 布局
[_playerMaskView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.top.equalTo(self.view);
make.height.equalTo(_playerMaskView.mas_width).multipliedBy(0.56);
}];
全屏操作涉及到了屏幕旋轉(zhuǎn),需要在當(dāng)前的ViewController中加入如下代碼,具體可以參考這篇文章iOS 獲取屏幕方向,和強(qiáng)制屏幕旋轉(zhuǎn)
#pragma mark - 屏幕旋轉(zhuǎn)
//是否自動(dòng)旋轉(zhuǎn),返回YES可以自動(dòng)旋轉(zhuǎn)
- (BOOL)shouldAutorotate {
return YES;
}
//返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
//這個(gè)是返回優(yōu)先方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
// 全屏需要重寫(xiě)方法
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (orientation == UIDeviceOrientationPortrait || orientation
== UIDeviceOrientationPortraitUpsideDown) {
// 隱藏導(dǎo)航欄
[self.navigationController setNavigationBarHidden:NO animated:YES];
[_playerMaskView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.top.equalTo(self.view).with.offset(100);;
make.height.equalTo(_playerMaskView.mas_width).multipliedBy(0.56);
}];
}else {
// 顯示導(dǎo)航欄
[self.navigationController setNavigationBarHidden:YES animated:YES];
[_playerMaskView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
}
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
音頻播放
// 音頻播放
NSString *mp3Url = @"http://m10.music.126.net/20180414124141/e3e56fbce547d0fabda73f65db249437/ymusic/1f36/af3d/60a8/f7ac35fcd56465570b2031b93edd2546.mp3";
_audioPlayer = [[ZQPlayer alloc] initWithUrl:mp3Url];
[_audioPlayer play];
ZQPlayer結(jié)構(gòu)

ZQPlayer結(jié)構(gòu)
ZQPlayerImage.bundle 為圖片資源,若是需要自定義控件圖標(biāo)課進(jìn)行相應(yīng)的替換;
ZQPlayer 為基于AVPlayer封裝的播放器,可以播放網(wǎng)絡(luò)和本地的視頻音頻;
ZQPlayerMaskView 為播放視圖UI,是在ZQPlayer上加了一層各種控件,如播放、當(dāng)前時(shí)間、進(jìn)度條、總時(shí)間、全屏等,相應(yīng)的滑動(dòng)快進(jìn)快退手勢(shì)等,還有加載動(dòng)畫(huà)等;
提示:ZQPlayerMaskView 依賴(lài)于Masonry進(jìn)行布局
ZQPlayer
ZQPlayer.h方法和屬性
@interface ZQPlayer : NSObject
/** 使用播放源進(jìn)行初始化 */
- (instancetype)initWithUrl:(NSString *)url;
/** 下一首 */
- (void)nextWithUrl:(NSString *)url;
/** 播放 */
- (void)play;
/** 暫停 */
- (void)pause;
// 是否正在播放
@property (nonatomic, assign, readonly) BOOL isPlaying;
/** 視頻音頻長(zhǎng)度 */
@property (nonatomic, assign) CGFloat timeInterval;
@property (nonatomic, weak) id<ZQPlayerDelegate> delegate;
// 播放器
@property (nonatomic ,strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerItem *playerItme;
@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@end
播放器的狀態(tài)
typedef NS_ENUM(NSUInteger, ZQPlayerState) {
ZQPlayerStateReadyToPlay, // 播放器準(zhǔn)備完畢
ZQPlayerStatePlaying, // 正在播放(用戶(hù)播放)
ZQPlayerStatePause, // 暫停 (用戶(hù)暫停)
ZQPlayerStateStop, // 播放完畢
ZQPlayerStateBufferEmpty, // 緩沖中(這個(gè)狀態(tài)會(huì)暫停視頻,進(jìn)行緩沖)
ZQPlayerStateKeepUp // 緩沖完成(這個(gè)狀態(tài)可以播放)
};
代理
@protocol ZQPlayerDelegate <NSObject>
@optional
/**
播放器狀態(tài)變化
@param player 播放器
@param state 狀態(tài)
*/
- (void)ZQPlayerStateChange:(ZQPlayer *)player state:(ZQPlayerState)state;
/**
視頻源開(kāi)始加載后調(diào)用 ,返回視頻的長(zhǎng)度
@param player 播放器
@param time 長(zhǎng)度(秒)
*/
- (void)ZQPlayerTotalTime:(ZQPlayer *)player totalTime:(CGFloat)time;
/**
視頻源加載時(shí)調(diào)用 ,返回視頻的緩沖長(zhǎng)度
@param player 播放器
@param time 長(zhǎng)度(秒)
*/
- (void)ZQPlayerLoadTime:(ZQPlayer *)player loadTime:(CGFloat)time;
/**
播放時(shí)調(diào)用,返回當(dāng)前時(shí)間
@param player 播放器
@param time 播放到當(dāng)前的時(shí)間(秒)
*/
- (void)ZQPlayerCurrentTime:(ZQPlayer *)player currentTime:(CGFloat)time;
@end
ZQPlayerMaskView
ZQPlayerMaskView.h
@interface ZQPlayerMaskView : UIView
@property (nonatomic, weak) id <ZQPlayerDelegate> delegate;
/** 播放器 */
@property (nonatomic, strong,readonly) ZQPlayer *player;
/** 背景圖片
使用 backgroundImage.image來(lái)進(jìn)行設(shè)置*/
@property (nonatomic, strong,readonly) UIImageView *backgroundImage;
/**
是否為Wi-Fi環(huán)境 (默認(rèn)為YES)
若為YES則會(huì)自動(dòng)播放視頻,如果NO,則會(huì)彈出提示框給用戶(hù)進(jìn)行選擇
建議獲取用戶(hù)網(wǎng)絡(luò)環(huán)境,若是移動(dòng)環(huán)境則設(shè)置為NO,其他設(shè)置為YES
*/
@property (nonatomic, assign) BOOL isWiFi;
/** 用播放源進(jìn)行播放 */
- (void)playWithVideoUrl:(NSString *)videoUrl;
@end
若需要自定義播放UI,可以參考ZQPlayerMaskView進(jìn)行自定義