最有可能准时:+timeWithTimeInterval 或 -performSelector:withObject:afterDelay:

发布于 2024-08-11 14:24:32 字数 455 浏览 2 评论 0原文

我想只播放很短的时间,比声音文件的持续时间短。因此,在我开始播放的同时,我想排队一个将停止声音的任务。

我看到两种用于排队停止方法的选择: NSTimer 和 PerformSelector:withObject:afterDelay:

有谁知道这两者中哪一个最有可能按时触发,或者具有更高的优先级?调用时不一定要达到毫秒精度,但精确到 0.1 秒就很好了。


附录:有谁知道我在哪里可以找到有关不同计时器和延迟任务优先级的文档?例如,以下排名如何:

  • NSTimer 任务
  • PerformSelector
  • 在 setNeedsDisplay 被调用后调用视图的 drawRect 在
  • setNeedsDisplay 被调用后调用图层的绘图例程
  • 任何其他延迟任务

尝试在以下位置执行此操作是否有用不同的线程?

I would like to play a sound for only a short span of time, shorter than the duration of the sound file. Therefore, at the same time as I start playing, I would like to queue up a task that will stop the sound.

I see two choices for queuing up the stop method: NSTimer and performSelector:withObject:afterDelay:

Does anyone know which of the two is most likely to trigger on time, or have a higher priority? It is not imperative that I get called with millisecond accuracy, but accuracy to 0.1 second would be great.


Addendum: Does anyone know where I could find documentation on the priority of different timer and delay tasks? For example, how would the below rank:

  • NSTimer tasks
  • performSelector
  • the calling of a view's drawRect after setNeedsDisplay has been called
  • the calling of a layer's drawing routines after setNeedsDisplay has been called
  • any other delayed tasks

And would it be useful to try to do this in a different thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热血少△年 2024-08-18 14:24:32

performSelector:withObject:afterDelay: 将创建一个计时器来知道何时触发,因此两种方法应该同样(不)可靠。如果您的代码在主线程上执行昂贵的计算并且不将控制权返回给运行循环,或者运行循环必须处理大量事件,则计时器将不会按时触发。

performSelector:withObject:afterDelay: will create a timer to know when to fire so both approaches should be equally (un-)reliable. If your code performs an expensive calculation on the main thread and doesn't return control to the run loop or if the run loop has to process lots of events, the timer won't fire on time.

一绘本一梦想 2024-08-18 14:24:32

有一个 NSTimer,但时间间隔比声音必须播放的时间间隔小得多。当您的计时器触发时,即声音播放时的几次,您将计算自启动以来已经过去了多少时间。当该时间超过您设置的余量时,您将停止声音。

Have a NSTimer, but with a much smaller time interval than the one that your sound has to play. When your timer fires, and that is a couple of times while your sound plays, you would count how much time has passed since you started it. When that time exceeds a margin set by you, you stop the sound.

飘逸的'云 2024-08-18 14:24:32

根据我的理解,它们应该是差不多的。然而,弄清楚这一点的最好方法是亲自测试。进行一系列测试并记录准确的时间,然后看看哪个更接近。

From my understanding, they should be pretty much the same. However, the best way to figure this out is to test it yourself. Run a bunch of tests and record the exact time, then see which is closer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文