iPhone上实现流媒体播放器

      首先,获取到视频的截图

- (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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值