运行MPMoviePlayerController时崩溃

时间:2013-05-20 18:59:10

标签: ios xcode mpmovieplayercontroller mpmovieplayer

过去几天,当我尝试在IB中创建的UIView中播放MPMoviePlayerController的实例时,我一直在与我的运动应用程序崩溃作斗争。 应用程序的其余部分运行正常,但如果实例化任何MPMoviePlayer,调试器会暂停应用程序而不会引发异常。这不仅仅是导致它的代码。从其他帖子或书籍中实例化moviePlayer的其他方法会产生相同的结果。

当我运行此代码时,它会退出到主要代码:

NSURL *url = exercise.exerciseVideoURL;//path for vid

    self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];



    [self.moviePlayerController.view setFrame:self.movieHostingView.bounds];

    [self.moviePlayerController prepareToPlay];
    [self.moviePlayerController setControlStyle:MPMovieControlStyleNone];
    [self.movieHostingView addSubview:self.moviePlayerController.view];


    [[self.moviePlayerController view]
     setAutoresizingMask:(UIViewAutoresizingFlexibleWidth |
                          UIViewAutoresizingFlexibleHeight)];

    [[self.moviePlayerController view] setClipsToBounds:YES];
    [[self.moviePlayerController view] setFrame:[[self movieHostingView] bounds]];
    [self.movieHostingView addSubview:self.moviePlayerController.view];
    self.moviePlayerController.repeatMode = MPMovieRepeatModeOne;

我尝试过使用MPMoviePlayer实例化来自其他帖子的代码,对模型进行分区并使用直接路径实例化它,就像在其他帖子中一样,但它仍然会退出到main()。

2 个答案:

答案 0 :(得分:11)

知道了!我已经从断点选项卡中删除了“All Exceptions”并且运行正常。为什么会导致它退出MPMoviePlayer ?! 无论如何,感谢所有帮助过的人。

答案 1 :(得分:0)

您是从磁盘(主捆绑)还是通过互联网阅读视频? 首先,要始终确保您的文件路径正确无误:

NSString *filepath = [url absoluteString];

if ([[NSFileManager defaultManager] fileExistsAtPath:filepath])
{
    NSLog(@"file exists so init player");
}
else
{
    NSLog(@"try again!");
}

在我的情况下,我总是在加载后添加MPMoviePlayerController。 所以你可以这样做:

NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"mp4"];

if ([[NSFileManager defaultManager] fileExistsAtPath:filepath])
{

    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    _moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

    [_moviePlayerController prepareToPlay];

    NSLog(@"frame = %@", NSStringFromCGRect(_moviePlayerController.backgroundView.frame));

    [_moviePlayerController.view setBackgroundColor:[UIColor yellowColor]];
    [_moviePlayerController.view setFrame:CGRectMake(0, 280, 320, 200)];


    // 

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieLoadStateDidChangeNotification:)
                                                 name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:_moviePlayerController];   
}

然后一段时间后:

- (void)movieLoadStateDidChangeNotification:(NSNotification *)notification
{
    [self.view addSubview:_moviePlayerController.view];
}