【问题标题】:ios playing 3 videos one after anotherios一个接一个播放3个视频
【发布时间】:2015-05-04 07:56:07
【问题描述】:

我正在尝试在第一个视频结束后立即开始一个新视频。我尝试使用 AVQueuePlayer,但是当第一个视频结束和第二个视频开始时会有一些延迟。

我需要它是样本同步的。即,如果视频 1 的长度为 10.25 秒,那么当我播放 11.5 秒时,视频 2 的长度应为 1.25 秒。

我一直在尝试保留 3 个 AVPlayer 对象,我使用相同的主时钟预滚动每个玩家。然后我试图拦截AVPlayerItemDidPlayToEndTimeNotification,然后调用setRate:time:atHostTime: 开始下一个视频。但是我没有播放任何视频。

我使用以下代码预卷我的视频:

- (void) preRollPlayer: (AVPlayer*) pPlayer withMasterClock: (CMClockRef) masterClock atTime: (NSTimeInterval) time
{
    [pPlayer setRate: 0.0f];
    [pPlayer play];

    [pPlayer seekToTime: CMTimeMakeWithSeconds( time, 1000000 ) toleranceBefore: kCMTimeZero toleranceAfter: kCMTimeZero];
    [pPlayer setMasterClock: masterClock];

    __block volatile int32_t completed  = 0;
    [pPlayer prerollAtRate: 1.0f completionHandler: ^( BOOL finished )
        {
            OSAtomicIncrement32( &completed );
        }];

    while( completed == 0 )
    {
        [NSThread sleepForTimeInterval: 0.01];
    }
}


然后我按如下方式调用 preRoll:

if ( pAudioVideoEntry.beforeVideoReader )
{
    [self preRollPlayer: pAudioVideoEntry.beforeVideoReader.player  withMasterClock: hostClock atTime: currentTime];
}

{
    [self preRollPlayer: pAudioVideoEntry.videoReader.player        withMasterClock: hostClock atTime: currentTime];
}
if ( pAudioVideoEntry.afterVideoReader )
{
    [self preRollPlayer: pAudioVideoEntry.afterVideoReader.player   withMasterClock: hostClock atTime: currentTime];
}


我终于开始视频如下:

[pAudioVideoEntry.beforeVideoReader.player  setRate: 1.0f time: kCMTimeZero atHostTime: syncTime];
    pAudioVideoEntry.playingPlayer  = pAudioVideoEntry.beforeVideoReader.player;

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector( beforeVideoEnded: )
                                             name: AVPlayerItemDidPlayToEndTimeNotification
                                           object: pAudioVideoEntry.beforeVideoReader.playerItem];

奇怪的是我听不到音频,通知从未发送过,每次调用我的AVPlayerItem 的输出AVPlayerItemVideoOutput 的hasNewPixelBufferForItemTime 都返回错误。所以我猜视频还没有开始。任何想法为什么?

【问题讨论】:

    标签: ios objective-c video avplayer


    【解决方案1】:

    所以在我发布这篇文章大约一个小时后(我花了一天的大部分时间都在为它奋斗),我在这里找到了一个简单的解决方案:https://stackoverflow.com/a/11337690/131140

    使用AVMutableComposition我可以将每个视频按顺序添加到合成中,并且可以完美播放:

        // Fill in the assets that make up the composition
        CMTime time             = kCMTimeZero;
        for( AVAsset* pThisAsset in pAssets )
        {
            CMTimeRange timeRange   = CMTimeRangeMake( kCMTimeZero, pThisAsset.duration );
            [pComposition insertTimeRange: timeRange ofAsset: pThisAsset atTime: time error: nil];
    
            time        = CMTimeAdd( time, pThisAsset.duration );
        }
    

    我在播放的视频图像与音频不同步时遇到了一个小错误,但音频表现完美。也大大简化了我的代码!

    【讨论】:

    • 嗨,Goz,请你给我一个上面代码的例子。
    • @MaheshNarla:我已经回答了你的问题。希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多