iphone dev从MPMoviePlayerController获取视频缩略图(帧)

时间:2012-03-01 09:07:48

标签: iphone

我试图从视频网址获取视频帧或缩略图,但没有成功,这是我的代码

- (void)viewDidLoad {
    thumbnailimg = [[UIImageView alloc]init];
    movie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=ec9KXrpYvzk"]];
    //UIImage *singleFrameImage = [movie thumbnailImageAtTime:10 
                                                 //timeOption:MPMovieTimeOptionExact];
    //thumbnailimg.image = singleFrameImage;

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(handleThumbnailImageRequestFinishNotification:) 
               name:MPMoviePlayerThumbnailImageRequestDidFinishNotification
             object:movie];
    NSNumber * time =[NSNumber numberWithInt:10];


    NSArray *times = [NSArray arrayWithObjects:time,nil];
    [movie requestThumbnailImagesAtTimes:times timeOption:MPMovieTimeOptionExact];



    [super viewDidLoad];
}


-(void)handleThumbnailImageRequestFinishNotification:(NSNotification*)note
{

        NSDictionary *userinfo = [note userInfo];
        NSMutableDictionary *event = [NSMutableDictionary dictionary];
        NSError* value = [userinfo objectForKey:MPMoviePlayerThumbnailErrorKey];
        if (value!=nil)
        {

            [event setObject:[value description] forKey:@"error"];
        }
        else 
        {

            UIImage *image = [userinfo valueForKey:MPMoviePlayerThumbnailImageKey];
            thumbnailimg.image = image;

        }
        [event setObject:[userinfo valueForKey:MPMoviePlayerThumbnailTimeKey] forKey:@"time"];


}

即使handleThumbnailImageRequestFinishNotification也未触发,PLZ。告诉我我做错了什么以及如何纠正它,提前告诉我们,问候Saad

2 个答案:

答案 0 :(得分:1)

使用MPMoviePlayerController尝试使用其他视频(来自m3u8播放列表或MP4文件)无法从YouTube上播放视频。

答案 1 :(得分:0)

只想告诉时间应该以float形式提供,而不是int。 替换:

NSNumber * time =[NSNumber numberWithInt:10];

使用:

NSNumber * time =[NSNumber numberWithFloat:10.0f];

可能会解决问题。

类似的答案:https://stackoverflow.com/a/17870972