//數組
// ViewController.matom
// 媒體 視頻播放 9.0之前url
//spa
// Created by DC017 on 15/12/28..net
// Copyright © 2015年 DC017. All rights reserved.3d
//rest
#import "ViewController.h"orm
#import <MediaPlayer/MediaPlayer.h>視頻
@interface ViewController ()server
@property(nonatomic,strong)MPMoviePlayerController *moviePlayerController;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.moviePlayerController play];
[self addNotification];
[self suoluetu];
}
-(MPMoviePlayerController*)moviePlayerController{
if (!_moviePlayerController) {
NSString * strurl=[[NSBundle mainBundle] pathForResource:@"田埂上的夢" 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)addNotification{
//建立通知中心(通知中心爲單例模式)
NSNotificationCenter * notificationCenter=[NSNotificationCenter defaultCenter];
//播放狀態改變的通知
[notificationCenter addObserver:self selector:@selector(zhuangtaigaibian:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayerController];
//視頻播放完成時通知
[notificationCenter addObserver:self selector:@selector(bofangwanchengtongzhi:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerController];
//視頻縮略圖通知
[notificationCenter addObserver:self selector:@selector(suolueotu:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:self.moviePlayerController];
}
-(void)zhuangtaigaibian:(NSNotification *)notification{
//判斷播放狀態
switch (self.moviePlayerController.playbackState) {
case MPMoviePlaybackStatePlaying:
NSLog(@"正在播放");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暫停播放");
break;
case MPMoviePlaybackStateStopped:
NSLog(@"中止播放");
break;
default:
NSLog(@"播放狀態爲%li",self.moviePlayerController.playbackState);
break;
}
}
-(void)bofangwanchengtongzhi:(NSNotification *)notification{
//播放完成
NSLog(@"完成播放");
}
-(void)suoluetu{
//視頻縮略圖
//參數1 第一個數組(傳入獲取圖片的時間點)參數2 時間選項 (獲取時間點附近最近的一幀) //注意小數點
[self.moviePlayerController requestThumbnailImagesAtTimes:@[@6.5,@18.5] timeOption:MPMovieTimeOptionNearestKeyFrame];
}
-(void)suolueotu:(NSNotification * )notification{
//視頻縮略圖
NSLog(@"視頻截圖完成");
//取出照片
UIImage *image=notification.userInfo[MPMoviePlayerThumbnailImageKey];
//保存到相冊
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc{
//移除全部的self裏的通知
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end