7

I'm working on a IOS App (With Storyoard). I have a ViewController:

//movieViewController.h

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

@interface movieViewController : UIViewController {

    MPMoviePlayerViewController *moviePlayer;

}

@property (strong, nonatomic) MPMoviePlayerViewController *moviePlayer;
-(void) playMovie;

@end


//movieViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    [self playMovie];
}

-(void)playMovie
{
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"movie"
                                                      ofType:@"mov"];

    moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL
                                                     fileURLWithPath:videoPath]];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}

When I run I see my viewController with a "Loading..." that takes forever. enter image description here

Community
  • 1
  • 1
luca
  • 36,606
  • 27
  • 86
  • 125

2 Answers2

1

You should call -prepareToPlay and/or -play. Per MPMoviePlayerController's documentation:

Starting in iOS 5.0, in order to facilitate finer-grained playback control, a new movie player is not automatically prepared to play.

zadr
  • 2,505
  • 18
  • 19
  • You are mixing up MPMoviePlayerController and MPMoviePlayerViewController. – Till Apr 09 '13 at 00:02
  • 1
    MPMoviePlayerViewController has a `moviePlayer` property that is a MPMoviePlayerController object. – zadr Apr 09 '13 at 03:12
  • That is true indeed but there is no need to access that property for achieving what the OP asked for. MPMoviePlayerViewController does that out of the box. – Till Apr 09 '13 at 12:08
  • Where is that behavior documented? To the best of my knowledge, that is not the case. – zadr Apr 09 '13 at 19:07
  • Calling up both `prepareToPlay` and `play` did not work. Still it hits the infinite loop – sarat Jul 24 '13 at 02:35
0
-(IBAction)startVideo:(id)sender
{
    NSURL *video = [[NSURL alloc] initWithString:@"http://www.w3schools.com/html/movie.mp4"];
    MPMoviePlayerViewController *controller = [[MPMoviePlayerViewController alloc]initWithContentURL:video];
    controller.view.frame = CGRectMake(20, 50, 280, 180);

    controller.moviePlayer.view.center = self.view.center;
    [self.view addSubview:controller.view];

    [controller.moviePlayer prepareToPlay];
    [controller.moviePlayer play];
}
Jayesh Mardiya
  • 770
  • 7
  • 17