ios problems with video playback - ios

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!

Related

How to play Video in UIView using URL getting from server in iOS

I want to play video in my UIView from the URL that i got from the JSON Response from server.
This is code that i wrote to play video but nothing happens.
NSURL *movieURL = [NSURL URLWithString:objAlbum.strVideoURL];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[moviePlayerController.view setFrame:self.movieView.bounds];
[self.movieView addSubview:moviePlayerController.view];
// Configure the movie player controller
moviePlayerController.controlStyle = MPMovieControlStyleNone;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
In strVideoURL i'm getting video url.
Check that URL has any spaces. Better to Add below line of code
NSURL *movieURL = [NSURL URLWithString:[objAlbum.strVideoURL stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding]];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[moviePlayerController.view setFrame:self.movieView.bounds];
// Configure the movie player controller
moviePlayerController.controlStyle = MPMovieControlStyleNone;
//[moviePlayerController prepareToPlay];
[moviePlayerController play];
[self.movieView addSubview:moviePlayerController.view];
Note: Print the movie URL and play using browser to check it is valid URL or not..!
Hope it helps you...!
i have solve this using following code
NSURL *vidURL = [[NSURL alloc]initWithString:objAlbum.strVideoURL];
self.movie = [[MPMoviePlayerController alloc]initWithContentURL:vidURL];
self.movie.controlStyle = MPMovieControlStyleDefault;
self.movie.shouldAutoplay = YES;
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
but i want to open it now in UIView defined in TableViewCell like video open in FB(Facebook)

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.

Crash when Running MPMoviePlayerController

Ive been battling with a crash on my exercise app for the past couple of days when I try and play an instance of an MPMoviePlayerController in a UIView created in IB.
The rest of the app runs fine, but if any MPMoviePlayer is instantiated, the debugger pauses the app without raising an exception. It's not just this code that causes it. Other methods of instantiating a moviePlayer from other posts or books cause the same result.
Its when I run this code that it drops out to the main:
NSURL *url = exercise.exerciseVideoURL;//path for vid
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayerController.view setFrame:self.movieHostingView.bounds];
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone];
[self.movieHostingView addSubview:self.moviePlayerController.view];
[[self.moviePlayerController view]
setAutoresizingMask:(UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight)];
[[self.moviePlayerController view] setClipsToBounds:YES];
[[self.moviePlayerController view] setFrame:[[self movieHostingView] bounds]];
[self.movieHostingView addSubview:self.moviePlayerController.view];
self.moviePlayerController.repeatMode = MPMovieRepeatModeOne;
I've tried using MPMoviePlayer instantiating code from other posts, binning off the model and just instantiate it with a direct path like in some of the other posts, but it still drops out to main().
Got it! Ive removed "All Exceptions" from the breakpoint tab and its running fine.Why would that cause it to drop out for MPMoviePlayer?!
Anyhow, Thanks to everyone who helped out.
are you reading video from disk (main bundle) or via internet?
First, be always sure that your filepath is correct:
NSString *filepath = [url absoluteString];
if ([[NSFileManager defaultManager] fileExistsAtPath:filepath])
{
NSLog(#"file exists so init player");
}
else
{
NSLog(#"try again!");
}
In my case I always add MPMoviePlayerController after its loaded.
So you can do something like this:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"demo" ofType:#"mp4"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filepath])
{
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[_moviePlayerController prepareToPlay];
NSLog(#"frame = %#", NSStringFromCGRect(_moviePlayerController.backgroundView.frame));
[_moviePlayerController.view setBackgroundColor:[UIColor yellowColor]];
[_moviePlayerController.view setFrame:CGRectMake(0, 280, 320, 200)];
//
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieLoadStateDidChangeNotification:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:_moviePlayerController];
}
and then some time later:
- (void)movieLoadStateDidChangeNotification:(NSNotification *)notification
{
[self.view addSubview:_moviePlayerController.view];
}

ipad movie paused rather play when page is loaded

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

Resources