iOS中 MPMoviePlayer 实现视频音频播放

iOS播放视频文件一般使用 MPMoviePlayerViewController 和 MPMoviePlayerController。前者是一个view,后者是个Controller。区别就是 MPMoviePlayerViewController里面包含了一个MPMoviePlayerController

 

 注意:MPMoviePlayerViewController 必须   presentMoviePlayerViewControllerAnimated方式添加,否则Done按钮是不会响应通知MPMoviePlayerPlaybackDidFinishNotification事件的;

 

 首先要包含  #import  <MediaPlayer/MediaPlayer.h>头文件和MediaPlayer.framework。

 

 

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

 MPMovieControlModeVolumeOnly          只显示音量控制

 MPMovieControlModeHidden              没有控制器

 

 

 

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

 MPMovieScallingModeNone             不做任何缩放

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

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

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

 

 

 //通知

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

 

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

 

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

===============================================================================

需要引进的框架:MediaPlayer.framework

第一步:引进框架设置属性

[objc] view plain copy
  1. #import "RootViewController.h"  
  2. #import <MediaPlayer/MediaPlayer.h>  
  3.   
  4.   
  5. @interface RootViewController ()  
  6.   
  7. @property (nonatomicstrongMPMoviePlayerController *moviePlayer;  
  8.   
  9. @end  
  10.   
  11. @implementation RootViewController  

调用:

[objc] view plain copy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.       
  4.       
  5.     // 5.调用播放器  
  6.       
  7.     //播放网络视频  
  8.     NSString *urlString = @"http://video.szzhangchu.com/qiaokeliruanxinbudingA.mp4";  
  9.     //播放本地视图,找到文件的路径  
  10.       
  11. //    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"优酷网-唐豆豆微信摇一摇惨被骗.mp4" ofType:nil];  
  12.       
  13.     [self createMPPlayerController:urlString];  
  14.       
  15. }  

第二步:创建播放器

[objc] view plain copy
  1. - (void)createMPPlayerController:(NSString *)string  
  2. {  
  3.    // 1.初始化播放器  
  4.       
  5.     //准备网址  
  6. //     NSURL *urlString = [NSURL fileURLWithPath:fileNamePath];  
  7.      NSURL *urlString = [NSURL URLWithString:string];  
  8.       
  9.     //初始化播放器  
  10.    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:urlString];  
  11.      
  12.     //准备播放  
  13. //    [_moviePlayer prepareToPlay];  
  14.     //设置moviePlayer的frame  
  15.     _moviePlayer.view.frame = self.view.frame;  
  16.     //添加到父视图  
  17.     [self.view addSubview:_moviePlayer.view];  
  18.       
  19.       
  20.     // 2.配置属性  
  21.   
  22.     //是否自动播放,默认是NO  
  23.     _moviePlayer.shouldAutoplay = YES;  
  24.     //设置播放器的样式  
  25.     [_moviePlayer setControlStyle:(MPMovieControlStyleFullscreen)];  
  26.     //开始播放  
  27.     [_moviePlayer play];  
  28.       
  29.       
  30.     // 3.注册通知  
  31.     //注册通知  
  32.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinshed:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];  
  33.       
  34.       
  35. }  

第三步:实现通知方法

[objc] view plain copy
  1. // 4.实现通知中的方法  
  2. - (void)movieFinshed:(NSNotification *)sender  
  3. {  
  4.     //取出通知中心的moviePlayer  
  5.     MPMoviePlayerController *movie = [sender object];  
  6.       
  7.     //移除观察者  
  8.     [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];  
  9.     //将movie移出父视图  
  10.     [movie.view removeFromSuperview];  
  11.       
  12. }  

最终效果:

iOS中 MPMoviePlayer 实现视频音频播放


关注博主微博每日更新技术:http://weibo.com/hanjunqiang

原文地址:http://blog.csdn.net/qq_31810357/article/details/49922749