Hello All ! You might have seen the application “Livestation” for iPad & iPhone. If you haven’t heard about it, just google once. I am going to demonstrate “How to build the customized user interface for MPMoviePlayerViewController?” Just go through entire steps of this post or watch video tutorials listed below.
1) Create new project named “CustomMediaPlayer” & Import MediaPlayer.framework. ( If you don’t know how to add frameworks in XCode4, please go through this post. Read first step, It gives an idea of adding framework. )
2) Open Application delegate header file & place code as follows.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#import <UIKit/UIKit.h> #import <MediaPlayer/MediaPlayer.h> @interface CustomMediaPlayerAppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UINavigationController *nvCtr; @property (strong, nonatomic) UIViewController *vCtr; @property (strong, nonatomic) MPMoviePlayerViewController *mPlayer; -(void)initAndPlayMovie:(NSURL *)movieURL andViewController:(UIViewController*)vCtr; - (void)stopTapped:(id)sender; @end |
3) Register a notification for “Media Player did finish playing” on application launch.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; MainScreenVCtr *tempVariable=[[MainScreenVCtr alloc] initWithNibName:(UI_USER_INTERFACE_IDIOM())?@"MainScreeniPad":@"MainScreenVCtr" bundle:nil]; self.nvCtr=[[UINavigationController alloc] initWithRootViewController:tempVariable]; self.nvCtr.view.frame=CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height-20); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; [self.window addSubview:self.nvCtr.view]; self.vCtr=[[CustomControllsVCtr alloc] initWithNibName:(UI_USER_INTERFACE_IDIOM())?@"CustomControllsiPad":@"CustomControllsVCtr" bundle:nil]; [self.window makeKeyAndVisible]; return YES; } |
4) Place following method in Application delegate method for initialize & terminate movie player.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// a method to start movie player -(void)initAndPlayMovie:(NSURL *)movieURL andViewController:(UIViewController*)vCtr { self.mPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL]; // we have movie from file - Alizee :) [self.mPlayer.moviePlayer setMovieSourceType:MPMovieSourceTypeFile]; // we don't need standard controlls as we have built our own [self.mPlayer.moviePlayer setControlStyle:MPMovieControlStyleNone]; // aspect fit to screen mode [self.mPlayer.moviePlayer setScalingMode:MPMovieScalingModeAspectFit]; // full screen mode [self.mPlayer.moviePlayer setFullscreen:YES animated:YES]; // to start movie player [vCtr presentMoviePlayerViewControllerAnimated:self.mPlayer]; // now we will add our own view over video player self.vCtr.view.frame=CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height-20); [self.mPlayer.view addSubview:self.vCtr.view]; } - (void)stopTapped:(id)sender{ [self.mPlayer.moviePlayer stop]; } -(void)moviePlayBackDidFinish:(NSNotification*)notification { [self.mPlayer dismissMoviePlayerViewControllerAnimated]; [self.vCtr.view removeFromSuperview]; } |
5) Create a new View controller with XIB interface as follows.
Grab the sample code from here.
Make sure to add a video in project. I have not added a video in project ( to compress the size of sourcecode ).
To view complete demonstration just go through following videos once.


hi
Again you posted a great tutorial..
I have a query regarding use of youtube url in this example but it isn’t working..:(
Many Thanx
Dude – youtube URL requires inbuilt Youtube application support. MoviePlayer won’t support that formats. Sorry, Thatz not in my hands.
I used ur same technic for in tableview its producing sound and all other method working perfectly video is not showing sound is there
Hi, Mr. Sagar, Thanks for this good tutorial. I am currently learning iOS and have a small project where I want to keep record of played video with their last status and how many time it has been played in media player
Can you help me on this?