Reputation: 1353
In my app, I play a video using this simple code:
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"video" ofType:@"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.movieControlMode = MPMovieControlModeHidden;
[theMovie play];
I'd like to know how to stop the video with code, I have tried [theMovie stop];
but this doesn't work, and an error is given, 'theMovie' undeclared (first use in this function) Which is understandable as the "theMovie" is only declared in the method that plays it. Has anyone got any ideas on how to stop it with out having to show the built in movie player controls? Any Help appreciated.
Upvotes: 0
Views: 1187
Reputation: 1970
If you are creating that video in some method with that code and calling stop
in some other method, the error shows up because theMovie
only exists in the former method. You would need to set up an ivar
or @property
.
Check out this question.
EDIT:
A sample code (not tested):
@interface Foo : UIViewController {
MPMoviePlayerController *_theMovie;
}
@property (nonatomic, retain) MPMoviePlayerController *theMovie;
- (void) creationMethod;
- (void) playMethod;
- (void) stopMethod;
@end
@implementation Foo
@synthesize theMovie = _theMovie;
- (void) creationMethod {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath]; // retain not necessary
self.theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self.theMovie.movieControlMode = MPMovieControlModeHidden;
}
- (void) playMethod {
[self.theMovie play];
}
- (void) stopMethod {
[self.theMovie stop];
}
- (void) dealloc {
[_theMovie release];
}
@end
You would call the creationMethod
somewhere to create your movie player. This is just an example of how a player could be placed in a property so you could use it across many methods but not necessarily a best practice. You could/should take a look at the iPhone documentation on declared properties.
I must note that I haven't used the MPMoviePlayerController
class, though, so exact code might be different.
Upvotes: 1