MPMoviePlayerController ipad stuck loading - ios

This is the code i am trying to run to show a movie on the ipad.
What happens is i just see loading... on the bottom of the screen.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *movpath = [[NSBundle mainBundle] pathForResource:#"movie" ofType:#"mov"];
MPMoviePlayerViewController* mpviewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:movpath]];
mpviewController.view.userInteractionEnabled = YES;
[self.view addSubview:mpviewController.view];
MPMoviePlayerController *mp = [mpviewController moviePlayer];
mp.controlStyle = MPMovieControlStyleDefault;
[mp prepareToPlay];
[mp play];

Related

AVPlayer play library video in view

i want to play a video in a frame of a view, but the following code does not work.
can you help me?
MPMediaItem *song = [allVideos objectAtIndex:indexPath.row];
NSLog(#"%#",[song valueForProperty:MPMediaItemPropertyAssetURL]);
NSURL *url = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVPlayer *player = [[AVPlayer alloc] init];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:avAsset];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
player = [[AVPlayer alloc]initWithPlayerItem:playerItem];
//[player seekToTime:kCMTimeZero];
[player play];
You need to use AVPlayerLayer, add this
AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];
[self.view.layer addSublayer:playerLayer];
to your code.
Have you try to use MPMoviePlayerController as like below
MPMoviePlayerController * _player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
CGRect frame = self.frame;
frame.origin = CGPointZero;
_player.view.frame = frame;
[self.view addSubview:_player.view];
[_player prepareToPlay];
[_player play];
Try this one ..
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
player.view.frame = CGRectMake(0,45, 1024, 723); // take your require frame size of player
[player.view setBackgroundColor:[UIColor clearColor]];
player.repeatMode=MPMovieRepeatModeNone;
[self.view addSubview:player.view];
[player play];

Replacing AVPlayer with MPMoviePlayerController

I am trying to replace AVPlayer with MPMoviePlayerController as I want to be able to add an animation with transparent background to a view. The problem is the animation is not displaying with MPMoviePlayerController.
Please find my lines below. The first portion is with AVPlayer and it's work. The second is with MPMoviePlayerController and doesn't.
What the code does is it plays an animation and when this is done it is launching an action.
Code with AVPlayer (that works):
NSString *filepath = [[NSBundle mainBundle] pathForResource:theAnimationFileName ofType:theString;
//NSURL *fileURL = [NSURL fileURLWithPath:filepath];
// First create an AVPlayerItem
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:fileURL];
// Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
// Pass the AVPlayerItem to a new player
controlledPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
AVPlayerLayer *animatedLayer = [AVPlayerLayer playerLayerWithPlayer:controlledPlayer];
[animatedLayer setFrame:CGRectMake(0, 0, 1024, 1024)];
[thisReplacementView.layer addSublayer: animatedLayer];
// Begin playback
[controlledPlayer play];
Code with MPMoviePlayerController (does not display anything):
NSString *moviePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:theString];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
moviePlayer.controlStyle = MPMovieControlModeDefault;
moviePlayer.view.frame = CGRectMake(0, 0, 1024, 1024);
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[thisReplacementView addSubview:moviePlayer.view];
[moviePlayer play];
NSURL *url = [NSURL URLWithString:#"http://iOS.mp4"];
self.moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];
[self presentMoviePlayerViewControllerAnimated:self.moviePlayer];
-(void)playbackFinishedCallback:(NSNotification *)notification{
self.moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
self.moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
[self.moviePlayer.view removeFromSuperview];
}

iOS playing video exc_bad_access

would you please help me with this code, I'm trying to play video but I'm having error Exc_bad_Access code = 1 and some times code = 2:
-(IBAction)BtnPressed:(id)sender{
self.videoview.hidden = false;
NSString *btnTag = [NSString stringWithFormat:#"%d",[sender tag]];
NSString *videofilename = [NSString stringWithFormat:#"%#%#_%#", selectedGender, btnTag, selectedVowel];
//Playing video
NSString *filepath = [[NSBundle mainBundle] pathForResource:videofilename ofType:#"mp4"];
NSLog(#"file name is %#",filepath);
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
//NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:btnTag ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playercontroller];
//[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
//playercontroller.moviePlayer.scalingMode = MPMovieScalingModeNone;
playercontroller.moviePlayer.controlStyle = MPMovieControlStyleNone;
[playercontroller.view setFrame:CGRectMake(30, 50, 150, 200)];
[self.videoview addSubview:playercontroller.view];
[playercontroller.moviePlayer prepareToPlay];
[playercontroller.moviePlayer play];
//playercontroller = nil;
}
- (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];
}
}
Waiting for your advice
It looks like your MPMoviePlayerViewController isn't retained, so it is probably getting deleted.

Movie play error

I am trying play a video on device. Video is this
video
and my code:
NSString *urlStr = #"http://easyhtml5video.com/images/happyfit2.mp4";
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 320, 400);
[moviePlayer play];
When i start app an movie screen is seen but movie doesnt play. Can you help me where is the mistake ?
Try use
NSURL *url = [NSURL URLWithPath:urlStr];
instead of
NSURL *url = [NSURL fileURLWithPath:urlStr];
It is a web url not a file url.
Check this video for reference. This works for me -
- (IBAction)playMovie:(id)sender
{
NSString *filepath = #"http://easyhtml5video.com/images/happyfit2.mp4";
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}

Video playback disappears after a few seconds

i want to play a video within a UIView using MPMoviePlayerController.
After pressing a UIButton, the video appears and starts playing.
Unfortunately after 3-4 seconds, the video gets black, the audio is still playing.
Any ideas?
Thanks for all your time.
(Using Xcode 4.2.1)
-(void) playMovieButtonPressed:(UIButton*) button{
NSString* video = #"testmovie.m4v";
NSString *filepath = [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController* player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
player.movieSourceType = MPMovieSourceTypeFile;
[player.view setFrame:CGRectMake(20, 400, 300, 200)];
[self.view addSubview:player.view];
[player prepareToPlay];
[player play];
}
- (void) moviePlaybackComplete:(NSNotification*) notification {
NSLog(#"videoviewcontroller complete: %#", notification);
MPMoviePlayerController *mymoviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:mymoviePlayerController];
}
I´ve talked to an Apple Engineer. The solution is that the player would have to be an instance variable or property of the view controller. So its still accessible when view was being torn down.
-(void) playMovieButtonPressed:(UIButton*) button{
NSString* video = #"testmovie.m4v";
NSString *filepath = [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
// .....
}

Resources