| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- //
- // TCVideoPreview.m
- // TCLVBIMDemo
- //
- // Created by xiang zhang on 2017/4/18.
- // Copyright © 2017年 tencent. All rights reserved.
- //
- #import "TCVideoPreview.h"
- //#import "UIView+AdditionsX12.h"
- #import <AVFoundation/AVFoundation.h>
- #undef _MODULE_
- #define _MODULE_ "TXVideoPreview"
- #define playBtnWidth 42 //34
- #define playBtnHeight 42 //46
- #define pauseBtnWidth 42 //27
- #define pauseBtnHeight 42
- #undef _MODULE_
- #define _MODULE_ "TXVideoPreview"
- @interface TCVideoPreview()
- @end
- @implementation TCVideoPreview
- {
- UIButton *_playBtn;
- UIImageView *_coverView;
- CGFloat _currentTime;
- BOOL _videoIsPlay;
- BOOL _appInbackground;
- }
- - (instancetype)initWithFrame:(CGRect)frame coverImage:(UIImage *)image
- {
- self = [super initWithFrame:frame];
- if (self) {
- _renderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
- [self addSubview:_renderView];
-
- if (image != nil) {
- _coverView = [[UIImageView alloc] initWithFrame:_renderView.frame];
- _coverView.contentMode = UIViewContentModeScaleAspectFill;//UIViewContentModeScaleAspectFit;
- _coverView.image = image;
- _coverView.hidden = NO;
- [self addSubview:_coverView];
- }
-
- _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [self setPlayBtn:_videoIsPlay];
- [_playBtn addTarget:self action:@selector(playBtnClick) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:_playBtn];
-
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(applicationWillEnterForeground:)
- name:UIApplicationWillEnterForegroundNotification
- object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(applicationDidEnterBackground:)
- name:UIApplicationDidEnterBackgroundNotification
- object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(applicationDidBecomeActive:)
- name:UIApplicationDidBecomeActiveNotification
- object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(applicationWillResignActive:)
- name:UIApplicationWillResignActiveNotification
- object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(onAudioSessionEvent:)
- name:AVAudioSessionInterruptionNotification
- object:nil];
- }
- return self;
- }
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)layoutSubviews
- {
- [super layoutSubviews];
-
- _renderView.frame = CGRectMake(0, 0, self.width, self.height);
- _coverView.frame = _renderView.frame;
- if (_videoIsPlay) {
- _playBtn.frame = CGRectMake((self.frame.size.width - pauseBtnWidth)/2, (self.frame.size.height - pauseBtnHeight)/2.5 , pauseBtnWidth, pauseBtnHeight);
- } else {
- _playBtn.frame = CGRectMake((self.frame.size.width - playBtnWidth)/2, (self.frame.size.height - playBtnHeight)/2.5 , playBtnWidth, playBtnHeight);
- }
-
- }
- - (BOOL)isPlaying
- {
- return _videoIsPlay;
- }
- - (void)removeNotification
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)applicationWillEnterForeground:(NSNotification *)noti
- {
- if (_appInbackground){
- if (_delegate && [_delegate respondsToSelector:@selector(onVideoWillEnterForeground)]) {
- [_delegate onVideoWillEnterForeground];
- }
- _appInbackground = NO;
- }
- }
- - (void)applicationDidEnterBackground:(NSNotification *)noti
- {
- if (_videoIsPlay) {
- [self playBtnClick];
- }
- if (!_appInbackground) {
- if (_delegate && [_delegate respondsToSelector:@selector(onVideoEnterBackground)]) {
- [_delegate onVideoEnterBackground];
- }
- _appInbackground = YES;
- }
- }
- - (void)applicationDidBecomeActive:(NSNotification *)noti
- {
- if (_appInbackground){
- if (_delegate && [_delegate respondsToSelector:@selector(onVideoWillEnterForeground)]) {
- [_delegate onVideoWillEnterForeground];
- }
- _appInbackground = NO;
- }
- }
- - (void)applicationWillResignActive:(NSNotification *)noti
- {
- if (_videoIsPlay) {
- [self playBtnClick];
- }
- if (!_appInbackground) {
- if (_delegate && [_delegate respondsToSelector:@selector(onVideoEnterBackground)]) {
- [_delegate onVideoEnterBackground];
- }
- _appInbackground = YES;
- }
- }
- - (void) onAudioSessionEvent: (NSNotification *) notification
- {
- NSDictionary *info = notification.userInfo;
- AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
- if (type == AVAudioSessionInterruptionTypeBegan) {
- if (!_appInbackground) {
- if (_delegate && [_delegate respondsToSelector:@selector(onVideoEnterBackground)]) {
- [_delegate onVideoEnterBackground];
- }
- _appInbackground = YES;
- }
- _coverView.hidden = YES;
- if (_videoIsPlay) {
- _videoIsPlay = NO;
- [self setPlayBtn:_videoIsPlay];
- }
- }
- }
- - (void)setPlayBtnHidden:(BOOL)isHidden
- {
- _playBtn.hidden = isHidden;
- }
- - (void)playVideo
- {
- [self playBtnClick];
- }
- - (void)playBtnClick
- {
- _coverView.hidden = YES;
-
- if (_videoIsPlay) {
- _videoIsPlay = NO;
- [self setPlayBtn:_videoIsPlay];
-
- if (_delegate && [_delegate respondsToSelector:@selector(onVideoPause)]) {
- [_delegate onVideoPause];
- }
- }else{
- _videoIsPlay = YES;
- [self setPlayBtn:_videoIsPlay];
-
- if (_currentTime == 0) {
- if (_delegate && [_delegate respondsToSelector:@selector(onVideoPlay)]) {
- [_delegate onVideoPlay];
- }
- }else{
- if (_delegate && [_delegate respondsToSelector:@selector(onVideoResume)]) {
- [_delegate onVideoResume];
- }
- }
- }
- }
- -(void) setPlayBtn:(BOOL)videoIsPlay
- {
- if (videoIsPlay) {//pause_ugc_edit
- //暂停
- [_playBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
- _coverView.hidden = YES;
- }else{
- //play_ugc_edit
- //播放
- [_playBtn setImage:[UIImage imageNamed:@"ask_play"] forState:UIControlStateNormal];
- }
- _videoIsPlay = videoIsPlay;
- }
- -(void) onPreviewProgress:(CGFloat)time
- {
- _currentTime = time;
- if (_delegate && [_delegate respondsToSelector:@selector(onVideoPlayProgress:)]) {
- [_delegate onVideoPlayProgress:time];
- }
- }
- -(void) onPreviewFinished
- {
- if (_delegate && [_delegate respondsToSelector:@selector(onVideoPlayFinished)]) {
- [_delegate onVideoPlayFinished];
- }
- }
- @end
|