在 iPhone 上实现 OpenAL 时声音播放延迟(打嗝)

发布于 2024-10-04 05:17:50 字数 2092 浏览 3 评论 0原文

我在我的 iPhone 游戏中实现了 OpenAL 代码。当我开始游戏时,它运行 1 秒,停顿 2 秒,然后恢复(打嗝效果)。我相信它是由于声音文件加载而延迟的。解决办法是什么?谁能推荐任何书籍、网站或源代码(请不是 iPhone 参考)?是否有加载过程以及应该在哪里初始化加载过程?那会有帮助吗?

下面,我包含了我已实现的 OpenAL 代码的相关组件。声音文件将由游戏循环中的“if”语句播放和调用。 OpenALController 类用于创建声源和缓冲区,并且在 OpenALController 中调用 InitOpenAL 方法。 MyView是UIView的自定义子类,并连接到主视图(我没有使用默认视图)。

// MyView.m // 自定义的 UIView 作为主视图。
#import“OpenALSoundController.h”
- (void)开始播放{
...
[自初始化值];
...
[自初始化定时器];
}
- (void)初始化定时器{ if (theTimer == nil) {
theTimer = [CADisplayLink displayLinkWithTarget:self 选择器:@selector)gameLoop)];
theTimer.frameInterval = 2;
[theTimer addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
}
}
- (void)gameLoop { ... 如果某事= true // 播放声音 [[OpenALSoundController 共享声音控制器] playSound1];
...
}
...
@结束


// OpenALSoundController.h @interface OpenALSoundController : NSObject {
...}
...
+ (OpenALSoundController*) 共享声音控制器
...
@结束


// OpenALSoundController.m
// 单例访问器
{
静态 OpenALSoundController* share_sound_controller;
@同步(自我)
{
if(nil ==共享声音控制器)
{ Shared_sound_controller = [[OpenALSoundController alloc] init];
}
返回共享声音控制器;
}
返回共享声音控制器;
}
- (void)initOpenAL{
...
file_url = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"fire" ofType:@"wav"]];
firePcmData = MyGetOpenALAudioDataAll((CFURLRef)file_url, &data_size, &al_format,&sample_rate);
alBufferData(fireOutputBuffer,al_format,firePcmData,data_size,sample_rate);
[file_url 发布];
...
alSourcei(outputSourceFire, AL_BUFFER, fireOutputBuffer);
...
}

I implemented OpenAL code to my iphone game. When I starts the game, it runs for 1 sec and stalls for 2 sec then resumes (hiccup effect). I believe its delayed due to the sound files loading. What is the solution? Can anyone recommend any book, site or sources code (not the iphone reference, please)? Is there a loading process and where should I initialize the loading process? Would that help?

Below, I have included the related components of the OpenAL code that I have implemented. The sound file will be played and invoked by a "if" statement in the gameloop. The OpenALController class is for the sound sources and buffers creation and the InitOpenAL method is invoked in OpenALController. MyView is a customized subclass of UIView and connected to the main view (I didn't use the default view).

// MyView.m
// A customized UIView as the main view.
#import "OpenALSoundController.h"
- (void)startPlaying{
...
[self initializeValuables];
...
[self initializeTimer];
}
- (void)initializeTimer {
if (theTimer == nil) {
theTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector)gameLoop)];
theTimer.frameInterval = 2;
[theTimer addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
}
}
- (void)gameLoop {
...
If something = true
// Play the sound
[[OpenALSoundController sharedSoundController] playSound1];
...
}
...
@end


// OpenALSoundController.h
@interface OpenALSoundController : NSObject {
...}
...
+ (OpenALSoundController*) sharedSoundController
...
@end


// OpenALSoundController.m
// Singleton accessor
{
static OpenALSoundController* shared_sound_controller;
@synchronized(self)
{
if(nil == shared_sound_controller)
{
shared_sound_controller = [[OpenALSoundController alloc] init];
}
return shared_sound_controller;
}
return shared_sound_controller;
}
- (void) initOpenAL{
...
file_url = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"fire" ofType:@"wav"]];
firePcmData = MyGetOpenALAudioDataAll((CFURLRef)file_url, &data_size, &al_format,&sample_rate);
alBufferData(fireOutputBuffer, al_format, firePcmData, data_size, sample_rate);
[file_url release];
...
alSourcei(outputSourceFire, AL_BUFFER, fireOutputBuffer);
...
}

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

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

发布评论

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

评论(2

铜锣湾横着走 2024-10-11 05:17:51

您可能对 Finch 感兴趣,这是一个适用于 iOS 的 OpenAL 声音引擎。它非常适合游戏。重用一些已经存在的代码通常比开发和维护自己的代码更好。

You might be interested in Finch, an OpenAL sound engine for iOS. It’s very well suited to games. It’s usually better to reuse some already existing code than develop and maintain your own.

前事休说 2024-10-11 05:17:51

首先最好使用 mp3,因为 wav 文件很大并且从磁盘加载需要时间。 Mp3 文件在磁​​盘上较小,可以加载到内存中并在内存中解压缩以便播放。也尝试降低 mp3 比特率/编码质量进行试验。

此外,您还需要预加载声音以避免卡顿,否则第一次播放声音时会出现延迟。

First its better to use mp3, as wav files are huge and loading from disk takes time. Mp3 files are smaller on disk, loaded into memory and decompressed there for playing. Try experimenting by reducing mp3 bitrate/encoding quality too.

Also you need to preload sounds to avoid hiccups, otherwise you will have a delay the first time a sound is played.

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