1.头文件:
//// CustomMoviePlayerViewController.h// school//// Created by NW on 15/2/4.// Copyright (c) 2015年 Ternturing. All rights reserved.//#import#import //导入视频播放库@interface CustomMoviePlayerViewController : UIViewController@property (nonatomic,strong) MPMoviePlayerController *mp;@property (nonatomic,strong) NSURL *movieURL; //视频地址@property (nonatomic,strong) UIActivityIndicatorView *loadingAni; //加载动画@property (nonatomic,strong) UILabel *label;//加载提醒@property BOOL isHiddenStatusBar;//准备播放- (void)readyPlayer;@end
2,实现文件:
//// NSObject+CustomMoviePlayerViewController.m// school//// Created by NW on 15/2/4.// Copyright (c) 2015年 Ternturing. All rights reserved.//#import "CustomMoviePlayerViewController.h"#import "MKConstant.h"@implementation CustomMoviePlayerViewController@synthesize movieURL;- (void)viewDidLoad{ [super viewDidLoad]; self.loadingAni = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 37, 37)]; self.loadingAni.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; [self.view addSubview:self.loadingAni]; [self.loadingAni setTranslatesAutoresizingMaskIntoConstraints:NO]; NSLayoutConstraint *pConstraint = [NSLayoutConstraint constraintWithItem:self.loadingAni attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; [self.view addConstraint:pConstraint]; pConstraint = [NSLayoutConstraint constraintWithItem:self.loadingAni attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]; [self.view addConstraint:pConstraint]; pConstraint = [NSLayoutConstraint constraintWithItem:self.loadingAni attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:0.0 constant:37]; [self.view addConstraint:pConstraint]; pConstraint = [NSLayoutConstraint constraintWithItem:self.loadingAni attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.0 constant:37]; [self.view addConstraint:pConstraint]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(130, 190, 80, 40)]; self.label.text = @"加载中..."; self.label.textColor = [UIColor whiteColor]; self.label.backgroundColor = [UIColor clearColor]; [[self view] setBackgroundColor:[UIColor blackColor]]; [self.loadingAni startAnimating]; [self.view addSubview:self.label]; [self.label setTranslatesAutoresizingMaskIntoConstraints:NO]; pConstraint = [NSLayoutConstraint constraintWithItem:self.label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.loadingAni attribute:NSLayoutAttributeBottom multiplier:1.0 constant:30.0/3]; [self.view addConstraint:pConstraint]; pConstraint = [NSLayoutConstraint constraintWithItem:self.label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; [self.view addConstraint:pConstraint]; }- (BOOL)prefersStatusBarHidden{ // >=ios7 return self.isHiddenStatusBar;}- (void)showStatusBar{ // >=ios7 self.isHiddenStatusBar = NO; if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { [self prefersStatusBarHidden]; [self setNeedsStatusBarAppearanceUpdate]; }}- (void)hideStatusBar{ // >=ios7 self.isHiddenStatusBar = YES; if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { [self prefersStatusBarHidden]; [self setNeedsStatusBarAppearanceUpdate]; }}- (void) moviePlayerLoadStateChanged:(NSNotification*)notification{ [self.loadingAni stopAnimating]; [self.label removeFromSuperview]; // Unless state is unknown, start playback if ([self.mp loadState] != MPMovieLoadStateUnknown) { // Remove observer [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; // When tapping movie, status bar will appear, it shows up // in portrait mode by default. Set orientation to landscape //设置横屏 [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES]; // Rotate the view for landscape playback [[self view] setBounds:CGRectMake(0, 0, DESIGN_SCREEN_HEIGHT, DESIGN_SCREEN_WIDTH)]; [[self view] setCenter:CGPointMake(DESIGN_SCREEN_WIDTH/2, DESIGN_SCREEN_HEIGHT/2)]; //选中当前view [[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)]; // Set frame of movieplayer [[self.mp view] setFrame:CGRectMake(0, 0, DESIGN_SCREEN_HEIGHT, DESIGN_SCREEN_WIDTH)]; // self.mp..mov = MPMovieControlModeDefault; // Add movie player as subview [[self view] addSubview:[self.mp view]]; // Play the movie [self.mp play]; [[UIApplication sharedApplication] setStatusBarHidden:YES]; self.navigationController.navigationBar.hidden = YES; [self hideStatusBar]; }}- (void) moviePlayBackDidFinish:(NSNotification*)notification{ [self showStatusBar];// [[UIApplication sharedApplication] setStatusBarOrientation:<#(UIInterfaceOrientation)#>]; //还原状态栏为默认状态 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO]; // Remove observer [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; // [self dismissModalViewControllerAnimated:NO]; self.navigationController.navigationBar.hidden = NO; [self.navigationController popViewControllerAnimated:YES];}- (void) readyPlayer{ self.mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; if ([self.mp respondsToSelector:@selector(loadState)]) { // Set movie player layout [self.mp setControlStyle:MPMovieControlStyleFullscreen]; //MPMovieControlStyleFullscreen //MPMovieControlStyleEmbedded //满屏 [self.mp setFullscreen:YES]; // 有助于减少延迟 [self.mp prepareToPlay]; // Register that the load state changed (movie is ready) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; } else { // Play the movie For 3.1.x devices [self.mp play]; } // Register to receive a notification when the movie has finished playing. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];}@end
3.调用
- (IBAction)clickButtonPlay:(id)sender{ // MPMoviePlayerController *moviePlayer = [ [ MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:@"http://m.v.qq.com/live.html?stream_id=3956944389&lolboxAction=toExternalWebView"] ];// [moviePlayer play];// [self.navigationController pushViewController:moviePlayer animated:YES]; NSString* warefile = [self.dicData objectForKey:@"warefile"]; [MKServerHelper getWarefileWithFileName:warefile Completion:^(NSURL *url) { [self performSelectorOnMainThread:@selector(playMovieWithUrl:) withObject:url waitUntilDone:NO]; }];}- (void)playMovieWithUrl:(NSURL *)url//重定向返回的视频真实地址{ CustomMoviePlayerViewController *moviePlayer = [[CustomMoviePlayerViewController alloc] init]; //将视频地址传过去 moviePlayer.movieURL = url; //然后播放就OK了 [moviePlayer readyPlayer]; [self.navigationController pushViewController:moviePlayer animated:NO];}