tags:

views:

282

answers:

1

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 would 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.

+1  A: 

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.

mga
Yes, I understand why calling stop in another method won't work in this instance, but could you explain further about what I would need to do? Im not really fully understanding what you mean yet with ivar or @property. Perhaps an example? :D Oh and I checked out the question, and I couldn't relate it to my scenario. Thanks anyway.
Jessica
edited with an example
mga
Thank you, the code works perfectly and I understand it too. :)
Jessica