iOS AVPlayer封裝(視頻湖员、音頻播放器)

簡述

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)行自定義

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市衡招,隨后出現(xiàn)的幾起案子篱昔,更是在濱河造成了極大的恐慌,老刑警劉巖始腾,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件州刽,死亡現(xiàn)場離奇詭異,居然都是意外死亡窘茁,警方通過查閱死者的電腦和手機(jī)怀伦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來山林,“玉大人房待,你說我怎么就攤上這事⊥漳ǎ” “怎么了桑孩?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長框冀。 經(jīng)常有香客問我流椒,道長,這世上最難降的妖魔是什么明也? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任宣虾,我火速辦了婚禮,結(jié)果婚禮上温数,老公的妹妹穿的比我還像新娘绣硝。我一直安慰自己,他們只是感情好撑刺,可當(dāng)我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布鹉胖。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪甫菠。 梳的紋絲不亂的頭發(fā)上挠铲,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天,我揣著相機(jī)與錄音寂诱,去河邊找鬼拂苹。 笑死,一個胖子當(dāng)著我的面吹牛刹衫,可吹牛的內(nèi)容都是我干的醋寝。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼带迟,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了囱桨?” 一聲冷哼從身側(cè)響起仓犬,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎舍肠,沒想到半個月后搀继,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡翠语,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年叽躯,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肌括。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡点骑,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出谍夭,到底是詐尸還是另有隱情黑滴,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布紧索,位于F島的核電站袁辈,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏珠漂。R本人自食惡果不足惜晚缩,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望媳危。 院中可真熱鬧荞彼,春花似錦、人聲如沸济舆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至签夭,卻和暖如春齐邦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背第租。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工措拇, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人慎宾。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓丐吓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親趟据。 傳聞我的和親對象是個殘疾皇子券犁,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容