Hi sam31192,
Take a viewbased template, and name it as MoviePlayer. and add a New File ie UIViewControllerSubClass and name it as CustomMoviePlayer. place a movie which ever you want in resourse folder. i took the 01_01.m4v video. and proceed. the sample code as follows.
MoviePlayerAppDelegate.h
#import <UIKit/UIKit.h>
@classMoviePlayerViewController;
@interface MoviePlayerAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MoviePlayerViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MoviePlayerViewController *viewController;
@end
MoviePlayerAppDelegate.m
#import "MoviePlayerAppDelegate.h"
#import "MoviePlayerViewController.h"
@implementation MoviePlayerAppDelegate
@synthesize window;
@synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[windowaddSubview:viewController.view];
[windowmakeKeyAndVisible];
returnYES;
}
- (void)dealloc {
[viewControllerrelease];
[window release];
[super dealloc];
}
@end
MoviePlayerViewController.h
#import <UIKit/UIKit.h>
#import "CustomMoviePlayer.h"
@interface MoviePlayerViewController : UIViewController {
UIButton *btnObj;
CustomMoviePlayer *moviePlayer;
NSURL *movieURL;
}
@property (nonatomic, retain) IBOutlet UIButton *btnObj;
@property (nonatomic, retain) NSURL *movieURL;
-(IBAction)btnClicked;
@end
MoviePlayerViewController.m
#import "MoviePlayerViewController.h"
@implementation MoviePlayerViewController
@synthesize btnObj;
@synthesize movieURL;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[superviewDidLoad];
}
-(IBAction)btnClicked{
NSString *moviePath = [[NSBundlemainBundle] pathForResource:@"01_01"ofType:@"m4v"];
moviePlayer =[[[CustomMoviePlayeralloc] initWithPath:moviePath ]autorelease];
[selfpresentModalViewController:moviePlayeranimated:YES];
[moviePlayerreadyPlayer];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[superdidReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
CustomMoviePlayer.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface CustomMoviePlayer : UIViewController {
MPMoviePlayerController *mp;
NSURL *movieURL;
}
- (id)initWithPath:(NSString *)moviePath;
- (void)readyPlayer;
@end
CustomMoviePlayer.m
#import "CustomMoviePlayer.h"
@implementation CustomMoviePlayer
- (id)initWithPath:(NSString *)moviePath
{
// Initialize and create movie URL
if (self = [superinit])
{
movieURL = [NSURL fileURLWithPath:moviePath];
[movieURLretain];
}
returnself;
}
/*---------------------------------------------------------------------------
* For 3.2 and 4.x devices
* For 3.1.x devices see moviePreloadDidFinish:
*--------------------------------------------------------------------------*/
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
// Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
{
// Remove observer
[[NSNotificationCenter
defaultCenter]
removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
// When tapping movie, status bar will appear, it shows up
// in portrait mode by default. Set orientation to landscape
[[UIApplicationsharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRightanimated:NO];
// Rotate the view for landscape playback
[[selfview] setBounds:CGRectMake(0, 0, 480, 320)];
[[selfview] setCenter:CGPointMake(160, 240)];
[[selfview] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
// Set frame of movieplayer
[[mpview] setFrame:CGRectMake(0, 0, 480, 320)];
// Add movie player as subview
[[selfview] addSubview:[mpview]];
// Play the movie
[mp play];
}
}
/*---------------------------------------------------------------------------
* For 3.1.x devices
* For 3.2 and 4.x see moviePlayerLoadStateChanged:
*--------------------------------------------------------------------------*/
- (void) moviePreloadDidFinish:(NSNotification*)notification
{
// Remove observer
[[NSNotificationCenter
defaultCenter]
removeObserver:self
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
// Play the movie
[mp play];
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[[UIApplicationsharedApplication] setStatusBarHidden:NO];
[[UIApplicationsharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
// Remove observer
[[NSNotificationCenter
defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[selfdismissModalViewControllerAnimated:YES];
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) readyPlayer
{
[self.navigationControllersetNavigationBarHidden:YES];
mp = [[MPMoviePlayerControlleralloc] initWithContentURL:movieURL];
if ([mp respondsToSelector:@selector(loadState)])
{
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
else
{
// Register to receive a notification when the movie is in memory and ready to play.
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
}
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) loadView
{
[selfsetView:[[[UIViewalloc] initWithFrame:[[UIScreenmainScreen] applicationFrame]] autorelease]];
[[selfview] setBackgroundColor:[UIColorblackColor]];
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void)dealloc
{
[mprelease];
[movieURLrelease];
[superdealloc];
}
@end