登录视图上的MPMoviePlayerController黑屏

时间:2014-09-06 13:41:12

标签: ios objective-c mpmovieplayercontroller mpmovieplayer

我试图使用库https://github.com/arturfelipet/AnimatedLogin

我创建了自己的项目并编写了与其示例完全相同的代码(但在我的项目中我使用了Storyboard),但视频只显示黑屏。这就是我创建 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

但是当应用程序显示时,它只是一个黑屏。我在我的LoginViewController中使用此代码,它从App Delegate中呈现如下:

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

也许它必须对此做些什么?我已经尝试了很多东西,非常感谢你的回答。

感谢。

1 个答案:

答案 0 :(得分:1)

这是因为你在viewDidLoad中进行了所有初始化。这意味着处理将阻止您的屏幕,直到完成所有任务。

将该代码放入方法中并在延迟后使用以下方法调用:

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

编辑:在使用@tracifycray检查后结果显示,网址出现了问题,但不知怎的,它的结果为零。正如他所说,&#34;是的,我得到了它的工作。我尝试使用.mov文件,然后它工作了#34;。

相关问题