import "AssistiveTouch.h" 最主要我用的是XIB,需要自己定制自己定制
import "KGCAssisiveCell.h" 订制的Cell
import "KGCPlayOnlineM3u8.h" //由于我们的mp3链接地址是加密的和视频统一分装的
import "KGC_TimeManager.h" //定时器使用
import "KGCActionStatistic.h"//公司的统计
import "UIImage+GIF.h"// 加载的动画,不用考虑
@interface AssistiveTouch ()
@property (nonatomic, strong) NSDateFormatter * dateFormatter;
@property(nonatomic,strong) NSMutableArray * subtitlesarray;
@property(nonatomic,strong) NSMutableArray * begintimearray;
@property (weak, nonatomic) IBOutlet UIView * mViewBack;
@property (weak, nonatomic) IBOutlet UIView * mViewPlayer;
@property (weak, nonatomic) IBOutlet UIButton * upBtnclick;
@property (weak, nonatomic) IBOutlet UIButton * nextBtnclick;
@property (weak, nonatomic) IBOutlet UILabel * mTexthints;
@property(nonatomic,strong) UIView * mViewPlay;
@property (nonatomic) int currentLine;
@property (nonatomic) int PlayerStact;
@property (nonatomic) BOOL isShow;
@property(nonatomic)CGFloat mCurrentSecond;
@property (nonatomic,strong)UIImageView * imageVIewGif;
@property (nonatomic,strong)NSMutableArray * mArray;
@end
@implementation AssistiveTouch
-(id)initWithCoder:(NSCoder*)aDecoder
{
self= [superinitWithCoder:aDecoder];
if(self)
{
[self initFrameViewTop];
}
return self;
}
- (void)btnclickTop:(int)tag
{
pragma mark --- 记录学习进度的
[self RecordonUpdateProgress:YES];
[selfstop];
if(self.Delegate) {
[self.Delegate PlayTheNextSection:tag];
}
}
- (IBAction)rightBtn:(UIButton*)sender
{
NSArray *selSectionArr = [KGCLogicalprocessing sharprocessing].scoLists[[KGCLogicalprocessing sharprocessing].tmpIndexPath.section][@"lesson"];
if ([KGCLogicalprocessing sharprocessing].tmpIndexPath.row == [selSectionArr count] - 1) {
if(self.Delegate){
[self.Delegate showMessage:@"您好,当前播放为最后一课"];
}
}else{
[self RecordonUpdateProgress:NO];
[selfbtnclickTop:2];
}
}
- (IBAction)LeftBtn:(UIButton*)sender
{
if ([KGCLogicalprocessing sharprocessing].tmpIndexPath.row == 0) {
if(self.Delegate){
[self.Delegate showMessage:@"您好,当前播放为第一课"];
}
}else{
[self RecordonUpdateProgress:NO];
[selfbtnclickTop:1];
}
}
+(AssistiveTouch*)instanceTextView
{
NSArray* nibView = [[NSBundle mainBundle] loadNibNamed:@"AssistiveTouch" owner:nil options:nil];
return[nibViewobjectAtIndex:0];
}
- (void)subArrayAndbegin
{
if (_subtitlesarray || _begintimearray){
[_subtitlesarray removeAllObjects];
[_begintimearray removeAllObjects];
}
_subtitlesarray = [[NSMutableArray alloc] init];
_begintimearray = [[NSMutableArray alloc] init];
__weaktypeof(self) weakSelf =self;
[[KGCLogicalprocessing sharprocessing] string:0 strurl:@"" block:^(NSMutableArray *arrayOne,NSMutableArray *arrayTwo, BOOL is) {
weakSelf.begintimearray= arrayOne;
weakSelf.subtitlesarray= arrayTwo;
if(weakSelf.begintimearray.count==0) {
weakSelf.mTexthints.hidden=NO;
}else{
weakSelf.mTexthints.hidden=YES;
}
}];
}
-(void)awakeFromNib
{
[super awakeFromNib];
self.mArray = [NSMutableArray array];
for(inti =0; i<40; i++) {
[self.mArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"加载动画%d.png",i+1]]];}
[self PickerViewinit];
}
- (void)PickerViewinit
{
self.currentLine = 0;
self.mTableView.delegate = self;
self.mTableView.dataSource = self;
[self.mTableView setHeight:kScreenWidth*270/480-68-80];
self.mViewPlayer.frame = CGRectMake(0, self.mTableView.bottomY, self.width, 80);
[self.mSlider setHeight:2];
[self.mSlider setThumbImage:[UIImage imageNamed:@"btn-进度点标识.png"] forState:UIControlStateNormal];
[_mSlider addTarget:self action:@selector(progressSliderBeginVauleChange) forControlEvents:UIControlEventTouchDown];
[_mSlider addTarget:self action:@selector(progressSliderVauleEndChange) forControlEvents:UIControlEventTouchCancel | UIControlEventTouchUpInside];
UITapGestureRecognizer*tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(animateShow:)];
[self addGestureRecognizer:tap];
self.imageVIewGif = [[UIImageView alloc] initWithFrame:CGRectMake(self.width*0.35, 0, self.width*0.3, self.width/10)];
[self addSubview:self.imageVIewGif];
}
- (void)animateShow:(UITapGestureRecognizer*)tap
{
[UIView animateWithDuration:0.1 animations:^{
if(self.isShow==NO) {
[self setHeight:25 hidden:YES];
}else{
[self setHeight:80 hidden:NO];
}
}];
}
- (void)setHeight:(CGFloat)height hidden:(BOOL)hidden
{
self.isShow= hidden;
self.mViewBack.hidden= hidden;
[self.mTableViewsetHeight:kScreenWidth*270/480-68-height];
self.mViewPlayer.frame = CGRectMake(0, self.mTableView.bottomY, self.width, height);
}
- (NSDateFormatter*)dateFormatter
{
if (!_dateFormatter)
{
_dateFormatter = [[NSDateFormatter alloc] init];
}
return _dateFormatter;
}
- (NSString*)convertTime:(CGFloat)second
{
NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];
if(second/3600>=1)
{
[[selfdateFormatter]setDateFormat:@"HH:mm:ss"];
}else{
[[selfdateFormatter]setDateFormat:@"mm:ss"];
}
NSString*showtimeNew = [[selfdateFormatter]stringFromDate:d];
returnshowtimeNew;
}
- (void)initFrameViewTop
{
self.userInteractionEnabled = YES;
self.window.windowLevel = UIWindowLevelAlert + 1;
[self.window makeKeyAndVisible];
[self setWidth:kScreenWidth-20];
[self setHeight:kScreenWidth*270/480-68];
[self setCenterX:kScreenWidth/2];
[self setBottomY:kScreenWidth*270/480];
}
//播放当前mp3
-(void)initPlay:(NSString)strUrl strProgress:(NSString)progress
{
[self subArrayAndbegin];
if(KlogIn) {
[[KGC_TimeManager sharprocessing] pq_open];
[KGC_TimeManager sharprocessing].mStatetype = TimerManagerStateRun;
}
self.isPlayer=YES;
self.swithURL= strUrl;
[self initPLayAudio:self.swithURL];
[self ReachabilityStatus:progress];
}
pragma makr === 点击播放按钮
-
(IBAction)btnClickPlay:(UIButton*)sender {
self.btnPlayVideon.selected= !sender.selected;
[selfplayMp3];
}
- (void)playMp3
{
if (self.PlayerStact==AVPlayerStatusReadyToPlay)
{
if(self.isPlayer) {
[[KGC_TimeManager sharprocessing] TimerManagerState:TimerManagerStateSuspend];
self.isPlayer=NO;
[self.playerpause];
}else{
[[KGC_TimeManager sharprocessing] TimerManagerState:TimerManagerStateRun];
self.isPlayer=YES;
[self.playerplay];
}
}
}
pragma mark -- 初始化音频播放器
-(void)initPLayAudio:(NSString*)strUrl
{
AVAudioSession *session = [AVAudioSession sharedInstance];
[sessionsetActive:YESerror:nil];
[sessionsetCategory:AVAudioSessionCategoryPlayback error:nil];
self.mAVPlayerItem = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:strUrl]];
self.player = [[AVPlayer alloc]initWithPlayerItem:self.mAVPlayerItem];
// self.player.playImmediatelyAtRate = 0;
self.player.rate=1.0;
pragma mark -- 添加监听状态
[self.mAVPlayerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
pragma mark -- KVO监听音乐缓冲状态
[self.player.currentItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChangeListenerCallback:) name:AVAudioSessionRouteChangeNotification object:nil];
pragma mark -- 通过KVO添加播放结束事件监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.mAVPlayerItem];
[self.playerplay];
[self progressCurrent];
}
- (void)progressCurrent
{
__weak typeof(self) weakSelf = self;
[self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
floatcurrent =CMTimeGetSeconds(time);
floattotal =CMTimeGetSeconds(weakSelf.mAVPlayerItem.duration);
[weakSelf.imageVIewGifstopAnimating];
[weakSelfrollLycNumber:(int)current];
weakSelf.endTime.text= [weakSelfconvertTime:total];
weakSelf.playTime.text= [weakSelfconvertTime:current];
weakSelf.mCurrentSecond= current;
// weakSelf.mCurrenttotal = total;
}];
}
//发送学习进度
- (void)RecordonUpdateProgress:(BOOL)isSuccess
{
if(KlogIn) {
[KGCActionStatistic onUpdateProgress:[KGCLogicalprocessing sharprocessing].tmpIndexPath array:[KGCLogicalprocessing sharprocessing].scoLists last:self.mCurrentSecond is:isSuccess endTime:[NSString stringWithFormat:@"%d",[KGC_TimeManager sharprocessing].timeInterval]];
}
}
- (void)imagViewGiftoAnimation
{
if(![self.imageVIewGifisAnimating]) {
self.imageVIewGif.animationImages = self.mArray;
self.imageVIewGif.animationDuration = 1.0;
self.imageVIewGif.animationRepeatCount = 0;
[self.imageVIewGif startAnimating];
}
}
staticvoidextracted(AssistiveTouch*object) {
[objectimagViewGiftoAnimation];
}
- (void)observeValueForKeyPath:(NSString)keyPath ofObject:(id)object change:(NSDictionary)change context:(void*)context
{
[selfplayerStac:keyPath];
[selfkeyPath:keyPath];
extracted(self);
}
pragma mark -- 缓存进度条显示
- (void)keyPath:(NSString*)keyPath
{
if([keyPathisEqualToString:@"loadedTimeRanges"]) {
NSTimeIntervaltimeIntervale = [selfavailableDuration];
CMTimeduratione =self.mAVPlayerItem.duration;
CGFloattotalDuration =CMTimeGetSeconds(duratione);
NSString*str = [NSStringstringWithFormat:@"%f",timeIntervale];
NSString*strr = [NSStringstringWithFormat:@"%f",totalDuration];
if([strisEqualToString:@"nan"] || [strrisEqualToString:@"nan"]) {
return;
}
[self.mProgressViewsetProgress:timeIntervale/totalDurationanimated:YES];
if(CMTimeGetSeconds(duratione))
{
self.mSlider.maximumValue=CMTimeGetSeconds(duratione);
}else{
self.mSlider.maximumValue=1;
}
}
}
- (NSTimeInterval)availableDuration
{
NSArray *loadedTimeRanges = [[self.player currentItem] loadedTimeRanges];
CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];// 获取缓冲区域
floatstartSeconds =CMTimeGetSeconds(timeRange.start);
floatdurationSeconds =CMTimeGetSeconds(timeRange.duration);
NSTimeIntervalresult = startSeconds + durationSeconds;
returnresult;
}
pragma mark 监听耳机插拔事件
-
(void)audioRouteChangeListenerCallback:(NSNotification*)notification {
NSDictionary*interuptionDict = notification.userInfo;
NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
switch(routeChangeReason) {
// case AVAudioSessionRouteChangeReasonNewDeviceAvailable: {//耳机插入
// [UIView animateWithDuration:1 animations:^{
// }];
// }
// break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: {//耳机拔出
[UIView animateWithDuration:1 animations:^{
[self.playerpause];
self.isPlayer=NO;
self.btnPlayVideon.selected=NO;
}];
}
break;
// case AVAudioSessionRouteChangeReasonCategoryChange:
// break;
}
}
pragma mark -- 监听当时的状态是否可以进行播放
- (void)playerStac:(NSString*)keyPath
{
if([keyPathisEqualToString:@"status"]) {
switch(self.player.status) {
case AVPlayerStatusUnknown:
self.PlayerStact = AVPlayerStatusUnknown;
break;
case AVPlayerStatusReadyToPlay:
[self monitoringPlayback:self.mAVPlayerItem];
self.PlayerStact = AVPlayerStatusReadyToPlay;
self.btnPlayVideon.selected=YES;
self.isPlayer=YES;
break;
case AVPlayerStatusFailed:
self.PlayerStact=AVPlayerStatusFailed;
if(self.Delegate){
[self.DelegateshowMessage:@"请检查您的网络~"];
}
self.btnPlayVideon.selected=NO;
self.isPlayer=NO;
break;
default:
break;
}
}
}
pragma MARK --滑杆
- (void)monitoringPlayback:(AVPlayerItem*)playerItem
{
__weak typeof(self) weakSelf = self;
[self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:NULL usingBlock:^(CMTime time){
CGFloatcurrentSecond = playerItem.currentTime.value/playerItem.currentTime.timescale;
[weakSelf.mSlidersetValue:currentSecondanimated:YES];
}];
}
pragma mark -- 进来的时候进行进度更新
- (void)ReachabilityStatus:(NSString*)progress
{
[self.player seekToTime:CMTimeMake([progress intValue],1)];
}
pragma mark -- 用户拖动进度条,修改播放进度
- (void)playSliderValueChange:(UISlider*)sender
{
[self.player seekToTime:CMTimeMake(sender.value * CMTimeGetSeconds(self.player.currentItem.duration), 1)];
}
- (void)playbackFinished:(NSNotification*)notice
{
pragma mark -- 添加时间
[self RecordonUpdateProgress:YES];
[self btnclickTop:2];
}
pragma mark -- 不是彻底的关闭
- (void)stop
{
self.btnPlayVideon.selected = NO;
self.isPlayer=NO;
[[KGCPlayOnlineM3u8 KGCPlayOnlineM3u8] removerM3u8];
[[KGC_TimeManager sharprocessing] pq_close];
[self.playerpause];
}
pragma tableView--delegate
pragma tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return _subtitlesarray.count;
}
- (CGFloat)tableView:(UITableView)tableView heightForRowAtIndexPath:(NSIndexPath)indexPath
{
return 20;
}
-(UITableViewCell)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
staticNSString*KGCPostIdentifier =@"KGCAssisiveCell";
KGCAssisiveCell* cell = [tableViewdequeueReusableCellWithIdentifier:KGCPostIdentifier];
if(!cell){
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"KGCAssisiveCell" owner:nil options:nil];
cell = [nibslastObject];
}
if (_subtitlesarray.count > 0) {
if(indexPath.row!=0) {
cell.labelText.text= [_subtitlesarrayobjectAtIndex:indexPath.row];
cell.labelText.highlightedTextColor = [UIColor whiteColor];
}
}
returncell;
}
- (void)rollLycNumber:(int)number
{
if (_begintimearray.count != 0) {
for(inti =0; i<_begintimearray.count; i++) {
if(number == [[_begintimearrayobjectAtIndex:i]intValue]) {
self.currentLine++;
[self currentIndexTextIndexRow:self.currentLine];
}else{
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:self.currentLine-1 inSection:0];
KGCAssisiveCell*cell=[self.mTableViewcellForRowAtIndexPath:indexpath];
cell.labelText.font= [UIFontsystemFontOfSize:13];
}
}
}
}
- (void)currentIndexTextIndexRow:(int)row
{
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:row inSection:0];
KGCAssisiveCell*cell=[self.mTableViewcellForRowAtIndexPath:indexpath];
[self.mTableView selectRowAtIndexPath:indexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
cell.labelText.font = [UIFont systemFontOfSize:14];
}
pragma mark ---- 滑竿开始滑动
- (void)progressSliderBeginVauleChange
{
[self.playerpause];
}
pragma mark ----- 滑竿停止滑动
- (void)progressSliderVauleEndChange
{
if (self.PlayerStact == AVPlayerStatusReadyToPlay)// ljn-防止滑动崩溃
{
__weaktypeof(self) weakSelf =self;
CMTime changedTime = CMTimeMakeWithSeconds(self.mSlider.value, 1);
[self.playerseekToTime:changedTimecompletionHandler:^(BOOLfinished) {
[weakSelf.playerplay];
}];
}
}
- (void)removerNot
{
[self.mAVPlayerItem removeObserver:self forKeyPath:@"status"];
[self.mAVPlayerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.mAVPlayerItem];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}