创建MPMoviePlayerController时崩溃

时间:2014-07-03 14:02:06

标签: ios objective-c mpmovieplayercontroller

当我的应用首次启动时,我正尝试播放视频,但它崩溃了:

moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4v"]]];

我在.h文件中有一个moviePlayer的属性。

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;

以下是它的所有代码:

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"hasPerformedFirstLaunch"])
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4v"];

    if ([[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        NSLog(@"file exists so init player");

        self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4v"]]];
        [self.moviePlayer.view setFrame:CGRectMake(10, 10, width - 20, height - 10)];
        self.moviePlayer.repeatMode = MPMovieRepeatModeOne;
        [self.moviePlayer play];

        [self.view addSubview:self.moviePlayer.view];
    }
    else
    {
        NSLog(@"file not found");
    }

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasPerformedFirstLaunch"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

2 个答案:

答案 0 :(得分:0)

您是否导入了MediaPlayer框架?

@import MediaPlayer

答案 1 :(得分:0)

问题是文件没有位于mainBundle中。我把它添加到mainBundle中,它按预期工作。