我正试图在ios中播放来自url的视频。所以基本上我在主视图中添加MPMoviePlayerController
视图。所以我想要加载viewcontroller我应该能够看到带有缩略图的VideoView视频&我们在YouTube应用中看到的中心播放按钮图片。我知道如何获得视频thubnail,但有任何功能,我可以在中心获取播放按钮图像作为缩略图的一部分。
添加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];
答案 0 :(得分:0)
当您将控制sytle设置为MPMovieControlStyleNone时,播放器视图不会显示任何控件UI。您可能需要根据需要构建自己的控件UI。希望这有用。
答案 1 :(得分:0)
关注获取视频的代码。
NSString *str = [[self.videoArray objectAtIndex:i] valueForKey:@"vName"];
NSURL *videoURL = [NSURL URLWithString:str] ;
MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] initWithContentURL:videoURL]autorelease];
UIImage *thumbnail = [player thumbnailImageAtTime:0.41 timeOption:MPMovieTimeOptionNearestKeyFrame];
player = nil;
对于“播放”按钮,您需要在uiview上添加自定义。
愿这对你有所帮助。
答案 2 :(得分:0)
将视频控制样式用于none:
myPlayer.view.frame = videoView.bounds;
myPlayer.controlStyle = MPMovieControlStyleNone;
现在您可以将UIButton添加到中心视频视图框并自行控制视频。
[videoControllButton setBackgroundImage:[UIImage imageNamed:@"pauseButton.png"] forState:UIControlStateNormal];
[videoControllButton addTarget:self action:@selector(playPauseClick:) forControlEvents:UIControlEventTouchUpInside];
这是来自我的旧应用程序的代码,解决了这个问题:
init标签到视频播放器
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onPlayerTapped:)];
singleFingerTap.numberOfTapsRequired = 1;
singleFingerTap.delegate = self;
[myPlayer.view addGestureRecognizer:singleFingerTap];
并从按钮控制点击:
bool videoIsPlaying = NO;
-(IBAction)playFromIcon:(id)sender{
videoIsPlaying = TRUE;
[videoControllButton setBackgroundImage:[UIImage imageNamed:@"pauseButton"] forState:UIControlStateNormal];
[myPlayer play];
playIcon.hidden = YES;
}
点击播放器
-(void) onPlayerTapped:(UIGestureRecognizer *)gestureRecognizer {
if (videoIsPlaying == TRUE) {
videoIsPlaying = FALSE;
[videoControllButton setBackgroundImage:[UIImage imageNamed:@"playButton"] forState:UIControlStateNormal];
[myPlayer pause];
playIcon.hidden = NO;
}else{
videoIsPlaying = TRUE;
[videoControllButton setBackgroundImage:[UIImage imageNamed:@"pauseButton"] forState:UIControlStateNormal];
[myPlayer play];
playIcon.hidden = YES;
}
}
或点按按钮播放暂停...
-(IBAction)playPauseClick:(id)sender{
if (videoIsPlaying == TRUE) {
videoIsPlaying = FALSE;
[videoControllButton setBackgroundImage:[UIImage imageNamed:@"playButton.png"] forState:UIControlStateNormal];
[myPlayer pause];
playIcon.hidden = NO;
}else{
videoIsPlaying = TRUE;
[videoControllButton setBackgroundImage:[UIImage imageNamed:@"pauseButton.png"] forState:UIControlStateNormal];
[myPlayer play];
playIcon.hidden = YES;
}
}