iosobjective-cvideo-streamingmpmovieplayercontrollermpmoviewcontroller

MPMoviePlayerViewController not playing


I'll keep it simple, heres the code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
VideoEntry *entry = [videoEntries objectAtIndex:indexPath.row];

[HCYoutubeParser h264videosWithYoutubeURL:[NSURL URLWithString:entry.url] completeBlock:^(NSDictionary *videoDictionary, NSError *error) {
    NSArray *urls = [videoDictionary allValues];
    NSURL *url = [NSURL URLWithString:[urls objectAtIndex:0]];
    [mp.moviePlayer setAllowsAirPlay:YES];
    [mp.moviePlayer setContentURL:url];
    [mp.moviePlayer prepareToPlay];
    [mp.moviePlayer play];
    [self presentMoviePlayerViewControllerAnimated:mp];
}];

}

mp is a MPMoviePlayerViewController. The view controller is presented but then the movie doesn't start, it just says "Loading..." and before you ask I'm 100% sure the links work.

Thanks!


Solution

  • It doesn't work because the completion block isn't called on the main thread. You can solve by force the execution of your code on the main thread:

    [HCYoutubeParser h264videosWithYoutubeURL:[NSURL URLWithString:entry.url]
                                completeBlock:^(NSDictionary *videoDictionary, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            NSArray *urls = [videoDictionary allValues];
            NSURL *url = [NSURL URLWithString:[urls objectAtIndex:0]];
            [mp.moviePlayer setAllowsAirPlay:YES];
            [mp.moviePlayer setContentURL:url];
            [mp.moviePlayer prepareToPlay];
            [mp.moviePlayer play];
            [self presentMoviePlayerViewControllerAnimated:mp];
        });
    }];