使用Storyboard / Shows黑屏时,MPMoviePlayerController将无法播放

时间:2014-09-06 01:19:59

标签: ios objective-c mpmovieplayercontroller

我试图在我的应用中播放背景视频,但在使用Storyboard时它没有用。我读了一些关于ARC的东西并尝试了一切,但我的视频不能简单地播放。我尝试了完全相同的东西,但在另一个项目,我删除了Storyboard文件,并在那里工作。我需要在我使用Storyboard的项目中使用它。请帮帮我。

以下是代码:

ViewController.m:

#import "AnimatedLoginViewController.h"
#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];


    UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(65, 90, 190, 40)];
    logo.backgroundColor = [UIColor clearColor];
    [logo setImage:[UIImage imageNamed:@"logo_welcome.png"]];

    [self.view addSubview:logo];


    UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0];
    UIFont *defaultFont = [UIFont fontWithName:@"HelveticaNeue" size:16.0];


    NSDictionary * attributes = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIColor whiteColor], defaultFont, nil] forKeys:[NSArray arrayWithObjects:NSForegroundColorAttributeName, NSFontAttributeName, nil]];

    NSDictionary * attributesBold = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIColor whiteColor], boldFont, nil] forKeys:[NSArray arrayWithObjects:NSForegroundColorAttributeName, NSFontAttributeName, nil]];

    attributes = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIColor blackColor], defaultFont, nil] forKeys:[NSArray arrayWithObjects:NSForegroundColorAttributeName, NSFontAttributeName, nil]];

    attributesBold = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIColor blackColor], boldFont, nil] forKeys:[NSArray arrayWithObjects:NSForegroundColorAttributeName, NSFontAttributeName, nil]];

    UIButton *emailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [emailButton setFrame:CGRectMake(10, screen.size.height==568?455:370, 300, 43)];

    attributedString = [[NSMutableAttributedString alloc] init];
    [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Enter your work " attributes:attributes]];

    [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Email" attributes:attributesBold]];
    [emailButton setAttributedTitle:attributedString forState:UIControlStateNormal];

    [emailButton setBackgroundImage:[UIImage imageNamed:@"SignInMailButton.png"] forState:UIControlStateNormal];
    [emailButton setBackgroundImage:[UIImage imageNamed:@"SignInMailButtonTap.png"] forState:UIControlStateHighlighted];
    [emailButton setBackgroundImage:[UIImage imageNamed:@"SignInMailButtonTap.png"] forState:UIControlStateSelected];
    [emailButton setEnabled:YES];
    [self.view addSubview:emailButton];


    boldFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0];
    defaultFont = [UIFont fontWithName:@"HelveticaNeue" size:14.0];

    attributes = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIColor grayColor], defaultFont, nil] forKeys:[NSArray arrayWithObjects:NSForegroundColorAttributeName, NSFontAttributeName, nil]];

    attributesBold = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIColor whiteColor], boldFont, nil] forKeys:[NSArray arrayWithObjects:NSForegroundColorAttributeName, NSFontAttributeName, nil]];

    UIButton *signInButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [signInButton setFrame:CGRectMake(10, screen.size.height==568?500:410, 300, 43)];

    attributedString = [[NSMutableAttributedString alloc] init];
    [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Do you have an activation code? " attributes:attributes]];

    [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Enter now" attributes:attributesBold]];
    [signInButton setAttributedTitle:attributedString forState:UIControlStateNormal];
    [signInButton setEnabled:YES];
    [self.view addSubview:signInButton];


    [[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

0 个答案:

没有答案