|
//
// TPMoviePlayerView.m
// ThePaperBase
//
// Created by zhousan on 16/1/6.
// Copyright © 2016年 scar1900. All rights reserved.
//
#import "TPMoviePlayerView.h"
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>
#import "TPLightView.h"
#import <MediaPlayer/MediaPlayer.h>
#import "PlayerView.h"
@interface TPMoviePlayerView () {
CGPoint beginPoint;
CGPoint movePoint;
CGPoint tempPoint;
UISlider* volumeViewSlider;
CGFloat sysV;
CGFloat value;
NSString *directionString;
CTCallCenter *callCenter;
}
@property (nonatomic, strong) UIView *playBackView;
@property (nonatomic, strong) UILabel *totalLabel;
@property (nonatomic, strong) UILabel *currLabel;
@property (nonatomic, strong) UIImageView *playImage;
@property (nonatomic, strong) UIProgressView *timeSlider;
@property (nonatomic, strong) TPLightView *lightView;
@property (nonatomic, strong) PlayerView *playerView;
@property (nonatomic, strong) NSURL *contentURL;
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerItem *playerItem;
@property (nonatomic, assign) CGRect vedioFrame;
@end
@implementation TPMoviePlayerView
- (id)initWithFrame:(CGRect)frame contentUrl:(NSURL *)contentUrl andFullScreen:(BOOL)fullScreen andSuperView:(UIView *)superView{
self = [super initWithFrame:frame];
if (self) {
self.playerItem = [AVPlayerItem playerItemWithURL:contentUrl];
[self.playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];// 监听status属性
[self.playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];// 监听loadedTimeRanges属性
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
self.playerView = [[PlayerView alloc] initWithFrame:CGRectZero];
self.playerView.player = _player;
[self.player play];
[self addSubview:self.playerView];
[self.playerView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
[superView addSubview:self];
[self relayoutSubViewForScreen:frame];
// 添加视频播放结束通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:_playerItem];
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
volumeView.showsVolumeSlider = YES;
for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)view;
break;
}
}
sysV = volumeViewSlider.value;
[UIScreen mainScreen].wantsSoftwareDimming = YES;
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
self.backgroundColor = [UIColor blackColor];
// [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[MobClick event:@"4"];
[self addSubview:self.playBackView];
[self addSubview:self.lightView];
[self.lightView makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.centerY.equalTo(self);
make.size.mas_equalTo(CGSizeMake(155, 155));
}];
[self.playBackView makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.centerY.equalTo(self);
make.size.mas_equalTo(CGSizeMake(140, 60));
}];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationWillChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
callCenter = [[CTCallCenter alloc] init];
__block typeof(self) weakSelf = self;
callCenter.callEventHandler = ^(CTCall* call) {
if ([call.callState isEqualToString:CTCallStateDisconnected])
{
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.player play];
});
NSLog(@"Call has been disconnected");
}
else if ([call.callState isEqualToString:CTCallStateConnected])
{
NSLog(@"Call has just been connected");
}
else if([call.callState isEqualToString:CTCallStateIncoming])
{
NSLog(@"Call is incoming");
}
else if ([call.callState isEqualToString:CTCallStateDialing])
{
NSLog(@"call is dialing");
}
else
{
NSLog(@"Nothing is done");
}
};
}
return self;
}
- (void)relayoutSubViewForScreen:(CGRect)rect {
if (self) {
[self remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(rect.origin.x);
make.top.mas_equalTo(rect.origin.y);
make.size.mas_equalTo(rect.size);
}];
}
}
- (void)moviePlayDidEnd:(NSNotification *)notification {
if ([self.delegate respondsToSelector:@selector(playFinish)]) {
[self.delegate playFinish];
}
}
// KVO方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
// AVPlayerItem *playerItem = (AVPlayerItem *)object;
// if ([keyPath isEqualToString:@"status"]) {
// if ([playerItem status] == AVPlayerStatusReadyToPlay) {
// NSLog(@"AVPlayerStatusReadyToPlay");
// CMTime duration = self.playerItem.duration;// 获取视频总长度
// CGFloat totalSecond = playerItem.duration.value / playerItem.duration.timescale;// 转换成秒
// _totalTime = [self convertTime:totalSecond];// 转换成播放时间
// [self customVideoSlider:duration];// 自定义UISlider外观
// NSLog(@"movie total duration:%f",CMTimeGetSeconds(duration));
// [self monitoringPlayback:self.playerItem];// 监听播放状态
// } else if ([playerItem status] == AVPlayerStatusFailed) {
// NSLog(@"AVPlayerStatusFailed");
// }
// } else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
// NSTimeInterval timeInterval = [self availableDuration];// 计算缓冲进度
// NSLog(@"Time Interval:%f",timeInterval);
// CMTime duration = _playerItem.duration;
// CGFloat totalDuration = CMTimeGetSeconds(duration);
// [self.videoProgress setProgress:timeInterval / totalDuration animated:YES];
// }
}
- (NSTimeInterval)availableDuration {
NSArray *loadedTimeRanges = [[self.playerView.player currentItem] loadedTimeRanges];
CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];// 获取缓冲区域
float startSeconds = CMTimeGetSeconds(timeRange.start);
float durationSeconds = CMTimeGetSeconds(timeRange.duration);
NSTimeInterval result = startSeconds + durationSeconds;// 计算缓冲总进度
return result;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
|