我可以在同一个游戏中使用 Mix_OpenAudio 和 SDL_OpenAudio 吗?

发布于 2024-10-10 00:54:09 字数 467 浏览 0 评论 0原文

我正在尝试使用 SDL 1.2 完成一个学校项目,但遇到了无法克服的障碍。

游戏有多个游戏状态模块,每个模块都有一个功能 int Run(SDL_Surface *screen);

在每个模块中,Run() 初始化音频,运行其循环,然后在返回之前关闭音频。

所有模块都使用 Mix_OpenAudio() 和 Mix_CloseAudio() 来初始化/取消初始化声音,并且可以很好地协同工作。

唯一的例外是电影电影模块,我在其中使用 SDL_FFMpeg 来显示电影。这个模块迫使我改用SDL_OpenAudio()

SDL_OpenAudio 第一次失败,并出现错误“音频设备已打开”,但第二次可以正常工作。

一旦我运行了电影模块,其他模块中的所有声音都会停止工作,即使它们的所有函数都返回 0 表示成功。

有人知道发生了什么事吗?

I am trying to finish up a school project using SDL 1.2, but I have hit a snag that I can't get by.

The game has several game state modules, each with a function
int Run(SDL_Surface *screen);

In each module, Run() initializes audio, runs through its loop, then closes audio before returning.

all of the modules use Mix_OpenAudio() and Mix_CloseAudio() to init/deinit the sound, and work fine together.

The only exception is the cinematic movie module where I have used SDL_FFMpeg to show a movie. This module forces me to use SDL_OpenAudio() instead.

SDL_OpenAudio fails the first time with the error "Audio device is already open", but then works the second time.

Once I have run the movie module, all the sound in the other modules stops working even though all their functions return 0 for success.

anyone have any idea whats going on?

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

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

发布评论

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

评论(2

⒈起吃苦の倖褔 2024-10-17 00:54:09

冲突章节告诉我们:

使用 SDL_mixer 函数时,您
需要避免以下功能
来自 SDL:

SDL_OpenAudio
请改用 Mix_OpenAudio。

因此我想您问题的答案是:您不能混合使用 SDL_OpenAudio()MIX_OpenAudio() 函数。

The conflicts chapter tells this :

When using SDL_mixer functions you
need to avoid the following functions
from SDL:

SDL_OpenAudio
Use Mix_OpenAudio instead.

therefore I guess the answer to your question is : you can not mix SDL_OpenAudio() and MIX_OpenAudio() functions.

薆情海 2024-10-17 00:54:09

SDL_OpenAudio()SDL_CloseAudio() 是一对函数,一旦你在某个地方打开音频,你必须关闭它,否则你会得到类似的错误

音频设备已打开

当使用SDL_OpenAudio()时,它是一个单例,通过跟踪SDL源代码,你可能会发现有一个current_audio > 对象,这是全局音频对象。如果您同时打开多个音频文件,这可能会导致竞争情况。

我认为这个配对功能只适合音频/视频播放,不适用于游戏(游戏中可能同时有多个音频源),因为你可能不会在一个单一的时间中同时播放多个音频/视频时间过程。因此,在开始播放视频/音频文件之前,必须通过调用 SDL_GetAudioStatus() 确保 SDL 的音频状态为 SDL_AUDIO_STOPPED。播放音频/视频文件后,您应该执行以下操作:

SDL_LockAudio();
SDL_CloseAudio();
SDL_UnlockAudio();

这将确保音频完全停止。顺便说一句,锁定/解锁操作是为了确保您的音频回调代码此时不会运行。

SDL_OpenAudio() and SDL_CloseAudio() is a pair function, once you open audio some where, you must close it, or you'll get the error like

Audio device is already open

While using SDL_OpenAudio(), it's a singleton, by trace into SDL source code, you may find that there was a current_audio object and this is the global audio object. If you open multiple audio files in the same time, this may cause race condition.

I think this pair function only fit for audio/video playing, not for game(in game there may be multiple audio source at the same time), because you'll probably not playing multiple audio/video time at the same time in a single process. So before you start to play a video/audio file, you must ensure that the SDL's audio status is SDL_AUDIO_STOPPED by calling SDL_GetAudioStatus(). After playing a audio/video file, you should do something like that:

SDL_LockAudio();
SDL_CloseAudio();
SDL_UnlockAudio();

This will ensure that the audio stop completely. BTW, the lock/unlock operation is to ensure that your audio callback code do not running at the point.

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