9.0以前的视屏播放 改良 IOS9.0后_视频 Created MovieView

//
//  ViewController.m
//  9.0以前的视屏播放
//
//  Created by DC020 on 15/12/28.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"
//引入头文件
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
@property(nonatomic,strong)MPMoviePlayerController *moviePlayer;//视频播放控制器
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.moviePlayer play];
    //1.获取缩略图
    [self imageRequest];
    //添加通知
    [self addNotatification];
}
//2.调用获取缩略图的方法
-(void)imageRequest{
    //参数:第一个数组(传入获取图片的时间点),第二个时间选项(获取时间点附近最近的一帧)
    [self.moviePlayer requestThumbnailImagesAtTimes:@[@3.0,@18.5] timeOption:MPMovieTimeOptionNearestKeyFrame];
}
-(void)addNotatification{
    //创建通知中心
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    //播放状态改变的通知
    [notificationCenter addObserver:self selector:@selector(stateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
    //播放完成的通知
    [notificationCenter addObserver:self selector:@selector(finishPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
    //3.视频缩略图的通知(缩略图请求完成的通知)
    [notificationCenter addObserver:self selector:@selector(imageRequestFinished:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:self.moviePlayer];
}

-(void)dealloc{
    //移除所有self里的通知监控
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


//4.此方法每次截图成功都会调用一次
-(void)imageRequestFinished:(NSNotification *)notification{
    NSLog(@"视频截图完成");
    //取出图片
    UIImage *image = notification.userInfo[MPMoviePlayerThumbnailImageKey];
    //保存到相册
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

-(void)finishPlaying:(NSNotification *)notification{
    NSLog(@"播放结束!!!!");
}

-(void)stateChange:(NSNotification*)notification{
    //判断播放状态
    switch (self.moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"正在播放...");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暂停播放...");
            break;
        case MPMoviePlaybackStateStopped:
            NSLog(@"停止播放...");
            break;
        default:
            NSLog(@"播放状态为:%li",self.moviePlayer.playbackState);
            break;
    }
}

#pragma mark 创建视频播放控制器
-(MPMoviePlayerController *)moviePlayer{
    if (!_moviePlayer) {
        //1.获取视频地址(可以本地,也可以网络)
        NSString *urlStr = [[NSBundle mainBundle]pathForResource:@"0" ofType:@"mp4"];
        NSURL *url = [NSURL fileURLWithPath:urlStr];
        //2.初始化播放控制器
        _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
        _moviePlayer.view.frame = self.view.frame;
        //播放器视图->自适应屏幕宽高
        _moviePlayer.view.autoresizingMask =UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_moviePlayer.view];
    }
    return _moviePlayer;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


//
//  ViewController.m
//  改良版播放器
//
//  Created by DC020 on 15/12/29.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"
#import<MediaPlayer/MediaPlayer.h>
@interface ViewController ()
//视频视图控制器
@property(nonatomic,strong)MPMoviePlayerViewController *movieVC;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(MPMoviePlayerViewController *)movieVC{
    if (!_movieVC) {
        //获取视频地址
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"我的少N时代.Our.Times.2015.TS720P.X264.AAC.Mandarin.CHS-ENG.Mp4Ba" ofType:@"mp4"]];
        _movieVC = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
    }
    return _movieVC;
}
- (IBAction)button:(UIButton *)sender {
    //为了保证每次点击都会重新创建播放器
    self.movieVC = nil;
    //模态窗口跳转播放器,期间实例化播放器
    [self presentMoviePlayerViewControllerAnimated:self.movieVC];
}
@end



//
//  ViewController.m
//  IOS9.0后_视频
//
//  Created by DC020 on 15/12/29.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property(nonatomic,strong)AVPlayerViewController *playerVC;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"煎饼侠" ofType:@"mp4"]];
    AVPlayer *player = [AVPlayer playerWithURL:url];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame =CGRectMake(0, 20, 200, 100);
    [self.view.layer addSublayer:playerLayer];
    [player play];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)playMovie:(UIButton *)sender {

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"煎饼侠" ofType:@"mp4"]];
    //创建一个播放器
    AVPlayer *player = [AVPlayer playerWithURL:url];
    //实例化播放视图控制器
    _playerVC = [[AVPlayerViewController alloc]init];
    _playerVC.player = player;
    [self presentViewController:_playerVC animated:YES completion:nil];
}
@end




//
//  ViewController.m
//  MovieView
//
//  Created by DC020 on 15/12/29.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"
#import "TableViewCell.h"
#import<AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UIImageView *imageView;
    UITableView *_tableView;
    NSArray *arrayName;
    NSMutableArray *arrayPic;
    UIImageView *imagePic;
    int i;
    
}

@property(nonatomic,strong)MPMoviePlayerController *moviePlayer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   imagePic=[[UIImageView alloc]init];
    
    arrayPic = [[NSMutableArray alloc]init];
    i = 0;
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width ,self.view.frame.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];

    arrayName = @[@"煎饼侠",@"心在跳"];
    for (int pic = 0; pic < arrayName.count; pic++) {
        [self imageRequest:12.0 local:pic];
    }
}


-(void)imageRequest:(CGFloat)timeBySecond local:(int)num{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:arrayName[num] ofType:@"mp4"] ];
    AVURLAsset *urlAsset = [AVURLAsset assetWithURL:url];
    AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
    CMTime time = CMTimeMakeWithSeconds(timeBySecond, 10);
    CMTime acturalTime;
    CGImageRef cgImage = [imageGenerator copyCGImageAtTime:time actualTime:&acturalTime error:nil];
    CMTimeShow(acturalTime);
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.image = image;
    [arrayPic addObject:imageView];
}


-(MPMoviePlayerController *)moviePlayer{
    if (!_moviePlayer) {
                //1.获取视频地址(可以本地,也可以网络)
        NSString *urlStr = [[NSBundle mainBundle]pathForResource:arrayName[i] ofType:@"mp4"];
        NSURL *url = [NSURL fileURLWithPath:urlStr];
        //2.初始化播放控制器
        _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
        _moviePlayer.view.frame = self.view.frame;
        //播放器视图->自适应屏幕宽高
        _moviePlayer.view.autoresizingMask =UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_moviePlayer.view];
        
        
    }
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 50, 20)];
    [button setTitle:@"关闭" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor whiteColor];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [_moviePlayer.view addSubview:button];
    
    return _moviePlayer;
}
-(void)back{
    [_moviePlayer.view removeFromSuperview];
    _moviePlayer = nil;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    i = (int)indexPath.row;
    NSLog(@"%li",indexPath.row);
    [self.moviePlayer play];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arrayName.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (Cell == nil) {
        Cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    Cell.label.text = arrayName[indexPath.row];
    
    
    imagePic = arrayPic[indexPath.row];
    Cell.image.image = imagePic.image;
    return Cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

转载于:https://my.oschina.net/u/2501648/blog/552961

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值