#import "ViewController.h"atom
#import <MediaPlayer/MediaPlayer.h>url
@interface ViewController ()spa
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;.net
@end 3d
@implementation ViewControllerorm
- (void)viewDidLoad {server
[super viewDidLoad];rem
// Do any additional setup after loading the view, typically from a nib.get
// 播放it
[self.moviePlayer play];
// 通知
[self addNotification];
}
- (void)addNotification {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
[notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
}
- (void)mediaPlayerPlaybackStateChange:(NSNotification *)nofication {
switch (self.moviePlayer.playbackState) {
case MPMoviePlaybackStatePlaying:
NSLog(@"正在播放...");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暫停播放.");
break;
case MPMoviePlaybackStateStopped:
NSLog(@"中止播放.");
break;
default:
NSLog(@"播放狀態:%li",self.moviePlayer.playbackState);
break;
}
}
-(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{
NSLog(@"播放完成.");
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (MPMoviePlayerController *)moviePlayer {
if (!_moviePlayer) {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Alizee_La_Isla_Bonita.mp4" withExtension:nil];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
_moviePlayer.view.frame = self.view.bounds;
_moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_moviePlayer.view];
}
return _moviePlayer;
}
@end