I have successfully stopped a video within 30 seconds. But I am not able to dismiss the MP MovieViewController and I want to stop activity for buffering. I have used this code.....
Video Play Code :
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Pungi" ofType:#"mp4"]];
self.movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.movie .controlStyle = MPMovieControlStyleEmbedded;
[ self.movie play];
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
self.movie.initialPlaybackTime = 0.5;
[NSTimer scheduledTimerWithTimeInterval:15.0
target:self
selector:#selector(stopVideo)
userInfo:nil
repeats:NO];
stopVideo :
[self.movie stop];
[self.movie.view removeFromSuperview];
[self.movie release];
Put your MPMoviePlayerController in a separate class and load it:
MoviePlayerViewController.h
#import <MediaPlayer/MediaPlayer.h>
#interface MoviePlayerViewController : UIViewController
#end
MoviePlayerViewController.m
#import "MoviePlayerViewController.h"
MPMoviePlayerViewController *movieController;
#interface MoviePlayerViewController ()
#end
#implementation MoviePlayerViewController
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(#"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(#"exitedFullscreen");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
[self dismissModalViewControllerAnimated:YES];
break;
case MPMovieFinishReasonPlaybackError:
break;
case MPMovieFinishReasonUserExited:
[self dismissModalViewControllerAnimated:YES];
break;
default:
break;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *videoName = #"Videoname";
NSString *filepath = [[NSBundle mainBundle] pathForResource:videoName #"movietype"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[movieController.view setFrame:CGRectMake(0, -20, 320, 480)];
[self.view addSubview:movieController.view];
[movieController.moviePlayer play];
}
ViewController.m
MoviePlayerViewController *player = [[MoviePlayerViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:player animated:YES];
What it does:
MoviePlayerViewController is a custom class which loads an MPMoviePlayerController with a Video. in the viewDidLoad method (or wherever you want it) you LOAD the MoviePlayerViewController.
([self presentModalViewController:animated])....
This has the advantage, that your main class isn't overloaded with moviecrap/definitions and you can EASILY dismiss the MoviePlayerViewController when it is finished using Notifications to check wether it has stopped or not.
if it has stopped:
[self dismissModalViewControllerAnimated:YES];
Hope this Helps!
As code below shows, you should pause and set initialPlaybackTime to -1 before actual stop. This is one of tricky things that MPMoviePlayerController provides.
[_moviePlayerController pause];
if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
}
[_moviePlayerController stop];
if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
}
[_moviePlayerController.view removeFromSuperview];
You need to add notification observer in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
Then add the method:
- (void)moviePlayerPlaybackDidFinishNotification:(NSNotification*)notification
{
[self dismissMoviePlayerViewControllerAnimated];
}
Import the some headers in your header:
#import <MediaPlayer/MediaPlayer.h>
#import <MediaPlayer/MPMoviePlayerViewController.h>
You may also need to balance your "presentMoviePlayer" call with the dismiss somewhere:
[self dismissMoviePlayerViewControllerAnimated];
if you are finished with the resource early, you may be able to release it sooner by using NotificationManager to watch for MPMoviePlayerPlaybackDidFinishNotification.
and also
- (void)dealloc {
[movie release],
movie = nil;
[super dealloc];
}
try this.......
self.movie.initialPlaybackTime = -1;
[self.movie stop];
[self.movie release];
Related
I want to hide the controls from the MPMoviePlayer with this code:
-(IBAction)video:(id)sender {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Intro" ofType:#"mov"];
NSURL *movie = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *control = [[MPMoviePlayerController alloc]initWithContentURL:movie];
//[self.view addSubview: control.view];
control.scalingMode = MPMovieScalingModeFill;
control.controlStyle = MPMovieControlStyleNone;
control.shouldAutoplay = YES;
[control play];
MPMoviePlayerViewController *movieplayer = [[MPMoviePlayerViewController alloc]initWithContentURL:movie];
[self presentMoviePlayerViewControllerAnimated:movieplayer]; }
But that does not work.
You are repeating code. MPMoviePlayerViewController has MPMoviePlayerController. So use it as movieplayervc.moviePlayer.controlStyle = MPMovieControlStyleNone;
have you tried this
[videoPlayerobj setControlStyle:MPMovieControlStyleNone];
My player is set up in the viewDidLoad and this line hides the MPMoviePlayerController. I have intialised my MPMoviePlayer controller as *stream.
stream.view.hidden = YES;
Hope this helps!
You can play video and stop video and remove from your custom view with this code. and MPMoviePlayerController is movie player.
Hope this is useful for you.thank you
-(void)playMovie:(id)sender {
UIButton *buttonThatWasPressed = (UIButton *)sender;
buttonThatWasPressed.enabled = NO;
NSString * str=[[NSBundle mainBundle]pathForResource:#"yo2" ofType:#"mov"];
NSURL * url=[NSURL fileURLWithPath:str];
MPMoviePlayerController * movieController=[[MPMoviePlayerController alloc]initWithContentURL:url];
movieController.controlStyle=MPMovieControlStyleFullscreen;
[movieController.view setFrame:self.view.bounds];
[self.view addSubview:movieController.view];
[movieController prepareToPlay];
[movieController play];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDonePressed:)
name:MPMoviePlayerDidExitFullscreenNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES]; }
This method is called when your video or movie is stop from user or video playback has finish.
-(void) moviePlayBackDonePressed:(NSNotification*)notification {
[_moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];
if ([_moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[_moviePlayer.view removeFromSuperview];
}
_moviePlayer=nil;
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification { // Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}
Well i'm designing an iPhone app which will play video locally. When I click the button in the simulator it plays perfectly but when it stops or when I end it manually it crashed and keeps giving me that problem.. I tried clean, build, analyse and run again but still the same. Any help?
My code is that:
MoviePlayerViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>
#interface MoviePlayerViewController : UIViewController {
}
-(IBAction)playMovie:(id)sender;
#end
and the main bit in MoviePlayerViewController.m
- (IBAction)playMovie:(id)sender {
NSString *movpath = [[NSBundle mainBundle] pathForResource:#"think" ofType:#"mp4"];
MPMoviePlayerViewController *mpviewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:movpath]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self.view addSubview:mpviewController.view];
MPMoviePlayerController *mp = [mpviewController moviePlayer];
[mp prepareToPlay];
mp.scalingMode=MPMovieScalingModeAspectFill;
[[mpviewController moviePlayer] play];
}
- (void)playbackFinishedCallback:(NSNotification *)notification {
MPMoviePlayerViewController *mpviewController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mpviewController];
[mpviewController.view removeFromSuperview];
[mpviewController release];
}
There are few issues in the code, here are the fixes:
1> Remove [mpviewController release]; because it is created using a method which returns *autorelease* object.([notification object]). To release the mpviewController object declare it as instance variable and release it and make it nil.
if(mpviewController != nil)
{
[mpviewController release];
mpviewController = nil;
}
2> As you have declared mpviewController as instance variable, there is no need to access mpviewController variable via [notification object] because its not there as you have not supplied it when you add observer to notification center.
3> Replace following line of code:
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mpviewController];
with
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
Explaination: When you add observer you are not providing any object information but at the time of removal you
So now your code will become:
- (void)playbackFinishedCallback:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[mpviewController.view removeFromSuperview];
if(mpviewController != nil)
{
[mpviewController release];
mpviewController = nil;
}
}
Also, in - (void) dealloc of this controller you should write similar code for releasing mpviewController.
Thanks,
Have you tried making the movie player controller na ivar
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>
#interface MoviePlayerViewController : UIViewController {
}
#property (nonatomic, retain) MPMoviePlayerViewController *mpviewController;
-(IBAction)playMovie:(id)sender;
#end
Then you can do something like so in the implementation file
#synthesize mpviewController;
- (IBAction)playMovie:(id)sender {
NSString *movpath = [[NSBundle mainBundle] pathForResource:#"think" ofType:#"mp4"];
MPMoviePlayerViewController *mpController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:movpath]];
self.mpviewController = mpController;
[mpController release];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self.view addSubview:self.mpviewController.view];
MPMoviePlayerController *mp = [self.mpviewController moviePlayer];
[mp prepareToPlay];
mp.scalingMode=MPMovieScalingModeAspectFill;
[[self.mpviewController moviePlayer] play];
}
- (void)playbackFinishedCallback:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mpviewController];
[mpviewController.view removeFromSuperview];
}
- (void)viewDidUnload {
self.mpviewController = nil;
}
- (void)dealloc{
self.mpviewController = nil;
[super dealloc];
}
This is code supposed to play live video/audio stream,
it work's fine, but the single problem is that it doesnt show the video,
only the audio comes not the video...
#import <MediaPlayer/MediaPlayer.h>
#implementation movieplayerViewController
-(void)awakeFromNib{
NSURL *mediaURL = [NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:mediaURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setMovieSourceType:MPMovieSourceTypeStreaming];
[mp setFullscreen:YES];
[self.view addSubview:[mp view]];
[mp prepareToPlay];
[mp play];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSError *error = [[notification userInfo] objectForKey:#"error"];
if (error) {
NSLog(#"Did finish with error: %#", error);
}
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
If you are using iOS < 4.0 then this would happen. Because in iOS 4.0 there is new Class for playing video. Hope below code helps you.
-(void)playMovieFromLocalPath:(NSString *)strPath{
NSURL *movieURL = [[NSURL alloc]initFileURLWithPath:strPath];
NSString *strVersion = [[UIDevice currentDevice] systemVersion];
float version = [strVersion floatValue];
if(version < 4.0){
MPMoviePlayerController *themovie = [[MPMoviePlayerController alloc]initWithContentURL:movieURL];
themovie.scalingMode=MPMovieScalingModeAspectFill;
[themovie play];
}
else{
MPMoviePlayerViewController *themovie = [[MPMoviePlayerViewController alloc]initWithContentURL:movieURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(DidFinishPlaybackWithReason:) name:MPMoviePlayerPlaybackDidFinishNotification object:themovie.moviePlayer];
[self presentMoviePlayerViewControllerAnimated:themovie];
}
}
-(void)DidFinishPlaybackWithReason:(NSNotification *)aNotification{
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self dismissMoviePlayerViewControllerAnimated];
}
ı wrote this way.thanks for your helps
-(void)awakeFromNib{
[self playMovieFromLocalPath:#"http://eu01.kure.tv:1935/liveedge/shaber.smil/playlist.m3u8"];
}
I'm trying to make an app that plays a video, and I'm having problems. I hear the sound but see no video. I have sincerely tried looking around for a solution, but all the tips don't seem to work. Here's the code:
VideoTestViewController.m
#import "VideoTestViewController.h"
#implementation VideoTestViewController
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
NSString *moviePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"spitfiregrill_iPhone.m4v"];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
theMovie.controlStyle = MPMovieControlStyleDefault;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[theMovie play];
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// 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
VideoTestViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface VideoTestViewController : UIViewController {
}
#end
- (void)viewDidLoad {
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//---play partial screen---
player.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:player.view];
//---play movie---
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperView];
[player autorelease];
}
Video in full screen mode -
- (void)viewDidLoad {
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
MPMoviePlayerViewController *playerViewController =
[[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
[self.view addSubview:playerViewController.view];
//---play movie---
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperView];
[player autorelease];
}
A bit of guesswork - but this is what I'd do:
VideoTestViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface VideoTestViewController : UIViewController
{
MPMoviePlayerController* m_player;
}
- (void) play;
#end
VideoTestViewController.m
#implementation VideoTestViewController
- (void) viewDidAppear:(BOOL)animated
{
[self play];
}
- (void) play
{
NSURL* url = [[NSBundle mainBundle] URLForResource:#"spitfiregrill_iPhone" withExtension:#"m4v"];
m_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[m_player.backgroundView setBackgroundColor:[UIColor blackColor]];
[m_player.view setBackgroundColor:[UIColor blackColor]];
[m_player setControlStyle:MPMovieControlStyleNone];
[[m_player view] setFrame:[self.view bounds]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[m_player play];
[self.view addSubview:[m_player view]];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[m_player.view removeFromSuperview];
[m_player stop];
[m_player release];
m_player = nil;
}
#end
i will like to play a short movie clips in the end of my game in the apps, how will i need to done it ? is there any tutorial on MPMovieplayer ?
thanks for answering my questions
cheers
i currently have these code, how do i play it in the app without poping out the youtube interface ?
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"EndVideo" ofType:#"m4v"]];
MPMoviePlayerViewController * playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[playercontroller release]; playercontroller = nil;
refer
http://stackoverflow.com/questions/5015165/playing-a-video-file-from-server-in-an-iphone-app
I am playing a movie stored on server.Even if you have local file you can convert the path to NSURL & use it
Create a NSObject class CustomMoviePlayerViewController
In the .h file
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface CustomMoviePlayerViewController : UIViewController
{
MPMoviePlayerController *mp;
NSURL *movieURL;
}
- (id)initWithPath:(NSString *)moviePath;
- (void)readyPlayer;
#end
In the .m file
//
// CustomMoviePlayerViewController.m
//
#import "CustomMoviePlayerViewController.h"
#pragma mark -
#pragma mark Compiler Directives & Static Variables
#implementation CustomMoviePlayerViewController
-(void)viewWillDisappear:(BOOL) animated
{
if(mp) {
[mp stop];
}
}
- (id)initWithPath:(NSString *)moviePath
{
mp.controlStyle = MPMovieControlStyleNone;
// Initialize and create movie URL
if (self = [super init])
{
movieURL = [NSURL fileURLWithPath:moviePath];
[movieURL retain];
}
return self;
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
[mp setControlStyle:MPMovieControlStyleFullscreen];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
NSString *deviceModel = [NSString stringWithFormat: #"Device Type: %#\n", [[UIDevice currentDevice] model]];
NSRange range = [deviceModel rangeOfString:#"iPad"];
if(range.location != NSNotFound){
if ([mp loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[[self view] setBounds:CGRectMake(0, 0, 1024, 748)];
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft ){
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
}
else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight ){
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
}
[[self view] addSubview:[mp view]];
[mp play];
}
}
else
{
if ([mp loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:NO];
}
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[[self view] setBounds:CGRectMake(0, 0, 1024, 768)];
[[self view] setCenter:CGPointMake(160, 240)];
[[mp view] setFrame:CGRectMake(0, 0, 1024, 748)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[[self view] addSubview:[mp view]];
[mp play];
}
}
#endif
}
/*---------------------------------------------------------------------------
* For 3.1.x devices
* For 3.2 and 4.x see moviePlayerLoadStateChanged:
*--------------------------------------------------------------------------*/
- (void) moviePreloadDidFinish:(NSNotification*)notification
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
//[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[mp play];
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self dismissModalViewControllerAnimated:NO];
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) readyPlayer
{
//NSLog (#"readyPlayer");
[[UIApplication sharedApplication] setStatusBarHidden:YES];
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
mp.scalingMode= MPMovieScalingModeAspectFit;
if ([mp respondsToSelector:#selector(loadState)])
{
// Set movie player layout
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
//[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setControlStyle:MPMovieControlStyleNone];
[mp setFullscreen:YES];
#endif
file://localhost/Users/ngamacbook/Documents/iPhoneProjectDocs/Team%20Projects/FietIpad_21Feb/Fiet-iPad(05-01-11)
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
#endif
}
else
{
// Register to receive a notification when the movie is in memory and ready to play.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePreloadDidFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
//[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
}
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) loadView
{
[self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
[[self view] setBackgroundColor:[UIColor blackColor]];
self.view.clipsToBounds = YES;
self.view.autoresizesSubviews = YES;
if(mp) {
[mp stop];
}
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:NO];
}
}
- (void)dealloc
{
[mp release];
[movieURL release];
[super dealloc];
}
#end
whenever you want to play the file
NSString *movieFile=[[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp4"];
// Create custom movie player
moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithPath:movieFile] autorelease];
[self presentModalViewController:moviePlayer animated:NO];
[moviePlayer readyPlayer];
Also add MediaPlayer.framework to your project