声音在 iPhone 模拟器中通过断点播放,但在没有断点的情况下无法播放

发布于 2024-08-19 10:17:56 字数 1050 浏览 6 评论 0原文

我正在尝试在 iPhone 模拟器中播放声音(使用 3.1 SDK)。当我添加断点并单步执行 GDB 中的代码时,就会播放声音。但是,如果我禁用断点,则不会播放声音。

调用声音的代码是:

SoundEffect *completeSound = [myobj completeSound];
if (completeSound != nil) {
    [completeSound play];
}

SoundEffect 类有一个简单的播放方法:

// Plays the sound associated with a sound effect object.
-(void)play {
    // Calls Core Audio to play the sound for the specified sound ID.
    AudioServicesPlaySystemSound(_soundID);
}

如果我在“myobjcompleteSound”方法上方启用断点,它就会完美播放。但是,如果没有断点,则不会发出声音。

音频文件是一个 CAF 文件,长度为 5 秒。

调用 AudioServicesPlaySystemSound 后是否需要添加一些延迟以确保其播放?

更新:看来使用 AudioServicesAddSystemSoundCompletion 方法是解决方案。

SoundEffect *completeSound = [myobj completeSound];
if (completeSound != nil) {
    AudioServicesAddSystemSoundCompletion([completeSound _soundID], 
        NULL, NULL, AudioPlaybackComplete, self);
    [completeSound play];
} 

然后,当音频剪辑完成时,将调用 AudioPlaybackComplete 静态函数。这工作正常 - 尽管我仍然不明白为什么音频剪辑在初始用例中失败。

I am trying to get a sound to play in the iPhone Simulator (using 3.1 SDK). When I add a breakpoint, and step through the code in GDB, the sound plays. However, if I disable the breakpoint, the sound does not play.

The code to invoke the sound is:

SoundEffect *completeSound = [myobj completeSound];
if (completeSound != nil) {
    [completeSound play];
}

The SoundEffect class has a simple play method:

// Plays the sound associated with a sound effect object.
-(void)play {
    // Calls Core Audio to play the sound for the specified sound ID.
    AudioServicesPlaySystemSound(_soundID);
}

If I enable a breakpoint above the "myobj completeSound" method, it plays perfectly. However, if there's no breakpoint, no sound is emitted.

The audio file is a CAF file that's 5 seconds in length.

Is there some delay that I need to add after invoking AudioServicesPlaySystemSound to ensure that it plays?

UPDATE: it appears that using the AudioServicesAddSystemSoundCompletion method is the solution.

SoundEffect *completeSound = [myobj completeSound];
if (completeSound != nil) {
    AudioServicesAddSystemSoundCompletion([completeSound _soundID], 
        NULL, NULL, AudioPlaybackComplete, self);
    [completeSound play];
} 

Then the AudioPlaybackComplete static function is invoked when the audio clip is done. This works properly - although I still do not understand why the audio clip fails in the initial use case.

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

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

发布评论

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

评论(1

手心的海 2024-08-26 10:17:56

你检查过你的程序流程吗?因为它让我相信,如果没有断点,您的程序会重复触发 play 消息,并以如此高的频率多次重新启动声音播放,您永远听不到它的任何样本。通过断点,您可以阻止代码中断播放。

Have you checked your program flow? Because it leads me to believe that without the breakpoint your program repeatedly fires the play message and restarts the playback of the sound many times with so high frequency you never get to hear any samples of it. With the breakpoint, you halt your code from disrupting the playback.

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