issue playing video in iOS sdk MPMoviePlayerController - ios

I am trying to play video in iPhone, following is the code i have used.
It keep loading but video doesn't play, am i missing anything.
Player is launched but it sonly shows loading and never play the video.
I have also imported MediaPlayer/MediaPlayer.h
-(IBAction)play:(id)sender
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"test_output" ofType:#"mov"];
NSURL *url = [NSURL URLWithString:
moviePath];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
[_moviePlayer prepareToPlay];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}

First, check whether your movie file is added to your project or not.
Second, try creating path with fileURLWithPath. Replace your following code
NSURL *url = [NSURL URLWithString:moviePath];
with
NSURL *url = [NSURL fileURLWithPath:moviePath];

I can't see any code which calls play method of _moviePlayer.moviePlayer.
Try adding following statement to your code.
[_moviePlayer.moviePlayer play];
Add this statement before [self.view addSubview:_moviePlayer.view];
I hope this will work for you

Buddy your code seems to be correct. The problem is definitely with the URL. Double check whether url is correct or not.

Related

Call MPMoviePlayerViewController in app delegate

I am using story board in app.
Using storyboard all views are connected properly
Now new thing I want to do is,
When my Splash screen goes down, I want to show 3 sec video every time user opens the app.
I know how to load video from viewcontroller,
Following is code that I used to launch the video.
- (void)showVideo
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *path = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
NSLog(#"video path :- %#",url);
videoController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
videoController.moviePlayer.controlStyle = MPMovieControlStyleNone;
[self presentMoviePlayerViewControllerAnimated:videoController];
[videoController.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoController];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[videoController.view removeFromSuperview];
[videoController.moviePlayer stop];
videoController = nil;
[self.view removeFromSuperview];
}
But when I use this code in my rootViewController app crashes saying
Attempt to present <MPMoviePlayerViewController:> on <DashbaordVC:> whose view is not in the window hierarchy
But when I use same code in other demo app using navigation controller (No Storyboard) it works fine.
But in this app where story board is used, it crashes.
Also I tried
[self.navigationController pushViewController:self.videoController animated:NO];
Then I thought of adding this code in AppDelegate file and calling the method from ApplicationDidFinishLaunching
But didn't help.
can anyone guide me.... for the same
Also how to add MPMoviePlayerViewController in app delegate.
I think you can do this by MPMoviePlayerController. Try following code
NSString *path = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
videoPlayer = [[MPMoviePlayerController alloc] init];
[videoPlayer.view setFrame:CGRectMake(0.0, viewTopbar.frame.size.height,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - (viewTopbar.frame.size.height + 50.0))];
[videoPlayer setMovieSourceType:MPMovieSourceTypeFile];
[videoPlayer setContentURL:url];
[videoPlayer setControlStyle:MPMovieControlStyleEmbedded];
[videoPlayer setScalingMode:MPMovieScalingModeNone];
[videoPlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoPlayer];
[self.view addSubview:videoPlayer.view];
[videoPlayer play];
And if you dont want full screen then used it like this
[videoPlayer setControlStyle:MPMovieControlStyleNone];

MPMoviePlayerViewController -- how to eliminate black flash when video loads?

I'm using MPMoviePlayerViewController to show a video in my app. It works! Only problem is that there's a black flash just before the movie plays.
How can I get rid of the black flash? I've seen other threads, but they don't seem to have an explanation that works with MPMoviePlayerViewController and is sufficiently specific/detailed for a novice like me (most are for MPMoviePlayerController).
Would really appreciate any help!
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"aiw_intro_video" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] init];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mpvc.moviePlayer.controlStyle = MPMovieControlStyleNone;
[mpvc.moviePlayer setContentURL:fileURL];
[mpvc.moviePlayer play];
[self presentViewController:mpvc animated:NO completion:NULL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mpvc.moviePlayer];
After endlessly iterating and tweaking, I stumbled my way into a solution using MPMoviePlayerController.
Don't forget to declare the property in the .h file, e.g.
#property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
Then
// Add default image to smooth transition
UIImage *myImage = [UIImage imageNamed:#"aiw_launch1136_black.png"];
self.videoStartFrame.image = myImage;
// Play the intro video
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"aiw_intro_video" ofType:#"mp4"]]];
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
[self.moviePlayer.view setFrame:self.view.bounds];
[self.view addSubview:self.moviePlayer.view];
self.moviePlayer.view.hidden = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(isMovieReady:) name:MPMoviePlayerLoadStateDidChangeNotification object:self.moviePlayer];
// Detect that the video is ready and unhide the view
-(void)isMovieReady:(NSNotification *)notification {
MPMoviePlayerController *moviePlayer = [notification object];
if(moviePlayer.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK))
{
self.moviePlayer.view.hidden = NO;
}
}

