音频队列服务和 iPhone 系统声音的计时

发布于 2025-01-08 08:33:44 字数 1479 浏览 0 评论 0原文

我正在使用音频队列服务分析 iPhone/iPod Touch 上的传入音频。分析仪的预期行为归结为:当应用程序激活时,开始分析;当App被发送到后台时,停止分析。

我使用直接的方法,只要应用程序状态发生变化,就使用 AppDelegate 启动和停止分析器。

这是一些代码:

- (void)applicationWillResignActive:(UIApplication *)application {

    AudioQueueFlush(_aqRef);
    AudioQueueStop(_aqRef, true);
    AudioQueueDispose(_aqRef, true);
}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    AudioQueueNewInput(&_formatDescription, _renderCallback, NULL, NULL, NULL, 0, &_aqRef);
    AudioQueueAllocateBuffer(_aqRef, _aqBufferSize, &_aqBufferRef);
    AudioQueueEnqueueBuffer(_aqRef, _aqBufferRef, 0, NULL);
    AudioQueueStart(_aqRef, NULL);
}

所有变量都已初始化并且正在工作 - 将其视为给定并经过测试。我知道,队列和缓冲区不应该每次都被处理和分配,但目前没有什么区别,而且这样更完整。

发生的情况如下:

步骤 1) 启动应用程序,音频队列启动,鲍勃是你的叔叔。

步骤 2) 按下电源按钮,应用程序将暂停。但您听不到锁定声音,因为音频队列似乎仍然阻止系统声音。然而,有时它确实会出现,大多是在应用程序首次启动后,但这并不可靠。第一个故障。

步骤 3) 按主页按钮并解锁手机。同样,您不会听到解锁手机的系统声音,因为音频队列已经接管。不过,您可能会听到一小段解锁声音。有时。也不靠谱。第二个故障。

步骤 4) 按主页按钮并将应用程序发送到后台。应用程序消失后大约一秒钟,系统会记得它无法播放步骤 3 中的解锁声音,并立即播放。全音量。那真的很糟糕。

这让我想到两个问题:

问题 1):当按下电源按钮时,我没有看到比 applicationWillResignActive: 更早停止音频队列的任何其他方法。我在这里缺少什么吗?我对缺少锁定声音感到满意,但我很高兴了解任何其他使其听起来(或不听起来)可靠的方法。

问题2):第4步中延迟的解锁声音让我很头疼。防止这种情况的一个简单方法是延迟音频队列的启动,以便可以不受阻碍地播放解锁声音。但这似乎是一种黑客行为,如果有人能给我指出更好的方向,我会成为一个更快乐的程序员。

谢谢/安德烈亚斯

I am analyzing incoming audio on an iPhone/iPod Touch using the Audio Queue Services. The desired behavior of the analyzer boils down to this: when the App becomes active, start analyzing; when the App is sent to the background, stop analyzing.

I am using a straight forward approach, using the AppDelegate to start and stop the analyzer whenever the App state changes.

Here's some code:

- (void)applicationWillResignActive:(UIApplication *)application {

    AudioQueueFlush(_aqRef);
    AudioQueueStop(_aqRef, true);
    AudioQueueDispose(_aqRef, true);
}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    AudioQueueNewInput(&_formatDescription, _renderCallback, NULL, NULL, NULL, 0, &_aqRef);
    AudioQueueAllocateBuffer(_aqRef, _aqBufferSize, &_aqBufferRef);
    AudioQueueEnqueueBuffer(_aqRef, _aqBufferRef, 0, NULL);
    AudioQueueStart(_aqRef, NULL);
}

All variable are initialized and are working – take that as given and tested. The queue and the buffers shouldn't be disposed and allocated each time, I know, but it doesn't make a difference for now and it's more complete this way.

Here is what happens:

Step 1) Launch the App, the audio queue starts, Bob's your uncle.

Step 2) Push the power button and the App is suspended. You don't hear the locking-sound though, because the audio queue still seems to block the system sounds. However, sometimes it does appear, mostly after the first launch of the App, but that is not reliable. First glitch.

Step 3) Push the home button and unlock the phone. Again, you won't hear the system sound of unlocking the phone because the audio queue has already taken over. You may hear a short bit of the unlocking-sound, though. Sometimes. Not reliable either. Second glitch.

Step 4) Push the home button and send the App into the background. About a second after the App has disappeared, the system remembers that it couldn't play the unlocking sound in Step 3 and plays it NOW. At full volume. That's really bad.

This leads me to two questions:

Question 1): When the power button gets pushed, I don't see any other way to stop the audio queue any earlier than in applicationWillResignActive:. Anything I am missing here? I am ok with the locking-sound missing, but I am happy to learn about any other way to make it sound (or not sound) reliably.

Question 2): The delayed unlocking sound in Step 4 is giving me a headache. An easy way to prevent this, would be to delay the start of the audio queue so the unlocking sound can be played unhindered. But that seems like a hack and if anybody can point me in a better direction I would be a much more happy coder.

Thanks /Andreas

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文