为什么这行阻止我的应用程序

时间:2011-10-19 12:33:55

标签: iphone video

在我的viewDidLoad方法中,我有以下几行:

    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"video1" ofType:@"mp4"];
    NSString *path2 = [[NSBundle mainBundle] pathForResource:@"video2" ofType:@"mp4"];

    MPMoviePlayerController *videoPlayer1 = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path1]];
    MPMoviePlayerController *videoPlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path2]];

    UIImage *thumbnail1 = [videoPlayer1 thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
    UIImage *thumbnail2 = [videoPlayer2 thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];

在这种状态下,我的应用程序在访问此类时被阻止。如果我删除这两行,它的效果很好:

    UIImage *thumbnail1 = [videoPlayer1 thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
    UIImage *thumbnail2 = [videoPlayer2 thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];

它工作得很好。所以我正在开发ios 4并且我有视频。可能出错了?谢谢

编辑:我在我的控制台中有这个:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray removeObjectAtIndex:]: index 1 beyond bounds [0 .. 0]'

1 个答案:

答案 0 :(得分:1)

MPMoviePlayerController API是异步的,我不相信它可以在你加载电影之前获得缩略图。您可以注册MPMoviePlayerLoadStateDidChangeNotification通知,并检查moviePlayer loadState是否为MPMovieLoadStatePlayable。注意加载状态是位掩码,因此使用按位运算符:

if (moviePlayer.loadState & MPMovieLoadStatePlayable)
    // get the thumbnail

不是

if (moviePlayer.loadState == MPMovieLoadStatePlayable)
    // get the thumbnail

或者您可以使用

- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option

方法,当缩略图准备就绪时,您将收到MPMoviePlayerThumbnailImageRequestDidFinishNotification通知。