iOS中 MediaPlayer framework实现视频播放

简介:

  iOS开发中播放音乐可以使用MPMusicPlayerController类来实现,播放视频可以使用MPMoviePlayerController和MPMoviePlayerViewController类来实现,同时MPMediaPickerController 类可以用于从系统媒体库中选择媒体播放。这几个类都包含与MediaPlayer.framework框架中。

这里主要介绍MediaPlayer.framework


指定根视图:

[objc]  view plain  copy
  1. RootTableViewController *rootTVC = [[RootTableViewController alloc] initWithStyle:UITableViewStylePlain];  
  2. UINavigationController *rootNC = [[UINavigationController alloc] initWithRootViewController:rootTVC];  
  3. self.window.rootViewController = rootNC;  


RootTableViewController.m

设置相关属性:

[objc]  view plain  copy
  1. #import "RootTableViewController.h"  
  2. #import "TestCell.h"  
  3. #import "TestModel.h"  
  4. #import "UIImageView+WebCache.h"  
  5. #import <MediaPlayer/MediaPlayer.h>  
  6.   
  7. @interface RootTableViewController ()  
  8.   
  9. @property (nonatomicstrongMPMoviePlayerViewController *mpPVC;  
  10.   
  11. @property (nonatomicstrongNSMutableArray *dataSourceArray;  
  12.   
  13. @property (nonatomicstrongNSIndexPath *selectedIndexPath;  
  14.   
  15. @property (nonatomic, assign) CGRect selectedRect;  
  16.   
  17. @end  
  18.   
  19. @implementation RootTableViewController  

调用:

[objc]  view plain  copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     [self.tableView registerNib:[UINib nibWithNibName:@"TestCell" bundle:nil] forCellReuseIdentifier:@"testCell"];  
  6.     self.dataSourceArray = [NSMutableArray array];  
  7.       
  8.     [self loadDataAndShow];  
  9.   
  10. }  

加载网络数据:

[objc]  view plain  copy
  1. - (void)loadDataAndShow  
  2. {  
  3.     NSURL *url = [NSURL URLWithString:@"http://c.m.163.com/nc/video/list/V9LG4B3A0/y/1-20.html"];  
  4.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  5.     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  
  6.           
  7.         if (data != nil) {  
  8.             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];  
  9.             NSArray *array = dict[@"V9LG4B3A0"];  
  10.             for (NSDictionary *aDict in array) {  
  11.                 TestModel *model = [[TestModel alloc] init];  
  12.                 [model setValuesForKeysWithDictionary:aDict];  
  13.                 [self.dataSourceArray addObject:model];  
  14.             }  
  15.               
  16.             [self.tableView reloadData];  
  17.         } else {  
  18.             NSLog(@"%@", [connectionError localizedDescription]);  
  19.         }  
  20.           
  21.     }];  
  22. }  

#pragma mark - Table view data source

[objc]  view plain  copy
  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  2. {  
  3.     return 1;  
  4. }  
  5.   
  6. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  7. {  
  8.     return self.dataSourceArray.count;  
  9. }  


返回cell

[objc]  view plain  copy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell" forIndexPath:indexPath];  
  4.     TestModel *model = self.dataSourceArray[indexPath.row];  
  5.     [cell.movieImageView sd_setImageWithURL:[NSURL URLWithString:model.cover]];  
  6.       
  7.     UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];  
  8.     [cell.movieImageView addGestureRecognizer:tapGR];  
  9.       
  10.     return cell;  
  11. }  

添加手势:

[objc]  view plain  copy
  1. - (void)tapAction:(UITapGestureRecognizer *)sender  
  2. {  
  3.     if (self.mpPVC.view) {  
  4.         [self.mpPVC.view removeFromSuperview];  
  5.     }  
  6.     UIView *view = sender.view;  
  7.     UITableViewCell *cell = (UITableViewCell *)view.superview.superview;  
  8.     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];  
  9.     self.selectedIndexPath = indexPath;  
  10.     TestModel *model = self.dataSourceArray[indexPath.row];  
  11.     self.mpPVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:model.mp4_url]];  
  12.       
  13.     self.mpPVC.view.frame = CGRectMake(00, [UIScreen mainScreen].bounds.size.width370);  
  14.     [self.mpPVC.moviePlayer setScalingMode:MPMovieScalingModeAspectFill];  
  15.     [self.mpPVC.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];  
  16.     [cell addSubview:self.mpPVC.view];  
  17.       
  18.       
  19. }  
返回高:

[objc]  view plain  copy
  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     return 370;  
  4. }  

添加划出屏幕小窗口效果:

