视频播放器

先导入框架 MediaPlayer.framework



/*  moviePlayer.moviewControlMode = MPMovieControlModeDefault;

 

    MPMovieControlModeDefault            显示播放/暂停、音量和时间控制

 

    MPMovieControlModeVolumeOnly         只显示音量控制

 

    MPMovieControlModeHidden             没有控制器

    moviePlayer.scallingMode = MPMovieScallingModeAspectFit;

 

    你可以使用下列宽高比值:

 

    MPMovieScallingModeNone            不做任何缩放

 

    MPMovieScallingModeAspectFit       适应屏幕大小,保持宽高比

 

    MPMovieScallingModeAspectFill      适应屏幕大小,保持宽高比,可裁剪

 

    MPMovieScallingModeFill            充满屏幕,不保持宽高比

 

    你会观察到以下通知:

 

    MPMoviePlayerContentPreloadDidFinishNotification

 

    当电影播放器结束对内容的预加载后发出。因为内容可以在仅加载了一部分的情况下播放,所以这个通知可能在已经播放后才发出。

 

    MPMoviePlayerScallingModeDidChangedNotification

 

    当用户改变了电影的缩放模式后发出。用户可以点触缩放图标,在全屏播放和窗口播放之间切换。

 

    MPMoviePlayerPlaybackDidFinishNotification

 

    当电影播放完毕或者用户按下了Done按钮后发出。


 

 

 

 */


主要介绍   MPMoviePlayerViewController的使用  
福利  附赠  MPMoviePlayerController的使用  不过是写在备注里的哦


#import <UIKit/UIKit.h>


@interface MainViewController : UIViewController


@end




#import "MainViewController.h"

#import <MediaPlayer/MediaPlayer.h>


@interface MainViewController ()

@property(nonatomic,strong)MPMoviePlayerController * movieController;

@property(nonatomic,strong)MPMoviePlayerViewController * movieViewController;

@end


@implementation MainViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.navigationItem.title = @"MoviePlayer";

    

    // Do any additional setup after loading the view.

    // ios 播放视频一般使用MPMoviePlayerViewControllerMPMoiePlayerController。前者是一个view,后者是一个Controller 区别是MPMoviePlayerViewController里面包含一个MPMoviePlayerController

    

    // MPMoviePlayerViewController必须presentMoviePlayerViewControllerAnimated方式添加,否则Done按钮是不会影响通知MPMoviePlayerPlaybackDidFinishNotification事件的

    

   /* NSString * filePath = [[NSBundle mainBundle] pathForResource:@"程序上传" ofType:@"mp4"];

    self.movieController = [self createMoviePlayerControllerWithFilePath:filePath];

    */

    

    

    UIButton * playButton = [UIButton buttonWithType:(UIButtonTypeSystem)];

    [self.view addSubview:playButton];

    [playButton setTitle:@"模态Controller播放" forState:(UIControlStateNormal)];

    playButton.frame = CGRectMake(10, 330, 300, 35);

    playButton.layer.borderWidth = 1.0;

    playButton.layer.borderColor = [[UIColor blackColor] CGColor];

    playButton.layer.cornerRadius = 5.0;

    [playButton addTarget:self action:@selector(didClickButtonAction:) forControlEvents:(UIControlEventTouchDown)];

    

    

}


//  模态出播放器的时候打开

- (void)didClickButtonAction:(UIButton *)button{

    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"程序上传" ofType:@"mp4"];

    self.movieViewController = [self createMoviePlyerViewControllerWithFilePath:filePath];


    

}

- (MPMoviePlayerViewController * )createMoviePlyerViewControllerWithFilePath:(NSString *)filePath

{

    MPMoviePlayerViewController * movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:filePath]];

   // [self.view addSubview:movieViewController.view];

    [movieViewController.moviePlayer prepareToPlay];// MPMoviePalyerController不同

    [movieViewController.moviePlayer setScalingMode:MPMovieScalingModeAspectFit];

    [movieViewController.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];

    [movieViewController.view setBackgroundColor:[UIColor clearColor]];

    [movieViewController.view setFrame:self.view.frame];

    [self presentMoviePlayerViewControllerAnimated:movieViewController]; // Done 起作用的关键:presentMoviePlayerViewControllerAnimated

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(movieViewContrlFinishedCallBack:)

                                                 name:MPMoviePlayerPlaybackDidFinishNotification object:movieViewController.moviePlayer];

    return movieViewController;

    

}

- (void)movieStateChangeCallback:(NSNotification * )notify

{

    NSLog(@"%s",__FUNCTION__);

    // 点击播放器中的播放、暂停按钮响应事件

}

- (void)movieViewContrlFinishedCallBack:(NSNotification * )notify

{

    NSLog(@"%s",__FUNCTION__);

    

    // 视频播放完或者在presentMoviePlayerViewControllerAnimated下的Done按钮被点击的响应通知

    MPMoviePlayerViewController * theMovie = [notify object];

    [[NSNotificationCenter defaultCenter]removeObserver:self

                                                   name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];

    [self dismissMoviePlayerViewControllerAnimated];

    

}


/*

//  MPMoviePalyerController

- (MPMoviePlayerController *)createMoviePlayerControllerWithFilePath:(NSString *)filePath

{

    // 初始化

    NSURL * movieURL = [NSURL fileURLWithPath:filePath];

    MPMoviePlayerController * movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

    [movieController prepareToPlay];

    [self.view addSubview:movieController.view]; // 设置写在添加后面

    movieController.shouldAutoplay = NO; // 自动播放

    [movieController setControlStyle:MPMovieControlStyleDefault];

    [movieController setFullscreen:YES];

    // [movieController.view setFrame:self.view.bounds];

    [movieController.view setFrame:CGRectMake(10, 70, 300, 240)];

    movieController.view.backgroundColor = [UIColor yellowColor];

    // 注册相关操作的通知

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(movieContrlFinishedCallBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:movieController];// 播放完后的通知

    return movieController;

    

}

- (void)movieContrlFinishedCallBack:(NSNotification * )notify

{

    NSLog(@"%s",__FUNCTION__);


    MPMoviePlayerController * theMovie = [notify object];

    [[NSNotificationCenter defaultCenter] removeObserver:self

                                                    name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];

    // 卸载监听,保证程序不会崩溃

    [theMovie.view removeFromSuperview];

}

*/

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值