簡述
https://github.com/qigge/ZQPlayer
ZQPlayer 是一個基于AVPlayer封裝的視頻佑淀、音頻播放器,視頻播放效果圖如下
豎屏效果
橫屏效果
安裝
1.手動添加:
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; // 是否允許自動加載贸呢,
[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)
//是否自動旋轉(zhuǎn),返回YES可以自動旋轉(zhuǎn)
- (BOOL)shouldAutorotate {
return YES;
}
//返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
//這個是返回優(yōu)先方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
// 全屏需要重寫方法
- (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)前時間坚芜、進(jìn)度條览芳、總時間、全屏等鸿竖,相應(yīng)的滑動快進(jìn)快退手勢等沧竟,還有加載動畫等铸敏;
提示:ZQPlayerMaskView 依賴于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;
/** 視頻音頻長度 */
@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, // 正在播放(用戶播放)
ZQPlayerStatePause, // 暫停 (用戶暫停)
ZQPlayerStateStop, // 播放完畢
ZQPlayerStateBufferEmpty, // 緩沖中(這個狀態(tài)會暫停視頻,進(jìn)行緩沖)
ZQPlayerStateKeepUp // 緩沖完成(這個狀態(tài)可以播放)
};
代理
@protocol ZQPlayerDelegate <NSObject>
@optional
/**
播放器狀態(tài)變化
@param player 播放器
@param state 狀態(tài)
*/
- (void)ZQPlayerStateChange:(ZQPlayer *)player state:(ZQPlayerState)state;
/**
視頻源開始加載后調(diào)用 悟泵,返回視頻的長度
@param player 播放器
@param time 長度(秒)
*/
- (void)ZQPlayerTotalTime:(ZQPlayer *)player totalTime:(CGFloat)time;
/**
視頻源加載時調(diào)用 杈笔,返回視頻的緩沖長度
@param player 播放器
@param time 長度(秒)
*/
- (void)ZQPlayerLoadTime:(ZQPlayer *)player loadTime:(CGFloat)time;
/**
播放時調(diào)用,返回當(dāng)前時間
@param player 播放器
@param time 播放到當(dāng)前的時間(秒)
*/
- (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來進(jìn)行設(shè)置*/
@property (nonatomic, strong,readonly) UIImageView *backgroundImage;
/**
是否為Wi-Fi環(huán)境 (默認(rèn)為YES)
若為YES則會自動播放視頻糕非,如果NO蒙具,則會彈出提示框給用戶進(jìn)行選擇
建議獲取用戶網(wǎng)絡(luò)環(huán)境,若是移動環(huán)境則設(shè)置為NO朽肥,其他設(shè)置為YES
*/
@property (nonatomic, assign) BOOL isWiFi;
/** 用播放源進(jìn)行播放 */
- (void)playWithVideoUrl:(NSString *)videoUrl;
@end
若需要自定義播放UI禁筏,可以參考ZQPlayerMaskView進(jìn)行自定義