[objc]  view plain  copy
  1. - (void)scrollViewDidScroll:(UIScrollView *)scrollView  
  2. {  
  3.     TestCell *cell = (TestCell *)[self.tableView cellForRowAtIndexPath:self.selectedIndexPath];  
  4.     // 当前cell在tableView的坐标  
  5.     CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:self.selectedIndexPath];  
  6.     CGRect rectInWindow = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]];  
  7.     self.selectedRect = CGRectMake(rectInTableView.origin.x, rectInTableView.origin.y, cell.movieImageView.bounds.size.width + 20, cell.movieImageView.bounds.size.height + 20);  
  8.       
  9.       
  10.     if ([self.mpPVC.moviePlayer isPreparedToPlay]) {  
  11.         if (rectInWindow.origin.y <= -370 || rectInWindow.origin.y >= [UIScreen mainScreen].bounds.size.height) {  
  12.             [UIView animateWithDuration:.5 animations:^{  
  13.                  
  14.                 self.mpPVC.view.frame = CGRectMake(self.view.bounds.size.width - 170self.view.bounds.size.height - 170170170);  
  15.                 [self.view.window addSubview:self.mpPVC.view];  
  16.                 self.mpPVC.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;  
  17.                   
  18.                   
  19.             }];  
  20.         } else {  
  21.             self.mpPVC.view.frame = self.selectedRect;  
  22.             [self.tableView addSubview:self.mpPVC.view];  
  23.             self.mpPVC.moviePlayer.controlStyle = MPMovieControlStyleDefault;  
  24.         }  
  25.     }  
  26.       
  27. }  

自定义cell

[objc]  view plain  copy
  1. //.h  
  2. #import <UIKit/UIKit.h>  
  3.   
  4. @interface TestCell : UITableViewCell  
  5.   
  6. @property (weak, nonatomic) IBOutlet UIImageView *movieImageView;  
  7.   
  8. @end  
  9.   
  10. //.m  
  11. - (void)awakeFromNib  
  12. {  
  13.     self.movieImageView.userInteractionEnabled = YES;  
  14. }  

cell布局如下




添加model类:

[objc]  view plain  copy
  1. //.h  
  2. #import <Foundation/Foundation.h>  
  3.   
  4. @interface TestModel : NSObject  
  5.   
  6. @property (nonatomiccopyNSString *cover;  
  7. @property (nonatomiccopyNSString *mp4_url;  
  8.   
  9. @end  
  10.   
  11. //.m  
  12. - (void)setValue:(id)value forUndefinedKey:(NSString *)key  
  13. {  
  14.       
  15. }  

最终效果:


每日更新关注:http://weibo.com/hanjunqiang  新浪微博

http://blog.csdn.net/qq_31810357/article/details/49893439
相关文章
|
安全 数据安全/隐私保护 iOS开发
iOS小技能:【发红包】使用tweak和lua脚本结合进行实现
我们开发的大部分越狱程序,都是编译成动态链接库(`例如:介绍的越狱程序(Tweak)开发,就是动态链接库。`),然后通过越狱平台的MobileSubstrate(iOS7上叫CydiaSubstrate)来加载进入目标程序(Target),通过对目标程序的挂钩(Hook),来实现相应的功能。
262 0
|
开发工具 iOS开发 Perl
iOS SDK封装Framework带资源文件封装(二)
iOS SDK封装Framework带资源文件封装
 iOS SDK封装Framework带资源文件封装(二)
|
5G 开发工具 iOS开发
iOS SDK封装Framework带资源文件封装(一)
iOS SDK封装Framework带资源文件封装
iOS SDK封装Framework带资源文件封装(一)
|
移动开发 JavaScript Android开发
iOS12网页视频播放点击全屏按钮会导致闪退
iOS12网页视频播放点击全屏按钮会导致闪退
255 0
|
iOS开发
iOS开发 - 打包静态framework后,引用时必须做的一件事,否则崩溃
iOS开发 - 打包静态framework后,引用时必须做的一件事,否则崩溃
158 0
|
移动开发 JavaScript weex
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
219 0
|
存储 数据处理 iOS开发
iOS开发-本地推送实现方法和数据处理方案(二)
iOS开发-本地推送实现方法和数据处理方案(二)
168 0
|
存储 数据处理 iOS开发
iOS开发-本地推送实现方法和数据处理方案(一)
iOS开发-本地推送实现方法和数据处理方案(一)
208 0
|
iOS开发
iOS开发 - 不通过import引入类名实现push或present
iOS开发 - 不通过import引入类名实现push或present
75 0
|
Android开发 iOS开发
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
354 0
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。