iOS中 MediaPlayer framework实现视频播放 韩俊强的博客
iOS开发中播放音乐可以使用MPMusicPlayerController类来实现,播放视频可以使用MPMoviePlayerController和MPMoviePlayerViewController类来实现,同时MPMediaPickerController
类可以用于从系统媒体库中选择媒体播放。这几个类都包含与MediaPlayer.framework框架中。
这里主要介绍MediaPlayer.framework
指定根视图:
RootTableViewController *rootTVC = [[RootTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *rootNC = [[UINavigationController alloc] initWithRootViewController:rootTVC];
self.window.rootViewController = rootNC;
RootTableViewController.m
设置相关属性:
#import "RootTableViewController.h" #import "TestCell.h" #import "TestModel.h" #import "UIImageView+WebCache.h" #import <MediaPlayer/MediaPlayer.h> @interface RootTableViewController () @property (nonatomic, strong) MPMoviePlayerViewController *mpPVC; @property (nonatomic, strong) NSMutableArray *dataSourceArray; @property (nonatomic, strong) NSIndexPath *selectedIndexPath; @property (nonatomic, assign) CGRect selectedRect; @end @implementation RootTableViewController
调用:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"TestCell" bundle:nil] forCellReuseIdentifier:@"testCell"];
self.dataSourceArray = [NSMutableArray array];
[self loadDataAndShow];
}
加载网络数据:
- (void)loadDataAndShow
{
NSURL *url = [NSURL URLWithString:@"http://c.m.163.com/nc/video/list/V9LG4B3A0/y/1-20.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data != nil) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *array = dict[@"V9LG4B3A0"];
for (NSDictionary *aDict in array) {
TestModel *model = [[TestModel alloc] init];
[model setValuesForKeysWithDictionary:aDict];
[self.dataSourceArray addObject:model];
}
[self.tableView reloadData];
} else {
NSLog(@"%@", [connectionError localizedDescription]);
}
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSourceArray.count;
}
返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell" forIndexPath:indexPath];
TestModel *model = self.dataSourceArray[indexPath.row];
[cell.movieImageView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[cell.movieImageView addGestureRecognizer:tapGR];
return cell;
}
添加手势:
- (void)tapAction:(UITapGestureRecognizer *)sender
{
if (self.mpPVC.view) {
[self.mpPVC.view removeFromSuperview];
}
UIView *view = sender.view;
UITableViewCell *cell = (UITableViewCell *)view.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
self.selectedIndexPath = indexPath;
TestModel *model = self.dataSourceArray[indexPath.row];
self.mpPVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:model.mp4_url]];
self.mpPVC.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 370);
[self.mpPVC.moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[self.mpPVC.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
[cell addSubview:self.mpPVC.view];
}
返回高:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 370;
}
添加划出屏幕小窗口效果:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
TestCell *cell = (TestCell *)[self.tableView cellForRowAtIndexPath:self.selectedIndexPath];
// 当前cell在tableView的坐标
CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:self.selectedIndexPath];
CGRect rectInWindow = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]];
self.selectedRect = CGRectMake(rectInTableView.origin.x, rectInTableView.origin.y, cell.movieImageView.bounds.size.width + 20, cell.movieImageView.bounds.size.height + 20);
if ([self.mpPVC.moviePlayer isPreparedToPlay]) {
if (rectInWindow.origin.y <= -370 || rectInWindow.origin.y >= [UIScreen mainScreen].bounds.size.height) {
[UIView animateWithDuration:.5 animations:^{
self.mpPVC.view.frame = CGRectMake(self.view.bounds.size.width - 170, self.view.bounds.size.height - 170, 170, 170);
[self.view.window addSubview:self.mpPVC.view];
self.mpPVC.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
}];
} else {
self.mpPVC.view.frame = self.selectedRect;
[self.tableView addSubview:self.mpPVC.view];
self.mpPVC.moviePlayer.controlStyle = MPMovieControlStyleDefault;
}
}
}
自定义cell
//.h
#import <UIKit/UIKit.h>
@interface TestCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *movieImageView;
@end
//.m
- (void)awakeFromNib
{
self.movieImageView.userInteractionEnabled = YES;
}
cell布局如下
添加model类:
//.h
#import <Foundation/Foundation.h>
@interface TestModel : NSObject
@property (nonatomic, copy) NSString *cover;
@property (nonatomic, copy) NSString *mp4_url;
@end
//.m
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
最终效果:
每日更新关注:http://weibo.com/hanjunqiang
新浪微博
iOS中 MediaPlayer framework实现视频播放 韩俊强的博客的更多相关文章
- iOS中 动态热修补技术JSPatch 韩俊强的博客
.1.4) JSPatch bridge Objective-C and JavaScript. You can call any Objective-C class and method in Ja ...
- iOS中 最新支付宝支付(AliPay) 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 现在的支付方式一般有三种, 支付宝, 微信, 网银. 个人觉得最简单易用的还是支付宝, 微信虽然看起来币支付宝要简单 ...
- iOS中 Realm错误总结整理 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 一.错误信息:Attempting to modify object outside of a write tra ...
- iOS中 断点下载详解 韩俊强的博客
布局如下: 基本拖拉属性: #import "ViewController.h" #import "AFNetworking.h" @interface Vie ...
- HTML5中 基本用法及属性 韩俊强的博客
从今天开始更新H5相关学习:希望大家能一起学习,多学习一门语言,多一门乐趣! 了解Html5: Html5基本属性: <!DOCTYPE html> <html lang=" ...
- iOS开发中的零碎知识点笔记 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 1.关联 objc_setAssociatedObject关联是指把两个对象相互关联起来,使得其中的一个对象作为另外 ...
- iOS中 扫描二维码/生成二维码详解 韩俊强的博客
最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 指示根视图: se ...
- iOS中 扫描二维码/生成二维码具体解释 韩俊强的博客
近期大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 指示根视图: se ...
- iOS中 HTTP/Socket/TCP/IP通信协议具体解释 韩俊强的博客
简介: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 // 3. 会话层 // 4. 传输层 // 5. 网络层 // 6. 数据链接层 / ...
随机推荐
- Linux/Centos笔记目录
Linux介绍 Linux入门--个人感想 Google怎么用linux 初入Linux Windows XP硬盘安装Ubuntu 12.04双系统图文详解 实例讲解虚拟机3种网络模式(桥接. ...
- 关于Matchvs一些使用心得与建议
我的项目是类似<贪吃蛇>玩法的一款IO游戏,就是几个玩家在游戏界面中可以吃食物,也可以相互吃,吃了食物或对方都会变大这样子.我是在用cocos creator做完前端开发的部分后,开始接入 ...
- Mysql B-Tree, B+Tree, B*树介绍
[摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tree索引,InnoDB还支持B+Tree索引,Memory ...
- RabbitMQ日志无法禁用问题
最近使用spring+rabbitmq发现其Debug日志非常多,几天就把服务器磁盘弄爆了. 原来rabbitmq依赖logback.xml输出日志. 在和log4j.properties同目录下加一 ...
- 使用WeihanLi.Redis操作Redis
WeihanLi.Redis Intro StackExchange.Redis 扩展,更简单的泛型操作,并提供一些的适用于业务场景中的扩展 基于 Redis 的五种数据类型扩展出了一些应用: Str ...
- 状态模式、职责链模式——省去if-else的繁琐结构
小时候写日记都是这么写的:上午七点起床,八点之前洗脸刷牙吃早饭,十二点之前好好上课,中午一点,吃午饭,下午两点到六点,上课,下课,找请假,明天妈妈要带我去姥姥家,九点之前,看动画片,九点钟,收拾去姥姥 ...
- springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)
HandlerMethodReturnValueHandler是用于对Controller中函数执行的返回值进行处理操作的,springMVC提供了多个HandlerMethodReturnValue ...
- ajax中xmlhttp.readyState和xmlhttp.status的值及解释
xmlhttp.readyState的值及解释: 0:请求未初始化(还没有调用 open()). 1:请求已经建立,但是还没有发送(还没有调用 send()). 2:请求已发送,正在处理中(通常现在可 ...
- Redis之(三)管理命令
4.1键管理 通过学习五种数据类型的操作命令,可以发现,Redis对每种数据的处理之前,都要先指定该数据的key,然后再指定对该数据进行何种操作. Redis中的key有点类似于Java中的变量名,起 ...
- Dynamics CRM 导出系统中实体的属性字段到EXCEL
我们在CRM中看元数据信息,可以通过SDK中的metadata browser的解决方案包,但该解决方案包只是在可视化上方便了,但如果我们需要在excel中整理系统的数据字典时这个解决方案包就派不上用 ...