ipad movie paused rather play when page is loaded - ipad

I have a movie that loads fine and plays fine but it plays as soon as the page loads. I would like it to load in a paused state requiring the user to hen press play to watch.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"iobserve1" ofType:#"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayerController.view.frame = CGRectMake(60, 44, 900, 656); // player's frame must match parent's
[self.movieView addSubview:moviePlayerController.view];
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
[moviePlayerController prepareToPlay];
[moviePlayerController pause];
}
The pause on the last line does not seem to do anything. Any help? Thanks

Put your code blurb you've written above in viewwillappear.
Then add play at the end like so:
-(void) viewWillAppear: (BOOL) animated {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"iobserve1" ofType:#"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayerController.view.frame = CGRectMake(60, 44, 900, 656); // player's frame must match parent's
[self.movieView addSubview:moviePlayerController.view];
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
}
at the end of it.
In a viewdidappear do a
-(void) viewDidAppear: (BOOL) animated {
[moviePlayerController pause];
}
Remember to remove code from viewDidLoad

You might want to try setting the MPMoviePlayerController property shouldAutoplay to NO.
Add the following line before your prepareToPlay call;
moviewPlayerController.shouldAutoplay = NO;
From the reference:
shouldAutoplay
A Boolean that indicates whether a movie should begin playback automatically.
#property (nonatomic) BOOL shouldAutoplay
Discussion
The default value of this property is YES. This property determines
whether the playback of network-based content begins automatically
when there is enough buffered data to ensure uninterrupted playback.
Availability
Available in iOS 3.2 and later.
Declared In

Related

Use video as background of view controller iOS

Does anyone know how I can use a background video as the login like how AirBNB had their log in screen before? I tried to use a gif, but felt like it didn't have great resolution.
Please let me know! thank you!
Have not tried it myself but it should at least send you to the right direction:
- (void)viewDidLoad
{
[super viewDidLoad]
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"your_video_file_name" ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
// or get your videoURL some another way
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer.view setFrame:self.view.bounds];
[self.view insertSubview:moviePlayer.view atIndex:0]; // to be sure it is a background
// [self.view addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
}

How to init MPMoviePlayerController with video paused

I'm developing an app for iOS where it shows a movie, my method is a class method this is the code
Functions.m
static MPMoviePlayerController *moviePlayer = nil;
+(void)playMP4Movie:(NSString *)moviename
insideOfView:(UIView *)view
autoplay:(BOOL)autoplay{
NSString *path = [[NSBundle mainBundle] pathForResource:moviename ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
moviePlayer.repeatMode = MPMovieRepeatModeOne; // for looping
moviePlayer.view.frame = view.bounds;
[moviePlayer prepareToPlay];
if (autoplay) { [moviePlayer play]; }
else { [moviePlayer stop]; }
[view addSubview:moviePlayer.view];
}
and where I want to call it with video paused:
[Functions playMP4Movie:videoName insideOfView:_videoPlayerView autoplay:NO];
it shows my video but no matter if I set autoplay to YES or NO, the result is always the same... any help I'll appreciate
Try using:
if (autoplay) { [moviePlayer setShouldAutoplay:YES]; }
else { [moviePlayer setShouldAutoplay:NO]; }
And see if you have better luck.

ios problems with video playback

i am using this code to playback a video on ios 6 (in Xcode and device simulator)
- (void) playMovie {
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"flying" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController prepareToPlay];
[moviePlayerController.view setFrame: self.view.bounds];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
[moviePlayerController play];
}
playback starts, but the video aborts after 5 seconds. It simply disappears and i see a black window and no error messages in Xcode.
any clues?
Thanks
The MPMoviePlayerController variable is being released after 5 seconds. I introduced the variable declaration at the implementation level and the definition in the playMovie method. Now it works!

iOS How to play multiple videos on one view

I was wondering how to play multiple videos in one page, like where you have two buttons, and when you press one, it plays a video, and when you press the other it plays a different one. I have this code so far:
-(void)WelcomeVideo1
{
NSURL *url31 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"DirectorsWelcome" ofType:#"mp4"]];
welcomePlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:url31];
welcomePlayer1.controlStyle = MPMovieControlStyleDefault;
welcomePlayer1.shouldAutoplay = YES;
[self.view addSubview:welcomePlayer1.view];
[welcomePlayer1 setFullscreen:YES animated:YES];
}
-(void) moviePlayBackDidFinish:(NSNotification *)aNotification{
[welcomePlayer1.view removeFromSuperview];
welcomePlayer1 = nil;
}
- (void)moviePlayerWillExitFullscreen:(NSNotification*) aNotification {
[welcomePlayer1 stop];
[welcomePlayer1.view removeFromSuperview];
welcomePlayer1 = nil;
}
-(void)storyVideo1
{
NSURL *url4 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"OurStory" ofType:#"mp4"]];
storyPlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:url4];
storyPlayer1.controlStyle = MPMovieControlStyleDefault;
storyPlayer1.shouldAutoplay = YES;
[self.view addSubview:storyPlayer1.view];
[storyPlayer1 setFullscreen:YES animated:YES];
}
-(void) moviePlayBackDidFinish2:(NSNotification *)aNotification{
[storyPlayer1.view removeFromSuperview];
storyPlayer1 = nil;
}
- (void)moviePlayerWillExitFullscreen2:(NSNotification*) aNotification {
[storyPlayer1 stop];
[storyPlayer1.view removeFromSuperview];
storyPlayer1 = nil;
}
but whenever I try to play both videos, the 2nd one I play crashes the app. Any ideas?
Use AVPlayer and AVPlayerLayer instead of MPMoviePlayerController.
Take a look into following apple example.

ShouldAutoplay on MoviePlayerController

I am a new developer, therefore I'm still learning and know I'm doing some things wrong. I have a segmented control object in which when the user presses one of the segments, it goes to play a video. I have it setup to where they press the segment, then have to press a play button to get the video to play. I want to cut out the play button and have it play automatically. This is where I'm having trouble. I found the shouldAutoplay option but when I use it and cut out the button, it won't take me to the video at all. I'm sure I'm not using the shouldAutoplay option correctly. Was hoping for some help or at least a point in the right direction.
- (IBAction)playMovie:(id)sender;
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"mytestimony" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.shouldAutoplay = YES;
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie autorelease];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
Have you tried [theMovie play];?
UPDATE: I just noticed you set shouldAutoplay in theMovie, but what you presented is another instance of MPMoviePlayerViewController, which is moviePlayer. That's why your movie did not autoplay. You should instead have:
[self presentMoviePlayerViewControllerAnimated:theMovie];
Don't play the video, just prepare it for playing and put the shouldAutoPlay as NO, because you don't want to video player to play it automatically.
-(void) SetVideoFile:(NSString *)fileName {
videoFileName=fileName;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:fileName]];
moviePlayer.initialPlaybackTime = 0;
moviePlayer.view.frame = self.view.frame ;
[orientationHandler VideoStart];
[self.view addSubview:moviePlayer.view] ;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.shouldAutoplay = NO;
[moviePlayer prepareToPlay];
}
-(void)moviePlaybackDidFinish:(NSNotification *)notification {
// your custom code which you want to play after the player did finish playing
}

Resources