首先,获取到视频的截图
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *movieCell = [DataTable dequeueReusableCellWithIdentifier:@"movieCell"];
if (movieCell==nil) {
movieCell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"movieCell"]autorelease];
}
NSString *linkStr = [linkArray objectAtIndex:indexPath.row];
movieCell.textLabel.text = linkStr;
MPMoviePlayerController *movieController = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:linkStr]];
[movieController requestThumbnailImagesAtTimes:[NSArray arrayWithObject:[NSNumber numberWithDouble:0]] timeOption:MPMovieTimeOptionNearestKeyFrame];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(movieThumbnailLoadComplete:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:movieController];
return movieCell;
}
-(void)movieThumbnailLoadComplete:(NSNotification*)notification{
NSDictionary *userInfo = [notification userInfo];
NSLog(@"userInfo:%@",userInfo);
NSNumber *timecode =
[userInfo objectForKey: @"MPMoviePlayerThumbnailTimeKey"];
UIImage *image =
[userInfo objectForKey: @"MPMoviePlayerThumbnailImageKey"];
}
点击后,启动播放
-(void)initPlayer{
//显示loadingView
[loadingView show:YES];
//使用playerItem获取视频的信息,当前播放时间,总时间等
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:movieURL];
//player是视频播放的控制器,可以用来快进播放,暂停等
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
[moviePlayeView setPlayer:player];
[moviePlayeView.player play];
//计算视频总时间
CMTime totalTime = playerItem.duration;
//因为slider的值是小数,要转成float,当前时间和总时间相除才能得到小数,因为5/10=0
totalMovieDuration = (CGFloat)totalTime.value/totalTime.timescale;
//NSLog(@"totalMovieDuration:%f",totalMovieDuration);
//在totalTimeLabel上显示总时间
totalTimeLabel.text = [self convertMovieTimeToText:totalMovieDuration];
//检测视频加载状态,加载完成隐藏loadingView
[moviePlayeView.player.currentItem addObserver:self
forKeyPath:@"status"
options:NSKeyValueObservingOptionNew
context:nil];
//添加视频播放完成的notifation
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:moviePlayeView.player.currentItem];
}
上述代码来源:https://github.com/yuyi012/VideoStreamDemo2