1.iOS8.0的实现方法
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
@property (nonatomic, strong) MPMoviePlayerController *mpc;
@end
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSURL *URL = [[NSBundle mainBundle] URLForResource:@"abc.mp4" withExtension:nil];
MPMoviePlayerController *mpc = [[MPMoviePlayerController alloc] initWithContentURL:URL];
mpc.view.frame = CGRectMake(50, 50, 200, 200);
[self.view addSubview:mpc.view];
[mpc play];
self.mpc = mpc;
}
- (void) moviePlayerPlaybackDidFinishNotification:(NSNotification *) notification {
MPMovieFinishReason reason = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
NSURL *URL = [[NSBundle mainBundle] URLForResource:@"高清.mp4" withExtension:nil];
self.mpc.contentURL = URL;
[self.mpc play];
}
}
2.iOS9.0的实现方法,可实现画中画的效果
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
AVPlayerViewController *pvc = [[AVPlayerViewController alloc] init];
NSURL *URL = [[NSBundle mainBundle] URLForResource:@"Alizee_La_Isla_Bonita.mp4" withExtension:nil];
pvc.player = [[AVPlayer alloc] initWithURL:URL];
pvc.delegate = self;
[self presentViewController:pvc animated:YES completion:^{
[pvc.player play];
}];