MPMoviePlayerController.view isn't removed from superview

here is my code
- (void)viewDidLoad
{
...
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"movie" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
_moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayerController.controlStyle=MPMovieControlStyleDefault;
[_moviePlayerController play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieIsOver:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void)movieIsOver:(NSNotification *)notification
{
NSLog(#"movie is over");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.moviePlayerController.view removeFromSuperview];//moviePlayerController is MPMoviePlayerController
}
When the movie plays to the end, I can't see "movie is over" log and the moviePlayerController.view isn't removed. I don't know why.
EDIT:
MPMoviePlayerPlaybackDidFinishNotification works well.I see the "movie is over" log.The problem is moviePlayerController.view isn't removed.
I found out the solution:add
_moviePlayerController.fullscreen = NO;
before removing view from superview
Instead of adding _moviePlayerController.view as subview
[self.view addSubview:_moviePlayerController.view];
you can present _moviePlayerController just like this :
[self presentMoviePlayerViewControllerAnimated: _moviePlayerController];
and in movieIsOver method you simply dismiss
[self dismissMoviePlayerViewControllerAnimated];
I hope its help

MPMoviePlayerController gives me a black empty view,no video playing

iOS 5.1 ,here is the code:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: player.view];
// ...
[player play];
in fact ,this is just the same as the apple reference say,but when I click the button to use this function,there is just a black Rectangle stay there,no vedio playing ,no sound happening,and no crush,so I want to know how to make this work
Sorry for very late reply. The Solution is kind of wierd. But it helped me keep going while facing the same issue.
MPMovieSourceTypeStreaming is the thing you have missed.
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
player.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview: player.view];
// ...
[player play];
You may be sending your
[player play]
message too soon.
Use the following notification observation
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];
To listen for the loadState to change to playable. To check for that update do this :
- (void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
The movie should begin playing once it's ready.
Try this below code for play video from your project Resource :
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"map" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: player.view];
// ...
[player play];
Thanks..!
Building on #Dinesh's answer, your URL creation is incorrect. The value of
[NSURL URLWithString:#"map.mp4"]
will be nil since #"map.mp4" is not a valid URL. To create a valid url from the app's bundle do what Dinesh says. To create a remote URL you would do something like
[NSURL URLWithString:#"http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"]
The URL should contain all it's parts: protocol, host, path, file.
Cheers, Felipe
Add your movie player controller view on main windows like:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview: player.view];
[player play];
Hope so it will work!
Your player should be a property of your view controller, something like this:
.h:
#property(nonatomic,weak)MPMoviePlayerController *moviePlayer;
.m:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
//set the player to your property
self.moviePlayer=player;
[self.view addSubview: self.moviePlayer.view];
// ...
[self.moviePlayer play];
Please use MPMoviePlayerViewController Because of MP4 file. When you use MOV then working perfect!!
MPMoviePlayerViewController *moviePlayerViewController;
-(void)PlayVideoController:(NSString*)videoUrl1 {
NSURL *fileURL = [NSURL URLWithString:videoUrl1];
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
// Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
//Present
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
// Play the movie!
moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayerViewController.moviePlayer play];
}
-(void)myMovieFinishedCallback:(NSNotification*)aNotification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
[moviePlayerViewController release], moviePlayerViewController = nil;
}
You might want to cross check video url for blank spaces. Following code works for me.
- (IBAction)btnPlayVideo:(id)sender {
self.strVideoUrl = [self.strVideoUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#", self.strVideoUrl]];
NSLog(#"Url to play = %#", self.strVideoUrl);
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view setFrame:self.view.bounds];
[self.view addSubview:self.moviePlayerController.view];
self.moviePlayerController.shouldAutoplay = YES;
self.moviePlayerController.fullscreen = YES;
self.moviePlayerController.controlStyle=NO;
[self.moviePlayerController prepareToPlay];
self.moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming;
[self.moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
self.moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view removeFromSuperview];
self.closeVideAdProp.hidden = NO;
btnCloseAd.hidden=FALSE;
}

How to Open quicktime player on iPad

I am programming an iPad app. In this app I want to open a movie URL in default Quicktime player. When I tried to open the URL in the browser the movie starts playing in the browser. How can I open the movie in the default player (so i can get play pause controls..)
Any help would be much appreciated.
Thanks
Saurabh
Finally I think it is not possible to open the quicktime player on iPad. So I searched on google and found MPMoviePlayerViewController class, which solves my problem quite well.
- (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]; }
I have posted a complete tutorial my blog http://www.makebetterthings.com/blogs/iphone/play-video-on-iphone-and-ipad/

Resources