ZYXMoviePlayerController.h
#import <UIKit/UIKit.h>
@protocol ZYXMoviePlayerControllerDelegate <NSObject>
- (void)moviePlayerDidFinished;
- (void)moviePlayerDidCapturedWithImage:(UIImage *)image;
@end
@interface ZYXMoviePlayerController : UIViewController
@property (nonatomic, weak) id<ZYXMoviePlayerControllerDelegate> delegate;
@property (nonatomic, strong) NSURL *movieURL;
@end
ZYXMoviePlayerController.m
#import "ZYXMoviePlayerController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ZYXMoviePlayerController ()
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@end
@implementation ZYXMoviePlayerController
- (MPMoviePlayerController *)moviePlayer
{
if (!_moviePlayer) {
// 负责控制媒体播放的控制器
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:self.movieURL];
//_moviePlayer.view.frame = self.view.bounds;
//_moviePlayer.fullscreen = YES;
_moviePlayer.view.frame = CGRectMake(10, 80, self.view.frame.size.width-20, 200);
_moviePlayer.controlStyle = NO;
_moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_moviePlayer.view];
}
return _moviePlayer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];
[self.moviePlayer play];
[self addNotification];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.moviePlayer play];
}
#pragma mark - 添加通知
- (void)addNotification
{
// 1. 添加播放状态的监听
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(stateChanged) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
// 2. 播放完成
[nc addObserver:self selector:@selector(finished) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
// 3. 全屏
[nc addObserver:self selector:@selector(enterFullScreen) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[nc addObserver:self selector:@selector(exitFullScreen) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
// 4. 截屏完成通知
[nc addObserver:self selector:@selector(captureFinished:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
// 数组中有多少时间,就通知几次
// MPMovieTimeOptionExact 精确
// MPMovieTimeOptionNearestKeyFrame 大概
[self.moviePlayer requestThumbnailImagesAtTimes:@[@(25.0), @(40.0)] timeOption:MPMovieTimeOptionNearestKeyFrame];
}
- (void)captureFinished:(NSNotification *)notification{
NSLog(@"captureFinished notification = %@", notification);
if (self.delegate && [self.delegate respondsToSelector:@selector(moviePlayerDidCapturedWithImage:)]) {
[self.delegate moviePlayerDidCapturedWithImage:notification.userInfo[MPMoviePlayerThumbnailImageKey]];
}
}
- (void)finished{
// 1. 删除通知监听
[[NSNotificationCenter defaultCenter] removeObserver:self];
// 2. 返回上级窗体
// 谁申请,谁释放!
if (self.delegate && [self.delegate respondsToSelector:@selector(moviePlayerDidFinished)]) {
[self.delegate moviePlayerDidFinished];
}
NSLog(@"完成");
}
- (void)enterFullScreen{
self.moviePlayer.fullscreen = YES;
_moviePlayer.view.frame = self.view.bounds;
}
- (void)exitFullScreen{
self.moviePlayer.fullscreen = NO;
_moviePlayer.view.frame = CGRectMake(10, 80, self.view.frame.size.width-20, 200);
}
/**
MPMoviePlaybackStateStopped, 停止
MPMoviePlaybackStatePlaying, 播放
MPMoviePlaybackStatePaused, 暂停
MPMoviePlaybackStateInterrupted, 中断
MPMoviePlaybackStateSeekingForward, 下一个
MPMoviePlaybackStateSeekingBackward 前一个
*/
- (void)stateChanged{
switch (self.moviePlayer.playbackState) {
case MPMoviePlaybackStatePlaying:
NSLog(@"播放");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暂停");
break;
case MPMoviePlaybackStateStopped:
// 执行[self.moviePlayer stop]或者前进后退不工作时会触发
NSLog(@"停止");
break;
default:
break;
}
}
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[self.moviePlayer stop];
self.moviePlayer = nil;
}
@end
/*
播放
captureFinished notification = NSConcreteNotification 0x7b6d06f0 {name = MPMoviePlayerThumbnailImageRequestDidFinishNotification; object = <MPMoviePlayerController: 0x7b9674b0>; userInfo = {
MPMoviePlayerThumbnailImageKey = "<UIImage: 0x7b6d0a40>, {1024, 576}";
MPMoviePlayerThumbnailTimeKey = "24.3";
}}
captureFinished notification = NSConcreteNotification 0x7b7e1920 {name = MPMoviePlayerThumbnailImageRequestDidFinishNotification; object = <MPMoviePlayerController: 0x7b9674b0>; userInfo = {
MPMoviePlayerThumbnailImageKey = "<UIImage: 0x7b74c8c0>, {1024, 576}";
MPMoviePlayerThumbnailTimeKey = "29.1";
}}
暂停
完成
*/
ViewController.m
#import "ViewController.h"
#import "ZYXMoviePlayerController.h"
@interface ViewController () <ZYXMoviePlayerControllerDelegate>
@property (nonatomic, strong) ZYXMoviePlayerController *moviePlayer;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (ZYXMoviePlayerController *)moviePlayer{
if (!_moviePlayer) {
_moviePlayer = [[ZYXMoviePlayerController alloc] init];
_moviePlayer.delegate = self;
NSURL *url = nil;
//url = [[NSBundle mainBundle] URLForResource:@"promo_full.mp4" withExtension:nil];
url = [NSURL URLWithString:@"http://localhost/videos/promo_full.mp4"];
_moviePlayer.movieURL = url;
}
return _moviePlayer;
}
- (void)moviePlayerDidFinished{
// 谁申请,谁释放
// dismissViewControllerAnimated将当前视图控制器的模态窗口关闭
//[self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)moviePlayerDidCapturedWithImage:(UIImage *)image{
self.imageView.image = image;
}
- (IBAction)click{
//[self presentViewController:self.playerController animated:YES completion:nil];
[self.navigationController pushViewController:self.moviePlayer animated:YES];
}
@end