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实现视频播放 韩俊强的博客的更多相关文章

  1. iOS中 动态热修补技术JSPatch 韩俊强的博客

    .1.4) JSPatch bridge Objective-C and JavaScript. You can call any Objective-C class and method in Ja ...

  2. iOS中 最新支付宝支付(AliPay) 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 现在的支付方式一般有三种, 支付宝, 微信, 网银. 个人觉得最简单易用的还是支付宝, 微信虽然看起来币支付宝要简单 ...

  3. iOS中 Realm错误总结整理 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 一.错误信息:Attempting to modify object outside of a write tra ...

  4. iOS中 断点下载详解 韩俊强的博客

    布局如下: 基本拖拉属性: #import "ViewController.h" #import "AFNetworking.h" @interface Vie ...

  5. HTML5中 基本用法及属性 韩俊强的博客

    从今天开始更新H5相关学习:希望大家能一起学习,多学习一门语言,多一门乐趣! 了解Html5: Html5基本属性: <!DOCTYPE html> <html lang=" ...

  6. iOS开发中的零碎知识点笔记 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 1.关联 objc_setAssociatedObject关联是指把两个对象相互关联起来,使得其中的一个对象作为另外 ...

  7. iOS中 扫描二维码/生成二维码详解 韩俊强的博客

    最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 指示根视图: se ...

  8. iOS中 扫描二维码/生成二维码具体解释 韩俊强的博客

    近期大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 指示根视图: se ...

  9. iOS中 HTTP/Socket/TCP/IP通信协议具体解释 韩俊强的博客

    简介: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 // 3. 会话层 // 4. 传输层 // 5. 网络层 // 6. 数据链接层 / ...

随机推荐

  1. hash数组快速查找一个字符串中出现最多的字符,并统计出现的次数

    如何快速查找一个字符串中出现最多的字符,并统计出现的次数? 可以使用hash数组,也就是关联数组实现快速查找功能. function seek(str) { var hash = []; var ma ...

  2. Tomcat常用参数的配置

    1.修改端口号 Tomcat端口配置在server.xml文件的Connector标签中,默认为8080,可根据实际情况修改. 修改端口号 2.解决URL中文参数乱码 在server.xml文件的Co ...

  3. ubuntu + 不识别无线网卡简易处理方式 + 需有线联网

    进入ubuntu, 连接有线网; 系统设置 -> 软件和更新 -> 附加驱动 -> 找到无线网卡驱动并更新 若无候选的无线网卡驱动,我就没招了.

  4. 吴恩达深度学习第2课第3周编程作业 的坑(Tensorflow+Tutorial)

    可能因为Andrew Ng用的是python3,而我是python2.7的缘故,我发现了坑.如下: 在辅助文件tf_utils.py中的random_mini_batches(X, Y, mini_b ...

  5. Python3 教程

    Python的3.0版本,常被称为Python 3000,或简称Py3k.相对于Python的早期版本,这是一个较大的升级.为了不带入过多的累赘,Python 3.0在设计的时候没有考虑向下兼容. 查 ...

  6. Winform DevExpress控件库(一) DevExpress控件库的安装与新建第一个DevExpress项目

    前言:因为这段时间要接触到DevExpress控件库,而我本身甚至对winform的控件都了解甚少,所以处在学习中,写下博客主要是为了方便后期的回顾,当然也可以给一些新人第一次接触时做为学习的参考,以 ...

  7. hibernate5学习之理解数据库级并发

    本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50551741 当我们谈起隔离的时候,我们总是假定两个物体直接要么隔 ...

  8. Linux-2.6.25 TCPIP函数调用大致流程

    插口层系统调用send    sys_send        sys_sendtosendto    sys_sendto        sock_sendmsgsendmsg    sys_send ...

  9. Android 实现串口的移植

    安卓串口的实现,需要底层C++配合,不过这次我们根据framework中的思想,直接用API修改提供给JAVA层调用,这个就比较简单了. DEV项目需要,要实现在Android中实现串口的收发功能,有 ...

  10. logistic分类

    对Logistic回归模型,个人做的一些总结: 公式就不套用了,教材上面基本都有而且详细.logistic回归用图形化形式描述如下: logistic回归是一种简单高效的分类模型,它不仅可以通过学习来 ...