【问题标题】:Looping a Video in AVFoundation AVSampleBufferDisplayLayer在 AVFoundation AVSampleBufferDisplayLayer 中循环播放视频
【发布时间】:2015-02-19 20:50:29
【问题描述】:

我正在尝试在 AVSampleBufferDisplayLayer 上循环播放视频。我可以让它玩一次没有问题。但是,当我尝试循环播放时,它不会继续播放。

根据AVFoundation to reproduce a video loop 的答案,没有办法倒带 AVAssetReader,所以我重新创建它。 (我确实看到了Looping a video with AVFoundation AVPlayer? 的答案,但 AVPlayer 功能更全面。我正在阅读文件,但仍然想要 AVSampleBufferDisplayLayer。)

一个假设是我需要停止一些 H264 标头,但我不知道这是否会有所帮助(以及如何)。另一个是它与CMTimebase有关,但我尝试了几件事无济于事。

以下代码,基于 Apple 关于直接访问视频编码的 WWDC 演讲:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"sample-mp4" ofType:@"mp4"];
    NSURL *fileURL = [NSURL fileURLWithPath:filepath];
    AVAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];

    UIView *view = self.view;

    self.videoLayer = [[AVSampleBufferDisplayLayer alloc] init];
    self.videoLayer.bounds = view.bounds;
    self.videoLayer.position = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));
    self.videoLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    self.videoLayer.backgroundColor = [[UIColor greenColor] CGColor];

    CMTimebaseRef controlTimebase;
    CMTimebaseCreateWithMasterClock( CFAllocatorGetDefault(), CMClockGetHostTimeClock(), &controlTimebase );

    self.videoLayer.controlTimebase = controlTimebase;
    CMTimebaseSetTime(self.videoLayer.controlTimebase, CMTimeMake(5, 1));
    CMTimebaseSetRate(self.videoLayer.controlTimebase, 1.0);

    [[view layer] addSublayer:_videoLayer];

    dispatch_queue_t assetQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //??? right queue?


    __block AVAssetReader *assetReaderVideo = [self createAssetReader:asset];
    __block AVAssetReaderTrackOutput *outVideo = [assetReaderVideo outputs][0];
    if( [assetReaderVideo startReading] )
    {
        [_videoLayer requestMediaDataWhenReadyOnQueue: assetQueue usingBlock: ^{
            while( [_videoLayer isReadyForMoreMediaData] )
            {
                CMSampleBufferRef sampleVideo;
                if ( ([assetReaderVideo status] == AVAssetReaderStatusReading) && ( sampleVideo = [outVideo copyNextSampleBuffer]) ) {
                    [_videoLayer enqueueSampleBuffer:sampleVideo];
                    CFRelease(sampleVideo);
                    CMTimeShow(CMTimebaseGetTime(_videoLayer.controlTimebase));
                }
                else {

                    [_videoLayer stopRequestingMediaData];
                    //CMTimebaseSetTime(_videoLayer.controlTimebase, CMTimeMake(5, 1));
                    //CMTimebaseSetRate(self.videoLayer.controlTimebase, 1.0);
                    //CMTimeShow(CMTimebaseGetTime(_videoLayer.controlTimebase));
                    assetReaderVideo = [self createAssetReader:asset];
                    outVideo = [assetReaderVideo outputs][0];
                    [assetReaderVideo startReading];
                    //sampleVideo = [outVideo copyNextSampleBuffer];

                    //[_videoLayer enqueueSampleBuffer:sampleVideo];
                }
            }
        }];
    }
}

-(AVAssetReader *)createAssetReader:(AVAsset*)asset {
    NSError *error=nil;

    AVAssetReader *assetReaderVideo = [[AVAssetReader alloc] initWithAsset:asset error:&error];

    NSArray *videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
    AVAssetReaderTrackOutput *outVideo = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTracks[0] outputSettings:nil]; //dic];
    [outVideo res]

    [assetReaderVideo addOutput:outVideo];
    return assetReaderVideo;
}

非常感谢。

【问题讨论】:

  • 我今天遇到了同样的问题。我通过执行以下操作找到了部分解决方案:每个循环调用 [self.videoLayer flushAndRemoveImage] 并为每个循环递增 itemTime.epoch。它在大部分时间都有效,但并非总是如此。
  • 调用 enqueueSampleBuffer: 在主调度队列上似乎已经为我可靠地修复了它。
  • 这里是一个工作解决方案的源代码,它使用硬件编码和解码 API 在没有 AVAssetReader 的情况下实现无缝循环工作。 stackoverflow.com/questions/33285826/…

标签: ios objective-c video avfoundation avplayer


【解决方案1】:

尝试使用 swift 创建一个循环,然后将 Objective-c 文件与 swift 文件连接起来。 google 有很多关于桥接和循环的答案,所以只要用 swift google 一下。

【讨论】:

  • 谢谢 nachshon——我也看不出在 Swift 中我会调用什么来让它循环。有什么想法吗?
猜你喜欢
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 2015-04-23
  • 1970-01-01
相关资源
最近更新 更多