【问题标题】:How to get a notification when frames are being played of a video file, using iOS AVFoundation?如何在使用 iOS AVFoundation 播放视频文件的帧时获得通知?
【发布时间】:2013-10-08 06:27:27
【问题描述】:

我正在尝试处理从文件(不是相机流)加载的视频帧。

为此,我已经加载了视频(目前是捆绑包中的一个测试 mp4,虽然我打算将它作为一个外部 url)。

我阅读了很多关于 AVFoundation 的文档和教程,并找到了许多不同的建议,但没有一个有效。我希望在帧更改时找到委托方法或通知,但我认为这些实际上并不存在(可能是因为它们处理帧流太慢)。

这是我目前所拥有的。每次从视频流渲染图像时,我都需要运行 [self doImageProcess:] 选择器。如何做到这一点?

(一旦正常工作,我将发布完全工作的代码)

#import <AVFoundation/AVFoundation.h>

@implementation VTViewController

#pragma mark - View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    // load mp4

    NSURL *theURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"]];

    AVAsset *avAsset = [AVAsset assetWithURL:theURL];
    AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc] initWithAsset:avAsset];

    avPlayer = [[AVPlayer alloc] initWithPlayerItem:avPlayerItem];


    CMTime interval = CMTimeMake(33, 1000);  // 30fps
    id playbackObserver = [avPlayer addPeriodicTimeObserverForInterval:interval queue:nil usingBlock: ^(CMTime time) {
        // get image
        [self doImageProcess:nil];
    }];

   img1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
   [self.view addSubview:img1];


    avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:avPlayer];
    [avPlayerLayer setFrame:CGRectMake(0, 0, 480, 320)];//self.view.frame];

    [self.view.layer addSublayer:avPlayerLayer];
    [avPlayer seekToTime:kCMTimeZero];
    [avPlayer play];

}


- (void)doImageProcess:(UIImage *)theImage {

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, 1.0, 1.0);
    [avPlayerLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [arrImages addObject:img];

    [img1 removeFromSuperview];
    img1 = nil;
    img1 = [[UIImageView alloc] initWithImage:img];
    [self.view addSubview:img1];


    if ([arrImages count] > 4)
        [arrImages removeObjectAtIndex:0];

}

【问题讨论】:

  • 你解决了吗?

标签: ios video delegates avfoundation


【解决方案1】:

AVPlayeraddPeriodicTimeObserverForInterval:queue:usingBlock: 方法可能会有所帮助。它可以在特定的播放时间间隔执行一段代码。

【讨论】:

  • 我已经做到了(见上面的编辑),但图像没有从图层中正确捕获。有什么建议吗?
  • 我建议将其作为一个新的、更具体的问题提出。你会得到更好的答案。
猜你喜欢
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
  • 2011-09-09
相关资源
最近更新 更多