123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- //
- // YOUPAIUCIMVideoVC.m
- // wolfman
- //
- // Created by 张灿 on 2017/6/5.
- // Copyright © 2017年 shareSmile. All rights reserved.
- //
- #import "YOUPAIUCIMVideoVC.h"
- @interface YOUPAIUCIMVideoVC ()
- @property (nonatomic,strong) NIMVideoObject *videoObject;
- @end
- @implementation YOUPAIUCIMVideoVC
- @synthesize moviePlayer = _moviePlayer;
- - (instancetype)initWithVideoObject:(NIMVideoObject *)videoObject{
- self = [super initWithNibName:nil bundle:nil];
- if (self) {
- _videoObject = videoObject;
- }
- return self;
- }
- - (void)dealloc{
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- [[NIMSDK sharedSDK].resourceManager cancelTask:_videoObject.path];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.edgesForExtendedLayout = UIRectEdgeAll;
- self.navigationItem.title = @"视频";
- if ([[NSFileManager defaultManager] fileExistsAtPath:self.videoObject.path]) {
- [self startPlay];
- }else{
- __weak typeof(self) wself = self;
- [self downLoadVideo:^(NSError *error) {
- if (!error) {
- [wself startPlay];
- }else{
- [wself.view makeToast:@"下载失败,请检查网络"
- duration:2
- position:CSToastPositionCenter];
- }
- }];
- }
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear: animated];
- [SVProgressHUD dismiss];
- if (![[self.navigationController viewControllers] containsObject: self])
- {
- [self topStatusUIHidden:NO];
- }
- }
- - (void)viewDidDisappear:(BOOL)animated{
- [super viewDidDisappear:animated];
- [self.moviePlayer stop];
- }
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear: animated];
- if (_moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {//不要调用.get方法,会过早的初始化播放器
- [self topStatusUIHidden:YES];
- }else{
- [self topStatusUIHidden:NO];
- }
- }
- - (UIStatusBarStyle)preferredStatusBarStyle{
- return UIStatusBarStyleLightContent;
- }
- - (void)downLoadVideo:(void(^)(NSError *error))handler{
- [SVProgressHUD show];
- __weak typeof(self) wself = self;
- [[NIMSDK sharedSDK].resourceManager download:self.videoObject.url filepath:self.videoObject.path progress:^(float progress) {
- [SVProgressHUD showProgress:progress];
- } completion:^(NSError *error) {
- if (wself) {
- [SVProgressHUD dismiss];
- if (handler) {
- handler(error);
- }
- }
- }];
- }
- - (void)startPlay{
- self.moviePlayer.view.frame = self.view.bounds;
- self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
-
- [self.moviePlayer play];
- [self.view addSubview:self.moviePlayer.view];
-
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(moviePlaybackComplete:)
- name:MPMoviePlayerPlaybackDidFinishNotification
- object:self.moviePlayer];
-
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(moviePlayStateChanged:)
- name:MPMoviePlayerPlaybackStateDidChangeNotification
- object:self.moviePlayer];
-
-
- CGRect bounds = self.moviePlayer.view.bounds;
- CGRect tapViewFrame = CGRectMake(0, 0, bounds.size.width, bounds.size.height);
- UIView *tapView = [[UIView alloc]initWithFrame:tapViewFrame];
- [tapView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
- tapView.backgroundColor = [UIColor clearColor];
- [self.moviePlayer.view addSubview:tapView];
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)];
- [tapView addGestureRecognizer:tap];
- }
- - (void)moviePlaybackComplete: (NSNotification *)aNotification
- {
- if (self.moviePlayer == aNotification.object)
- {
- [self topStatusUIHidden:NO];
- NSDictionary *notificationUserInfo = [aNotification userInfo];
- NSNumber *resultValue = [notificationUserInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
- MPMovieFinishReason reason = [resultValue intValue];
- if (reason == MPMovieFinishReasonPlaybackError)
- {
- NSError *mediaPlayerError = [notificationUserInfo objectForKey:@"error"];
- NSString *errorTip = [NSString stringWithFormat:@"播放失败: %@", [mediaPlayerError localizedDescription]];
- [self.view makeToast:errorTip
- duration:2
- position:CSToastPositionCenter];
- }
- }
-
- }
- - (void)moviePlayStateChanged: (NSNotification *)aNotification
- {
- if (self.moviePlayer == aNotification.object)
- {
- switch (self.moviePlayer.playbackState)
- {
- case MPMoviePlaybackStatePlaying:
- [self topStatusUIHidden:YES];
- break;
- case MPMoviePlaybackStatePaused:
- case MPMoviePlaybackStateStopped:
- case MPMoviePlaybackStateInterrupted:
- [self topStatusUIHidden:NO];
- case MPMoviePlaybackStateSeekingBackward:
- case MPMoviePlaybackStateSeekingForward:
- break;
- }
-
- }
- }
- - (void)topStatusUIHidden:(BOOL)isHidden
- {
- [[UIApplication sharedApplication] setStatusBarHidden:isHidden];
- self.navigationController.navigationBar.hidden = isHidden;
- // self.navigationController.interactivePopGestureRecognizer.enabled = isHidden;
- }
- - (void)onTap: (UIGestureRecognizer *)recognizer
- {
- switch (self.moviePlayer.playbackState)
- {
- case MPMoviePlaybackStatePlaying:
- [self.moviePlayer pause];
- break;
- case MPMoviePlaybackStatePaused:
- case MPMoviePlaybackStateStopped:
- case MPMoviePlaybackStateInterrupted:
- [self.moviePlayer play];
- break;
- default:
- break;
- }
- }
- - (MPMoviePlayerController*)moviePlayer{
- if (!_moviePlayer) {
- _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:self.videoObject.path]];
- _moviePlayer.controlStyle = MPMovieControlStyleNone;
- _moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
- _moviePlayer.fullscreen = YES;
- }
- return _moviePlayer;
- }
- @end
|