Search code examples
iosobjective-cmpmovieplayercontrollermpmovieplayer

MPMoviePlayerController black screen on loginview


I'm trying to use the library https://github.com/arturfelipet/AnimatedLogin

I created my own project and wrote exactly the same code as in their example (but in my project I used Storyboard), but the video only shows a black screen. Here's the way I create the MPMoviePlayerController:

#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <QuartzCore/QuartzCore.h>

@interface ViewController () {
    MPMoviePlayerController *player;
}

@property (nonatomic, strong) MPMoviePlayerController *player;

@end

@implementation ViewController
@synthesize player;

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect screen = [[UIScreen mainScreen] bounds];

    NSURL *movieUrl = [[NSBundle mainBundle] URLForResource:@"background"  withExtension:@"mp4"];

    self.player = [[MPMoviePlayerController alloc] initWithContentURL:movieUrl];

    player.view.frame = screen;
    player.scalingMode = MPMovieScalingModeFill;
    [self.player setControlStyle:MPMovieControlStyleNone];
    [self.view addSubview:player.view];
    [player prepareToPlay];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playVideo)
                                                 name:MPMoviePlayerReadyForDisplayDidChangeNotification
                                               object:player];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.player];

    [player play];
}

- (void)moviePlayerDidFinish:(NSNotification *)note
{
    if (note.object == self.player) {
        NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
        if (reason == MPMovieFinishReasonPlaybackEnded)
        {
            [self.player play];
        }
    }
}

-(void)playVideo{
    [player play];
}

@end

But it's only a black screen when the app shows up. I'm using this code in my LoginViewController which presents from App Delegate like this:

if (![PFUser currentUser])
        self.window.rootViewController = [UIStoryboard instansiateVCWithClass:[TPLoginViewController class]];

Maybe it has to do something with that? I've tried so many things and would really appreciate an answer.

Thanks.


Solution

  • That is because you doing all initializing in your viewDidLoad. Which means that processing will block your screen until all the task are done completely.

    Put that code in a method and call it after delay using:

    [self performSelector:<#(SEL)#> withObject:<#(id)#> afterDelay:<#(NSTimeInterval)#>];
    

    EDIT: As turned out after checking with @tracifycray, there was some problem with URL,it was turning out nil somehow. As he said, "Yes, I got it to work. I tried to use a .mov-file instead, and then it worked".