//
// 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
c++
//
// 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
ide