Search code examples
objective-cmpmovieplayercontroller

Objective C - MPMoviePlayerController cuts out


So im trying to play a movie like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    vidLocation=[[NSURL alloc] initWithString:@"http://cdn.webwaterfalls.com/uni/introNew2.mp4"];

    UIView * vidView=[[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];

    [self.view addSubview:vidView];

    MPMoviePlayerController * vid=[[MPMoviePlayerController alloc] initWithContentURL:vidLocation];

    if(vid){

        [vid setContentURL:vidLocation];

        [vid setMovieSourceType:MPMovieSourceTypeStreaming];

        [vid.view setFrame: vidView.bounds];

        [vidView addSubview:vid.view];

        [vid play];

    }
}

And the movie starts playing, displaying all the pause, seek to, volume and full screen controls.

But after 3 seconds the movie just cuts out, showing nothing. No movie. No controls.

Ive tried different movies (all of which play on an ipad fine), but they all give up playing after 3 seconds.

Can anyone explain what I am doing wrong?


Solution

  • Try something like this:

    - (void)viewDidAppear:(BOOL)animated {
       [super viewDidAppear:animated];
    
       //Use Apple's sample stream
       NSURL *mediaURL = [NSURL URLWithString:@"http://cdn.webwaterfalls.com/uni/introNew2.mp4"];
       self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaURL];
    
       //Begin observing the moviePlayer's load state.
       [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerLoadStateChanged:)
                                                 name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:self.moviePlayer];
    
       [self.moviePlayer setShouldAutoplay:NO];//Stop it from autoplaying
       [self.moviePlayer prepareToPlay];//Start preparing the video
    }
    
    - (void)moviePlayerLoadStateChanged:(NSNotification *)notification{
        NSLog(@"State changed to: %d\n", self.moviePlayer.loadState);
        if(self.moviePlayer.loadState == MPMovieLoadStatePlayable){
           //if load state is ready to play
          [self.view addSubview:[self.moviePlayer view]];
          [self.moviePlayer setFullscreen:YES];
          [self.moviePlayer play];//play the video
        }
    
    }