【发布时间】:2015-08-06 19:00:04
【问题描述】:
我正在尝试从 ios 中的 url 播放视频。所以基本上我在主视图中添加了一个 MPMoviePlayerController 视图。所以我想要加载视图控制器我应该能够看到带有视频缩略图的 VideoView &正如我们在 YouTube 应用中看到的那样,中间的播放按钮图像。我知道如何获取视频缩略图,但有什么功能可以让我在中间获取播放按钮图像作为缩略图的一部分。
添加VideoView的代码:
NSURL *fileURL = [NSURL URLWithString:@"http://www.w3schools.com/html/movie.mp4"];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
CGRect movieFrame;
movieFrame.size = self.videoView.frame.size;
[self.moviePlayerController.view setFrame:movieFrame];
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone];
[self.moviePlayerController.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.videoView addSubview:self.moviePlayerController.view];
[self.videoView bringSubviewToFront:self.moviePlayerController.view];
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:self.moviePlayerController.view
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:self.moviePlayerController.view
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0]];
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:self.moviePlayerController.view
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0]];
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:self.moviePlayerController.view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
UIImage *thumbnail = [self.moviePlayerController thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
[self.moviePlayerController play];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fullVideoScreen:)];
singleFingerTap.numberOfTapsRequired = 2;
[self.moviePlayerController.view addGestureRecognizer:singleFingerTap];
获取缩略图的代码
NSURL *fileURL = [NSURL URLWithString:@"http://www.w3schools.com/html/movie.mp4"];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc]
initWithContentURL:fileURL];
mp.shouldAutoplay = NO;
mp.initialPlaybackTime = 0;
mp.currentPlaybackTime = 05;
// get the thumbnail
UIImage *thumbnail = [mp thumbnailImageAtTime:2
timeOption:MPMovieTimeOptionNearestKeyFrame];
self.videoView.backgroundColor=[UIColor colorWithPatternImage:thumbnail];
【问题讨论】:
标签: ios objective-c iphone ipad video