我可以在同一个游戏中使用 Mix_OpenAudio 和 SDL_OpenAudio 吗?
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
冲突章节告诉我们:
因此我想您问题的答案是:您不能混合使用
SDL_OpenAudio()
和MIX_OpenAudio()
函数。The conflicts chapter tells this :
therefore I guess the answer to your question is : you can not mix
SDL_OpenAudio()
andMIX_OpenAudio()
functions.SDL_OpenAudio()
和SDL_CloseAudio()
是一对函数,一旦你在某个地方打开音频,你必须关闭它,否则你会得到类似的错误当使用
SDL_OpenAudio()
时,它是一个单例,通过跟踪SDL源代码,你可能会发现有一个current_audio
> 对象,这是全局音频对象。如果您同时打开多个音频文件,这可能会导致竞争情况。我认为这个配对功能只适合音频/视频播放,不适用于游戏(游戏中可能同时有多个音频源),因为你可能不会在一个单一的时间中同时播放多个音频/视频时间过程。因此,在开始播放视频/音频文件之前,必须通过调用
SDL_GetAudioStatus()
确保 SDL 的音频状态为SDL_AUDIO_STOPPED
。播放音频/视频文件后,您应该执行以下操作:这将确保音频完全停止。顺便说一句,锁定/解锁操作是为了确保您的音频回调代码此时不会运行。
SDL_OpenAudio()
andSDL_CloseAudio()
is a pair function, once you open audio some where, you must close it, or you'll get the error likeWhile using
SDL_OpenAudio()
, it's a singleton, by trace into SDL source code, you may find that there was acurrent_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 callingSDL_GetAudioStatus()
. After playing a audio/video file, you should do something like that: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.