Trust me, all the videos on you tube will work for earlier versions of iOS, but
I needed it to work for me in iOS 4.2, and after a lot of mix and match of code, found the following to
work well. Here is the code in the ViewController implementation :
-(IBAction)playMovie {
//Get the Movie
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@”sample_iPod” ofType:@”m4v”];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
//Place it in subview, else it won’t work
theMovie.view.frame = CGRectMake(0, 0, 480, 320);
[self.view addSubview:theMovie.view];
//Resize window – a bit more practical
UIWindow *moviePlayerWindow = nil;
moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
[moviePlayerWindow setTransform:CGAffineTransformMakeScale(0.9, 0.9)];
// Play the movie.
[theMovie play];
}
I have this setting for LandScape on a iPhone 4.
please help me i try to play video on ios 4.2 code not working ?
#import “nycViewController.h”
@implementation nycViewController
– (IBAction) Go1{
//Get the Movie
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@”video33″ ofType:@”mp4″];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
//Place it in subview, else it won’t work
theMovie.view.frame = CGRectMake(0, 0, 480, 320);
[self.view addSubview:theMovie.view];
//Resize window – a bit more practical
UIWindow *moviePlayerWindow = nil;
moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
[moviePlayerWindow setTransform:CGAffineTransformMakeScale(0.9, 0.9)];
// Play the movie.
[theMovie play];
}
Hi Mato,
Kindly run the debugger and post the error stack. I am assuming you are getting run-time errors. am I right?
Please SheshPai check this i send you the project
LINK
Hi Mato,
Sorry for the delay in replying.
You have done everything right, except that I had not pasted the complete code. Otherwise, we cannot test our brain cells 😀
theMovie is a member variable, so declare it in the header file. VideoViewController.h
#import
#import
@interface VideoViewController : UIViewController {
MPMoviePlayerController *theMovie;
}
– (IBAction)video1;
@end
In your VideoViewController.m you have removed even the dealloc function, that is not the right thing to do.
I have copied your corrected file other than the header, so please check this out.
#import “VideoViewController.h”
@implementation VideoViewController
– (IBAction)video1 {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@”debutteaser” ofType:@”mp4″];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
//Place it in subview, else it won’t work
theMovie.view.frame = CGRectMake(0, 0, 480, 320);
[self.view addSubview:theMovie.view];
//Resize window – a bit more practical
UIWindow *moviePlayerWindow = nil;
moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
[moviePlayerWindow setTransform:CGAffineTransformMakeScale(0.9, 0.9)];
// Play the movie.
[theMovie play];
}
// Override to allow orientations other than the default portrait orientation.
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
– (void)dealloc {
[theMovie dealloc];
[super dealloc];
}
@end
There you go, this works perfectly on my device. Let me know if you still have issues :).
Hi!
I’m new to iOS development. The viewcontroller.h file should be like this?
#import
#import
@interface iPhone_GAME001ViewController : UIViewController {
MPMoviePlayerController *theMovie;
}
– (IBAction)playMovie:(id)sender;
@end
What else should I do in the Interface Builder? I created a PLAY button and connected to “playMovie” action.
When I start the application and click PLAY then the App crashes.
Please help me with ideas.
Thanks,
atis
Hi Atis,
There is no error in whatever you have posted, and moreover you are getting a runtime error…SO.
The crash may be due to many reasons, however there is nothing to worry about.
If you post the debug error stack, I can help you. Before that you can try the following.
1. Ensure that the Xcode->Project->Edit Project settings are compatible with your SDK. Xcode Project format is 3.2 compatible and I am using Base SDK iOS 4.2.
2. Ensure that the MediaPlayer.framework is added in frameworks.
3. Ensure movie extension is of supported type.
Rest could be coding issues, which I’ll have to check the error stack for :).
Best of luck.
thank you SheshPai for your help
i have problem streaming video on SDK 4.2 this code working only for ipad i dont know what it’s wrong i try for iphone but not working play only audio no video
pleas help me thank you
– (IBAction) Goplay{
NSString *moviePath = @”link”;
NSURL *movieURL = [NSURL URLWithString:moviePath];
MPMoviePlayerViewController *movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
if ([self respondsToSelector:@selector(presentMoviePlayerViewControllerAnimated:)]) {
[self presentMoviePlayerViewControllerAnimated:movieViewController];
[movieViewController release], movieViewController = nil;
} else {
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish)
name:MPMoviePlayerPlaybackDidFinishNotification
object:movieViewController];
}
}
– (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer release];
}
Hi Mato,
What are you trying to do?
I would think of removing the if and else so that notification is sent when the movie ends.
Also, your selector should be this – @selector(moviePlayBackDidFinish:) observe the colon.
I had no problems whatsoever executing your code and seeing the video and audio.
Mato,
These links could explain the cause for you:
http://stackoverflow.com/questions/3184537/mpmovieplayerviewcontroller-crashes-on-os-3-0
http://stackoverflow.com/questions/3915076/mpmovieplayercontroller-throws-errors-only-in-universal-app
Thanks a lot! I have everything working great but I have one question, If I have this run on load how do I get it to go back to the regular UI view after this runs?
Hi Ryan,
If I understand what you are asking,
You want to have a regular view from where you want the movie to run in a another view and then when the movie ends it should move back to the regular view.
For any such thing where you want to come up with a new view by keeping the existing view intact, the trick is to always construct a view controller for the new view, and create these controllers which have a associated view already (e.g movie controllers or pickers (for Email, SMS and what not!).
In your case, then you can dismiss the parent view controller in the moviePlayBackDidFinish.
Enjoy.
Could you please post a zip file of the project? Can’t get it to work…
I did not have time to look into the code, See if it works for you 🙂
http://www.megaupload.com/?d=13PA2I93
I wants to cut a video from a inbetween..and record a clip to the local system
Hi There,
This post is really helpful . i was able to play video file (.m4v) using code. I am having trouble playing .mp4 video file using same code.
I would appreciate your help or input.
Thanks,
Tapan Desai
Hi Tapan,
I will give some steps which will appear rudimentary but meant for general use.
1. See that your mp4 movie plays in VLC or GOM player
2. Do not forget to add ofType:@”mp4″ without the . “dot”
3. Check the form factor of the movie (I am not sure this is needed though)
Thanks.
Can you please give an example about launching a video when the application starts? Thanks in advance!
Hi Pantelis,
I found a post on iPhoneDevSDK.com. Try to call the method in the RootControllers ViewDidLoad, and do let me know if this worked.
Let me know if this helped.
http://www.iphonedevsdk.com/forum/iphone-sdk-development/92812-playing-video-startup-app-without-controls.html
Thanks.
Thanks for your reply.
Your sample code can play in ViewDidLoad only if ARC is dissabled. Don’t know why…
I an now trying to make it iPad compatible because “theMovie.view.frame = CGRectMake(0, 0, 480, 320);”
Any suggestions for that?
I am also trying to figure out a why to revert landface orientation to portrait after th video ends…
Hi, I am new on these.
I try to play the video, but I have problem with these line:
NSURL *movieURL = [[NSURL fileURLWithPath:moviepath] retain];
the ERROR is: ARC forbids explicit message send of ‘retain’
When I write retain in the little popup display says VOID retain but have a red line in the middle of retain.
What I can do???
Thanks a lot…
You are using ARC – auto reference counting so you do not need retain. Change to:
NSURL *movieURL = [NSURL fileURLWithPath:moviepath];
Hi I launched the app to start playing the file and this is what it looked like:
Any idea how to get player controls (like the play / pause button, and the back button so they can go back to the screen that started the video)?
Thanks for the tutorial
Newbie to the world of programming here and I have a question. Your code here is the closest I’ve seen so far in trying to accomplish what I want done.
I have a multi-view application that I am creating in Xcode 4.3.3 and there are multiple pages pushed via segue in a table. Each page is supposed to have it’s own video that goes along with a lesson. I’ve been through the Apple development guides and have not seen anything that would show me how to accomplish this.
Right now I am triggering each video via a simple “Play Movie” button but if there is a way to embed the video right on that screen that’d be nice to pull off. I would prefer for the videos to be local but the only code I’ve successfully pulled of so far is streaming from a remote server.
Could you please help me?
hello…. Already i added layer in mpmovieplayer for watermark purpose….text is displaying fine…but when i entered into full mode screen text is not displaying….what i can do for displaying text in full mode screen above layer…..please help me soon…thanking in advance