objective-c 多媒体 视频播放器


ViewController.m文件

———————————————————————————————————————————————————————————————


#import "ViewController.h"

#import "TableViewCell.h"

#import <AVFoundation/AVFoundation.h>

#import "ViewControllerShiPin.h"



@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    UITableView * myTableView;

    NSArray * array;

    NSMutableArray * muarray;

}

@end


@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor=[UIColor whiteColor];

    myTableView=[[UITableView alloc]initWithFrame:CGRectMake(10,20,355 , 647)];

    [self.view addSubview:myTableView];

    myTableView.dataSource=self;

    myTableView.delegate=self;

    array=@[@"爱你一万年",@"稻草人之恋",@"田埂上的梦"];

    muarray =[[NSMutableArray alloc]initWithCapacity:10];

    [self imageRequest:13.0];

    NSLog(@"%@",muarray);

    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return array.count;

    

}

-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell==nil) {

        cell=[[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    }

    

    cell.myimage.image=muarray[indexPath.row];

    cell.label.text=array[indexPath.row];

    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 100;

}

-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath{

    

    ViewControllerShiPin * shipin=[[ViewControllerShiPin alloc]init];

    [self presentViewController:shipin animated:YES completion:nil];

    NSUserDefaults * chuanzhi=[NSUserDefaults standardUserDefaults];

    [chuanzhi setValue:array[indexPath.row] forKey:@"电影"];


}

//提取视频图片

-(void)imageRequest:(CGFloat)timeBySecond{

//for循环  循环出来后将每一个image添加进可变的数组中

    for (int i=0; i<array.count; i++) {

        NSString * strurl=[[NSBundle mainBundle]pathForResource:array[i] ofType:@"mov"];

        NSURL *url=[NSURL fileURLWithPath:strurl ];

        AVURLAsset * urlAsset=[AVURLAsset assetWithURL:url];

        AVAssetImageGenerator *imageGenerator=[AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];

        //创建截取时间点 (参数:视频第几秒,每秒的帧数)

        CMTime time=CMTimeMakeWithSeconds(timeBySecond, 10);

        //接收实际截取图片时间

        CMTime actualTime;

        CGImageRef CGImage =  [imageGenerator copyCGImageAtTime:time actualTime:&actualTime error:nil];

        //显示实际时间

        CMTimeShow(actualTime);

        //CGImage-UIimage

        UIImage * image=[[UIImage alloc]initWithCGImage:CGImage];

        [muarray addObject:image];

        

    }

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

———————————————————————————————————————————————————————————————

XIB.M文件


#import "TableViewCell.h"

#define W 80

#define H W

#define X 10

#define Y 10


@implementation TableViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];

    NSArray * array=[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:nil options:nil];

    self=[array  lastObject];

    [self layout];

    return self;

}


-(void)layout{

    _myimage=[[UIImageView alloc]initWithFrame:CGRectMake(X, Y, W, H)];

    [self addSubview:_myimage];

    _myimage.backgroundColor=[UIColor redColor];

    _label =[[UILabel alloc]initWithFrame:CGRectMake(150, 50, 100, 20)];

    [self addSubview:_label];

    _label.textAlignment=NSTextAlignmentNatural;

    _label.textColor=[UIColor orangeColor];

    

    

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];


    // Configure the view for the selected state

}


@end

———————————————————————————————————————————————————————————————

跳转之后的.m文件

//

//  ViewControllerShiPin.m

//  视频播放器

//

//  Created by DC017 on 15/12/29.



#import "ViewControllerShiPin.h"

#import <MediaPlayer/MediaPlayer.h>


@interface ViewControllerShiPin ()

@property(nonatomic,strong)MPMoviePlayerController *moviePlayerController;

@end

@implementation ViewControllerShiPin


- (void)viewDidLoad {

    [super viewDidLoad];

    [self.moviePlayerController play];

    self.view.backgroundColor=[UIColor whiteColor];

    UIButton * button=[[UIButton alloc]initWithFrame:CGRectMake(0, 20, 60, 20)];

    [button setTitle:@"返回" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];

    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];

    [button addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    

    // Do any additional setup after loading the view.

}

-(MPMoviePlayerController *)moviePlayerController{

    if (!_moviePlayerController) {

        NSUserDefaults * jieshou=[NSUserDefaults standardUserDefaults];

        NSString * strurl=[[NSBundle mainBundle] pathForResource:[jieshou valueForKey:@"电影"] ofType:@"mov"];

        NSURL *url=[NSURL fileURLWithPath:strurl];

        

        _moviePlayerController=[[MPMoviePlayerController alloc]initWithContentURL:url];

        _moviePlayerController.view.frame=self.view.frame;

        //自适应屏幕

        [self.view addSubview:_moviePlayerController.view];

        _moviePlayerController.view.autoresizingMask=UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;

    }

    return _moviePlayerController;

}

-(void)fanhui{

    [_moviePlayerController stop];

    ViewController * viewController=[[ViewController alloc]init];

    [self presentViewController:viewController animated:YES completion:nil];

   

}

- (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









转载于:https://my.oschina.net/u/2483781/blog/552962

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值