|
| 1 | +// |
| 2 | +// DWPlayVideoVC.m |
| 3 | +// RCTAuroraIMUI |
| 4 | +// |
| 5 | +// Created by Dowin on 2018/5/23. |
| 6 | +// Copyright © 2018年 HXHG. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "DWPlayVideoVC.h" |
| 10 | +#import <MediaPlayer/MediaPlayer.h> |
| 11 | + |
| 12 | +#define screenW [UIScreen mainScreen].bounds.size.width |
| 13 | +#define screenH [UIScreen mainScreen].bounds.size.height |
| 14 | + |
| 15 | +@interface DWPlayVideoVC () |
| 16 | +@property (nonatomic, strong) MPMoviePlayerController *moviePlayer; |
| 17 | +@property (strong, nonatomic) UIButton *playBtn; |
| 18 | +@property (strong, nonatomic) UIButton *backBtn; |
| 19 | +@end |
| 20 | + |
| 21 | +@implementation DWPlayVideoVC |
| 22 | + |
| 23 | +- (void)dealloc{ |
| 24 | +// NSLog(@"dealloc---------------"); |
| 25 | + [_moviePlayer stop]; |
| 26 | + [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 27 | +} |
| 28 | + |
| 29 | +- (void)viewWillAppear:(BOOL)animated{ |
| 30 | + [super viewWillAppear:animated]; |
| 31 | + [self topStatusUIHidden:YES]; |
| 32 | +} |
| 33 | + |
| 34 | +- (void)viewWillDisappear:(BOOL)animated{ |
| 35 | + [super viewWillDisappear:animated]; |
| 36 | + [self topStatusUIHidden:NO]; |
| 37 | +} |
| 38 | + |
| 39 | +- (void)viewDidAppear:(BOOL)animated{ |
| 40 | + [super viewDidAppear:animated]; |
| 41 | + [self setupBtn]; |
| 42 | +} |
| 43 | + |
| 44 | +- (void)viewDidLoad { |
| 45 | + [super viewDidLoad]; |
| 46 | + self.view.backgroundColor = [UIColor blackColor]; |
| 47 | + |
| 48 | + self.playBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 80)]; |
| 49 | + self.playBtn.center = CGPointMake(screenW *0.5, screenH * 0.5); |
| 50 | + |
| 51 | + NSString *strImgPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"IMUIAssets.bundle/image/video_play_btn" ofType:@"png"]; |
| 52 | + [self.playBtn setImage:[UIImage imageWithContentsOfFile:strImgPath] forState:UIControlStateNormal]; ; |
| 53 | + [self.playBtn addTarget:self action:@selector(clickPlayBtn) forControlEvents:UIControlEventTouchUpInside]; |
| 54 | + self.playBtn.hidden = YES; |
| 55 | + [self.view addSubview:self.playBtn]; |
| 56 | + |
| 57 | + self.backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)]; |
| 58 | + [self.backBtn setTitle:@"退出" forState:UIControlStateNormal]; |
| 59 | + self.backBtn.titleLabel.font = [UIFont systemFontOfSize:20]; |
| 60 | + [self.backBtn addTarget:self action:@selector(clickTapBackBtn) forControlEvents:UIControlEventTouchUpInside]; |
| 61 | + |
| 62 | + if (self.strPath) { |
| 63 | + [self startPlay]; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +- (void)clickPlayBtn{ |
| 68 | + self.playBtn.hidden = YES; |
| 69 | + [self.moviePlayer play]; |
| 70 | +} |
| 71 | + |
| 72 | +- (void)clickTapBackBtn{ |
| 73 | + [_moviePlayer stop]; |
| 74 | + [self dismissViewControllerAnimated:YES completion:nil]; |
| 75 | +} |
| 76 | + |
| 77 | +- (MPMoviePlayerController*)moviePlayer{ |
| 78 | + if (!_moviePlayer){ |
| 79 | + _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:self.strPath]]; |
| 80 | + _moviePlayer.controlStyle = MPMovieControlStyleEmbedded; |
| 81 | + _moviePlayer.scalingMode = MPMovieScalingModeAspectFill; |
| 82 | + _moviePlayer.fullscreen = YES; |
| 83 | + } |
| 84 | + return _moviePlayer; |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +- (void)startPlay{ |
| 89 | + self.moviePlayer.view.frame = self.view.bounds; |
| 90 | + self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
| 91 | + [self.moviePlayer play]; |
| 92 | + [self.view addSubview:self.moviePlayer.view]; |
| 93 | + |
| 94 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 95 | + selector:@selector(moviePlaybackComplete:) |
| 96 | + name:MPMoviePlayerPlaybackDidFinishNotification |
| 97 | + object:self.moviePlayer]; |
| 98 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 99 | + selector:@selector(moviePlayerLoadStateDidChange:) |
| 100 | + name:MPMoviePlayerLoadStateDidChangeNotification |
| 101 | + object:self.moviePlayer]; |
| 102 | + |
| 103 | + |
| 104 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 105 | + selector:@selector(moviePlayStateChanged:) |
| 106 | + name:MPMoviePlayerPlaybackStateDidChangeNotification |
| 107 | + object:self.moviePlayer]; |
| 108 | + |
| 109 | + [self.view bringSubviewToFront:self.playBtn]; |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +- (void)setupBtn{ |
| 114 | + for (UIView *tmpView in [[self.moviePlayer.view.subviews firstObject].subviews firstObject].subviews) { |
| 115 | + if ([tmpView isKindOfClass:NSClassFromString(@"MPVideoPlaybackOverlayView")]) { |
| 116 | + tmpView.hidden = NO; |
| 117 | + tmpView.alpha = 1.0; |
| 118 | + for (UIView *tt in [tmpView.subviews lastObject].subviews) { |
| 119 | + for (UIButton *tmpBtn in tt.subviews) { |
| 120 | + tmpBtn.hidden = NO; |
| 121 | + tmpBtn.alpha = 1.0; |
| 122 | + if ([tmpBtn isKindOfClass:NSClassFromString(@"MPKnockoutButton")] && (tmpBtn.frame.origin.x > screenW*0.5)) { |
| 123 | + self.backBtn.center = tmpBtn.center; |
| 124 | + tmpBtn.hidden = YES; |
| 125 | + [tmpBtn.superview addSubview:self.backBtn]; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +- (void)moviePlayerLoadStateDidChange:(NSNotification *)aNotification{ |
| 134 | + if (self.moviePlayer == aNotification.object) |
| 135 | + { |
| 136 | + switch (self.moviePlayer.loadState) { |
| 137 | + case MPMovieLoadStateUnknown: |
| 138 | + break; |
| 139 | + case MPMovieLoadStatePlayable: |
| 140 | + break; |
| 141 | + case MPMovieLoadStatePlaythroughOK: |
| 142 | + self.playBtn.hidden = YES; |
| 143 | +// NSLog(@"-------MPMovieLoadStatePlaythroughOK"); |
| 144 | + [self setupBtn]; |
| 145 | + break; |
| 146 | + case MPMovieLoadStateStalled: |
| 147 | + break; |
| 148 | + default: |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +- (void)moviePlaybackComplete: (NSNotification *)aNotification |
| 155 | +{ |
| 156 | + if (self.moviePlayer == aNotification.object) |
| 157 | + { |
| 158 | + self.playBtn.hidden = NO; |
| 159 | + [self setupBtn]; |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +- (void)moviePlayStateChanged: (NSNotification *)aNotification |
| 164 | +{ |
| 165 | + if (self.moviePlayer == aNotification.object) |
| 166 | + { |
| 167 | + switch (self.moviePlayer.playbackState) |
| 168 | + { |
| 169 | + case MPMoviePlaybackStatePlaying: |
| 170 | + [self topStatusUIHidden:YES]; |
| 171 | + break; |
| 172 | + case MPMoviePlaybackStatePaused: |
| 173 | + [self.playBtn setHidden:NO]; |
| 174 | + break; |
| 175 | + case MPMoviePlaybackStateStopped: |
| 176 | + break; |
| 177 | + case MPMoviePlaybackStateInterrupted: |
| 178 | + break; |
| 179 | + case MPMoviePlaybackStateSeekingBackward: |
| 180 | + break; |
| 181 | + case MPMoviePlaybackStateSeekingForward: |
| 182 | + break; |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | + |
| 188 | +- (void)topStatusUIHidden:(BOOL)isHidden{ |
| 189 | + [[UIApplication sharedApplication] setStatusBarHidden:isHidden]; |
| 190 | + self.playBtn.hidden = isHidden; |
| 191 | + |
| 192 | +} |
| 193 | + |
| 194 | +@end |
0 commit comments