Last active
August 29, 2015 14:07
-
-
Save wwwins/73380e6cdf773f0dedc5 to your computer and use it in GitHub Desktop.
MPMoviePlayerController sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MovieViewController.h | |
// VideoPlayerDemo | |
// | |
// Created by isobar on 2014/10/22. | |
// Copyright (c) 2014年 isobar. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
#import <MediaPlayer/MediaPlayer.h> | |
#define SIMPLE_MOVIE_VIEW_CONTROLLER | |
#define PLAY_FULL_SCREEN | |
@interface MovieViewController : UIViewController | |
@property (strong, nonatomic) MPMoviePlayerController *moviePlayer; | |
@property (weak, nonatomic) IBOutlet UIButton *ButtonPlay; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MovieViewController.m | |
// VideoPlayerDemo | |
// | |
// Created by wwwins on 2014/10/22. | |
// Copyright (c) 2014年 isobar. All rights reserved. | |
// | |
#import "MovieViewController.h" | |
@interface MovieViewController () | |
@end | |
@implementation MovieViewController | |
@synthesize moviePlayer = _moviePlayer; | |
- (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. | |
} | |
- (IBAction)playMovie:(id)sender | |
{ | |
NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]; | |
#ifdef SIMPLE_MOVIE_VIEW_CONTROLLER | |
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(moviePlayBackDidFinish:) | |
name:MPMoviePlayerPlaybackDidFinishNotification | |
object:nil]; | |
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController]; | |
[moviePlayerViewController.moviePlayer prepareToPlay]; | |
[moviePlayerViewController.moviePlayer play]; | |
#else | |
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url]; | |
_moviePlayer.controlStyle = MPMovieControlStyleDefault; | |
_moviePlayer.shouldAutoplay = YES; | |
#ifdef PLAY_FULL_SCREEN | |
// 全螢幕的時候只抓得到 MPMoviePlayerWillExitFullscreenNotification | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(moviePlayBackDidFinishForFullScreen:) | |
name:MPMoviePlayerWillExitFullscreenNotification | |
object:nil]; | |
[self.view addSubview:_moviePlayer.view]; | |
[_moviePlayer setFullscreen:YES animated:YES]; | |
#else | |
// 非全螢幕的時候才抓得到 MPMoviePlayerPlaybackDidFinishNotification | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(moviePlayBackDidFinish:) | |
name:MPMoviePlayerPlaybackDidFinishNotification | |
object:nil]; | |
CGRect viewInsetRect = CGRectInset ([self.view bounds], 5, 5); | |
[_moviePlayer.view setFrame:viewInsetRect]; | |
[self.view addSubview:_moviePlayer.view]; | |
[_moviePlayer play]; | |
#endif | |
#endif | |
} | |
- (void)moviePlayBackDidFinish:(NSNotification*)notification | |
{ | |
NSNumber *reason = [notification userInfo][MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; | |
NSLog(@">>>>reason:%@",reason); | |
MPMoviePlayerController *player = [notification object]; | |
[[NSNotificationCenter defaultCenter] | |
removeObserver:self | |
name:MPMoviePlayerPlaybackDidFinishNotification | |
object:nil]; | |
if ([player respondsToSelector:@selector(setFullscreen:animated:)]) | |
{ | |
[player.view removeFromSuperview]; | |
} | |
} | |
- (void)moviePlayBackDidFinishForFullScreen:(NSNotification *)notifiaction | |
{ | |
NSLog(@">>>>Exit Full Screen"); | |
[[NSNotificationCenter defaultCenter] | |
removeObserver:self | |
name:MPMoviePlayerWillExitFullscreenNotification | |
object:nil]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment