Initial commit

This commit is contained in:
ifish
2017-08-15 16:59:01 +08:00
commit bdc83e07ef
5766 changed files with 423223 additions and 0 deletions
@@ -0,0 +1,17 @@
//
// NTESVideoViewController.h
// NIM
//
// Created by chris on 15/4/12.
// Copyright (c) 2015年 Netease. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface NTESVideoViewController : UIViewController
- (instancetype)initWithVideoObject:(NIMVideoObject *)videoObject;
@property (nonatomic, readonly) MPMoviePlayerController *moviePlayer;
@end
@@ -0,0 +1,207 @@
//
// NTESVideoViewController.m
// NIM
//
// Created by chris on 15/4/12.
// Copyright (c) 2015年 Netease. All rights reserved.
//
#import "NTESVideoViewController.h"
#import "Toast+UIView.h"
#import "Reachability.h"
#import "UIAlertView+NTESBlock.h"
#import "SVProgressHUD.h"
#import "NTESNavigationHandler.h"
@interface NTESVideoViewController ()
@property (nonatomic,strong) NIMVideoObject *videoObject;
@end
@implementation NTESVideoViewController
@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:@"下载失败,请检查网络"
];
}
}];
}
}
- (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];
}
}
- (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
];
}
}
}
- (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;
NTESNavigationHandler *handler = (NTESNavigationHandler *)self.navigationController.delegate;
handler.recognizer.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
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7706" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="NTESVideoViewController">
<connections>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<point key="canvasLocation" x="196" y="254"/>
</view>
</objects>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
</document>