博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
播放网络视频或者本地视频
阅读量:5106 次
发布时间:2019-06-13

本文共 10286 字,大约阅读时间需要 34 分钟。

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];}

 

转载于:https://www.cnblogs.com/niwococo/p/4281281.html

你可能感兴趣的文章
python数据分析-05DataFrame深入
查看>>
【转】Java学习---Java核心数据结构(List,Map,Set)使用技巧与优化
查看>>
理解Java中的弱引用(Weak Reference)
查看>>
lamp
查看>>
linux 各级目录的作用
查看>>
2015年8月TIOBE编程语言排行榜
查看>>
打印九九乘法表,左下角、右上角、左上角、右下角,使用列表推导式
查看>>
文章学习记录
查看>>
Illegal access: this web application instance has been stopped already. Could not load com.taobao.g
查看>>
javascript中文版教程(转自 幸福清扬)
查看>>
HTML5 SVG
查看>>
eclipse for mac 快捷键
查看>>
【Demo 0075】获取系统进程列表
查看>>
嵌入式系统C编程之堆栈回溯(二)
查看>>
如何修改config?
查看>>
什么是CPU平均负载
查看>>
面试常问题
查看>>
解析LayoutSubviews
查看>>
Scriptable Render Pipeline
查看>>
Python之操作Excel
查看>>