当前位置:   article > 正文

AVPlayer基本使用方法_avplayer *newplayer = [[avplayer alloc] initwithur

avplayer *newplayer = [[avplayer alloc] initwithurl:[nsurl urlwithstring

AVPlayer同样适用与本地和远程媒体文件。可以通过AVPlayerLayer来显示。

AVURLAsset *avasset;

AVPlayerItem *item;

AVPlayer *player;

AVPlayerLayer *playerLayer;

CMTime totalTime;

CGFloat totalMovieDuration;


  1. - (void)setupPlaybackForURL:(NSURL *)url
  2. {
  3. NSURL *url = [NSURL URLWithString:[NSString stringWithUTF8String:url]];
  4. avasset = [[AVURLAsset alloc] initWithURL:url options:nil];
  5. //avasset = [AVURLAsset URLAssetWithURL:url options:nil];
  6. item = [[AVPlayerItem alloc] initWithAsset:avasset];
  7. //item = [AVPlayerItem playerItemWithAsset:avasset];
  8. if(player.currentItem)
  9. [player replaceCurrentItemWithPlayerItem:item];
  10. else
  11. player = [[AVPlayer alloc] initWithPlayerItem:item];
  12. //player = [AVPlayer playerWithPlayerItem:item];
  13. playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
  14. totalTime = item.duration;
  15. totalMovieDuration = (CGFloat)totalTime.value/totalTime.timescale;
  16. playerLayer.frame = CGRectMake(0, 0, 100, 70);
  17. playerLayer.backgroundColor = [UIColor blackColor].CGColor;
  18. playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
  19. //playerLayer.shadowOffset = CGSizeMake(0, 3);
  20. //playerLayer.shadowRadius = 5.0;
  21. //playerLayer.shadowColor = [UIColor blackColor].CGColor;
  22. //playerLayer.shadowOpacity = 0.8;
  23. //[uiView.layer addSublayer:playerLayer];
  24. playerLayer.hidden = NO;
  25. NSString *tracksKey = @"tracks";
  26. [avasset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:
  27. ^{
  28. dispatch_async(dispatch_get_main_queue(),
  29. ^{
  30. NSError *error = nil;
  31. AVKeyValueStatus status = [avasset statusOfValueForKey:tracksKey error:&error];
  32. if (status == AVKeyValueStatusLoaded) {
  33. [player pause];
  34. }
  35. else {
  36. // You should deal with the error appropriately.
  37. NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
  38. }
  39. });
  40. }];
  41. }


判断播放暂停状态

没有专门的pause或者playing状态,可以利用rate属性:

rate的值为0.0表示暂停视频,而1.0的值是在当前项目的自然速率播放。

  1. if(player.rate == 1.0)
  2. {
  3. playingStatue = YES;
  4. }
  5. else
  6. {
  7. playingStatue = NO;
  8. }




本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号