jorik
jorik

Reputation: 655

How to dismiss MPMoviePlayerController after stopping video

I have successfully stopped a video within 30 seconds. But I am not able to dismiss the MP MovieViewController and I want to stop activity for buffering. I have used this code.....

Video Play Code :

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Pungi" ofType:@"mp4"]];

self.movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.movie .controlStyle = MPMovieControlStyleEmbedded;
[  self.movie  play];
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
self.movie.initialPlaybackTime = 0.5;
[NSTimer scheduledTimerWithTimeInterval:15.0
                                 target:self
                               selector:@selector(stopVideo)
                               userInfo:nil
                                repeats:NO];

stopVideo :

[self.movie stop];
[self.movie.view removeFromSuperview];
[self.movie release];

Upvotes: 1

Views: 6497

Answers (5)

Janosch Hübner
Janosch Hübner

Reputation: 1694

Put your MPMoviePlayerController in a separate class and load it:

MoviePlayerViewController.h

#import <MediaPlayer/MediaPlayer.h> 

@interface MoviePlayerViewController : UIViewController

@end

MoviePlayerViewController.m

#import "MoviePlayerViewController.h"

MPMoviePlayerViewController *movieController;

@interface MoviePlayerViewController ()

@end

@implementation MoviePlayerViewController



- (void)willEnterFullscreen:(NSNotification*)notification {
    NSLog(@"willEnterFullscreen");
}

- (void)enteredFullscreen:(NSNotification*)notification {
    NSLog(@"enteredFullscreen");
}

- (void)willExitFullscreen:(NSNotification*)notification {
    NSLog(@"willExitFullscreen");
}

- (void)exitedFullscreen:(NSNotification*)notification {
    NSLog(@"exitedFullscreen");

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)playbackFinished:(NSNotification*)notification {
    NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    switch ([reason intValue]) {
        case MPMovieFinishReasonPlaybackEnded:
            [self dismissModalViewControllerAnimated:YES];
            break;
        case MPMovieFinishReasonPlaybackError:
            break;
        case MPMovieFinishReasonUserExited:
            [self dismissModalViewControllerAnimated:YES];
            break;
        default:
            break;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];



    NSString *videoName = @"Videoname";

    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:videoName @"movietype"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];


    [movieController.view setFrame:CGRectMake(0, -20, 320, 480)];
    [self.view addSubview:movieController.view];

    [movieController.moviePlayer play];

}

ViewController.m

    MoviePlayerViewController *player = [[MoviePlayerViewController alloc] initWithNibName:nil bundle:nil];
 [self presentModalViewController:player animated:YES];

What it does:

MoviePlayerViewController is a custom class which loads an MPMoviePlayerController with a Video. in the viewDidLoad method (or wherever you want it) you LOAD the MoviePlayerViewController. ([self presentModalViewController:animated]).... This has the advantage, that your main class isn't overloaded with moviecrap/definitions and you can EASILY dismiss the MoviePlayerViewController when it is finished using Notifications to check wether it has stopped or not. if it has stopped:

[self dismissModalViewControllerAnimated:YES];

Hope this Helps!

Upvotes: 5

Hsm
Hsm

Reputation: 1540

You need to add notification observer in viewDidLoad:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

Then add the method:

 - (void)moviePlayerPlaybackDidFinishNotification:(NSNotification*)notification 
 {
    [self dismissMoviePlayerViewControllerAnimated];
 }

Upvotes: 1

John Lingburg
John Lingburg

Reputation: 276

As code below shows, you should pause and set initialPlaybackTime to -1 before actual stop. This is one of tricky things that MPMoviePlayerController provides.

    [_moviePlayerController pause];

    if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
        ((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
    }

    [_moviePlayerController stop];
    if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
        ((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
    }
    [_moviePlayerController.view removeFromSuperview];

Upvotes: 2

Bera Bhavin
Bera Bhavin

Reputation: 703

try this.......

self.movie.initialPlaybackTime = -1; 
[self.movie stop]; 
[self.movie release]; 

Upvotes: 0

Vikas S Singh
Vikas S Singh

Reputation: 1766

Import the some headers in your header:

 #import <MediaPlayer/MediaPlayer.h>
 #import <MediaPlayer/MPMoviePlayerViewController.h>

You may also need to balance your "presentMoviePlayer" call with the dismiss somewhere:

[self dismissMoviePlayerViewControllerAnimated];

if you are finished with the resource early, you may be able to release it sooner by using NotificationManager to watch for MPMoviePlayerPlaybackDidFinishNotification.

and also

- (void)dealloc {
   [movie release], 
   movie = nil;
   [super dealloc];
}

Upvotes: 0

Related Questions