菜鸟教程小白 发表于 2022-12-12 23:43:33

IOS addPeriodicTimeObserverForInterval 触发太多次


                                            <p><p>我注意到我的应用发生了一件奇怪的事情。
这是一个使用 AVFoundation 类的视频应用程序。</p>

<p>我需要在给定时间触发一些事件。
我放了一些代码然后我评论它:</p>

<pre><code>/* I prepare the movie clip */
AVMutableComposition *composition = [ init];
NSBundle *bundle = ;
NSDictionary *optionsDictionary = forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
NSString *path = ;
NSURL *videoUrl = ;
AVURLAsset* sourceAsset = ;
) ofAsset:sourceAsset atTime:currentTime error:NULL];
</code></pre>

<p>在我的 viewDidLoad 中,我准备了剪辑。我使用 AVUrlAsset 以便能够将选项字典与 AVURLAssetPreferPreciseDurationAndTimingKey 一起使用以进行更精确的使用。</p>

<pre><code>/* I create the player */
AVPlayer *mPlayer = ];
AVPlayerLayer *mPlayerLayer = ;
mPlayerLayer.frame = CGRectMake(0.00, 96.00, 1024.00, 576.00);
;
</code></pre>

<p>我使用我的 AVUrlasset 中的项目创建一个播放器,然后在我的 View 中创建一个布局</p>

<pre><code>/* I set the observer */
[mPlayer addPeriodicTimeObserverForInterval:CMTimeMake(5,25) queue:NULL usingBlock:^(CMTime time) {
   NSLog(@&#34;Event : value: %lld, timescale %d, seconds: %f&#34;,
         time.value, time.timescale,(float) time.value / time.timescale); }];
</code></pre>

<p>我设置了观察者,每 5/25 秒,0.2 秒(25 是电影的帧率)。
在我的 block 中,我现在只写日志。</p>

<pre><code>/* Play the movie */
;
</code></pre>

<p>最后我玩。</p>

<p>似乎一切正常,除了我的日志有误:</p>

<pre><code>2012-11-15 16:43:05.382 PerfectCircle Beta Evento : value: 0, timescale 1, seconds: 0.000000
2012-11-15 16:43:05.410 PerfectCircle Beta Evento : value: 0, timescale 1, seconds: 0.000000
2012-11-15 16:43:05.563 PerfectCircle Beta Evento : value: 0, timescale 1, seconds: 0.000000
2012-11-15 16:43:05.580 PerfectCircle Beta Evento : value: 0, timescale 1, seconds: 0.000000
2012-11-15 16:43:05.747 PerfectCircle Beta Evento : value: 5489807, timescale 1000000000, seconds: 0.005490
2012-11-15 16:43:05.751 PerfectCircle Beta Evento : value: 8949705, timescale 1000000000, seconds: 0.008950
2012-11-15 16:43:05.753 PerfectCircle Beta Evento : value: 10679967, timescale 1000000000, seconds: 0.010680
2012-11-15 16:43:05.990 PerfectCircle Beta Evento : value: 248121672, timescale 1000000000, seconds: 0.248122
2012-11-15 16:43:06.169 PerfectCircle Beta Evento : value: 426865945, timescale 1000000000, seconds: 0.426866
</code></pre>

<p>在随机触发数次后,它开始计数良好。但它在开始时触发事件的次数增加了 5/6 倍。我尝试了不同的电影和编解码器。
如果我提高费率(es: CMTimeMake(25,25) )没有任何改变。</p>

<p>我以这种方式开始使用 <code>addBoundaryTimeObserverForTimes</code> :</p>

<pre><code>NSArray *starts = ,nil];
;
</code></pre>

<p>但我也有同样的问题。但是在这里,如果我提高利率,我就不会再看到问题了(但这对我的目标不利)。
我的问题是我必须精确计算电影在一个精确时刻播放了多少次。我不能用 <code>if (currenttime==0.3)</code> 测试它,因为它不精确。</p>

<p>这是一个错误?我想念什么?你听说过类似的吗?</p>

<p>感谢您的帮助。
丹尼尔</p>

<p>更新:
这似乎是一个开始和结束的问题。</p>

<pre><code>2012-11-15 16:43:05.747 PerfectCircle Beta Evento : value: 0, timescale 1, seconds: 0.000000
2012-11-15 16:43:05.747 PerfectCircle Beta Evento : value: 5489807, timescale 1000000000, seconds: 0.005490
</code></pre>

<p>错误的日志与正确的日志有不同的时间尺度。在播放结束时也会发生同样的情况。似乎在开始和结束时它会执行计时器,但电影尚未加载或已经关闭。
我尝试在播放后放置观察者,但没有任何改变。</p>

<p>我还为我的 CMTimeMake 尝试了不同且更高的时间尺度......但没有效果</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我知道这个问题有点老了,但无论如何.......</p>

<p>第一件事:</p>

<p>如果您查看 <a href="https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVPlayer_Class/index.html#//apple_ref/occ/instm/AVPlayer/addPeriodicTimeObserverForInterval:queue:usingBlock:" rel="noreferrer noopener nofollow" title="documentation">documentation</a>您可能会看到以下语句。</p>

<blockquote>
<p>The block is invoked periodically at the interval specified,
interpreted according to the timeline of the current item. <strong>The block
is also invoked whenever time jumps and whenever playback starts or
stops</strong>. If the interval corresponds to a very short interval in real
time, the player may invoke the block less frequently than requested.
Even so, the player will invoke the block sufficiently often for the
client to update indications of the current time appropriately in its
end-user interface.</p>
</blockquote>

<p>这表明为什么在开始和结束播放时调用它。</p>

<p>关于函数被多次调用,我猜这是因为玩家正在遭受的内部状态变化。我检查了播放器的“rate”、“status”和“playerItem”属性,但似乎没有说明发生了什么。 </p>

<p>您可以做的一件事是仅在玩家真正玩游戏后才考虑事件。</p>

<p>在调用 play 方法之前添加以下代码。</p>

<pre><code>__block AVPlayer* blockPlayer = self.player;
__block typeof(self) blockSelf = self;

// Setup boundary time observer to trigger when audio really begins,
// specifically after 1/3 of a second playback
self.startObserver = [self.player addBoundaryTimeObserverForTimes:
      @[]
                           queue:NULL
                        usingBlock:^{                  
                        blockSelf.isPlaying = YES;   
                        // Remove the boundary time observer
                        ;
                        }];
</code></pre>

<p>现在在 <strong>addPeriodicTimeObserverForInterval</strong>block 上,您只需检查我们刚刚分配的变量。</p>

<pre><code>__block typeof(self) blockSelf = self;
[self addPeriodicTimeObserverForInterval:CMTimeMake(60, 1)
                                 queue:dispatch_get_main_queue()
                              usingBlock:^(CMTime time)
                                  {
                                    if (blockSelf.isPlaying) {
                                       ... do some stuff here
                                    }
                                  }];
</code></pre>

<p>这不是更清洁的解决方案,但对我来说效果很好。如果我找到更好的东西,我会来编辑。</p></p>
                                   
                                                <p style="font-size: 20px;">关于IOS addPeriodicTimeObserverForInterval 触发太多次,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/13401206/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/13401206/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: IOS addPeriodicTimeObserverForInterval 触发太多次