使用 SimpleAudioEngine 实现大量音效

发布于 2024-12-09 22:26:52 字数 309 浏览 0 评论 0原文

背景: 我的背景音乐作为音效播放,因为我想改变每次重复曲调之间的音高。

问题: 当背景音乐播放时,还会发生许多其他短音效。播放一定数量的音效后,我的背景音乐(也是音效)就会停止。队列似乎是循环的,因此在播放完 50 个音效后,当播放第 51 个音效时,无论是否完成,第一个音效都会被释放。

请求指导: 我认为解决这个问题可能有两个方向。 1.不播放背景音乐作为效果并弄清楚如何改变音调作为背景音乐而不是效果 2. 如何确保效果将保留到完成为止。

谢谢

Background:
My background music is being played as a sound effect because I want to change the pitch between each repetition of the tune.

The Issue:
While the background music is playing there's a lot of other short sound effects happening. After a certain number of sound effects play, my background music (also a sound effect) cuts out. The queue seems to cycle so that after say 50 sound effects have played, when the 51st plays, the 1st will be released whether it has completed or not.

Request for direction:
There are two directions I can see possibly going with this issue.
1. Not play the background music as an effect and figure out how to change the pitch as background music instead of an effect
2. Some how make sure that the effect will be retained until it has completed.

Thanks

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

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

发布评论

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

评论(1

む无字情书 2024-12-16 22:26:52

SimpleAudioEngine 不允许您控制通道,因此它只会在其内部分配的通道之一中播放音效。最终所有频道都在播放音频。在这种情况下,当要播放新的音频文件并且所有通道都已在播放时,SimpleAudioEngine 将取消现有的音频文件之一。有时这将是您的背景音乐。当当。

解决此问题通常要做的就是专门为背景音乐分配一个音频通道。没有其他音频会尝试在该通道上播放音频。您必须使用“常规”CocosDenshion API。看来您需要使用 CDAudioManager 或 <一个href="http://learn-cocos2d.com/api-ref/latest/CocosDenshion/html/interface_c_d_sound_engine.html" rel="nofollow">CDSoundEngine,但我没有使用 CocosDenshion 通道的经验。

就我个人而言,我从未使用过 CocosDenshion,每当我需要更多控制时,我都会找到 ObjectAL API 更容易理解,并且有优秀的文档。我为 ObjectAL 制作了以下辅助函数,用于在特定通道上播放声音。我只是为我的背景音乐实例化了一个 ALChannelSource ,并且只通过该频道播放背景音乐。返回的 ALSoundSource 甚至允许您更改音高、平移等,同时播放音频。

+(id<ALSoundSource>) playEffect:(NSString*)effect channel:(ALChannelSource*)channel loop:(bool)loop
{
    id<ALSoundSource> soundSource = nil;

    if (channel)
    {
        ALBuffer* buffer = [[OALAudioSupport sharedInstance] bufferFromFile:effect];
        soundSource = [channel play:buffer loop:loop];
    }

    return soundSource;
}

我通过 OALSimpleAudio 播放的所有其他音频文件班级。

The SimpleAudioEngine doesn't allow you to control channels, so it'll just play your sound effects in one of the channels it has allocated internally. Eventually all channels are playing audio. In that case, when a new audio file is to be played and all channels are already playing, SimpleAudioEngine will cancel one of the existing audio files. On occasion this will be your background music. Dang.

What you normally do to fix this is to assign (allocate) one audio channel specifically for the background music. No other audio will ever attempt to play audio on that channel. You will have to use the "regular" CocosDenshion API. It looks like you either need to use CDAudioManager or CDSoundEngine, but I have no experience with using channels with CocosDenshion.

Personally I never used CocosDenshion, whenever I needed more control I found the ObjectAL API to be easier to understand and it has excellent documentation. I made the following helper function for ObjectAL that plays a sound on a particular channel. I simply instantiated a ALChannelSource for my background music, and only played background music through that channel. The returned ALSoundSource even allows you to change pitch, pan, etc. while the audio is playing.

+(id<ALSoundSource>) playEffect:(NSString*)effect channel:(ALChannelSource*)channel loop:(bool)loop
{
    id<ALSoundSource> soundSource = nil;

    if (channel)
    {
        ALBuffer* buffer = [[OALAudioSupport sharedInstance] bufferFromFile:effect];
        soundSource = [channel play:buffer loop:loop];
    }

    return soundSource;
}

All other audio files I'm playing through the OALSimpleAudio class.

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