一段时间以来,我一直在网上搜索有关此问题的一些帮助,但找不到有关此特定问题的帮助:我打算使用 SDL Mixer 播放 wav 文件“click.wav”。由于目前我无法弄清楚的原因,SDL Mixer 无法正确加载它。
我不明白的是,在不同的机器上执行完全相同的代码。除了加载和播放声音文件之外,其他所有内容都可以完美编译和执行:
Mix_Chunk *sound = NULL;
sound = Mix_LoadWAV("click.wav");
if(sound == NULL) {
fprintf(framelog, "Unable to load WAV file: %s\n";
Mix_GetError());
}
Mix_LoadWAV 返回 NULL 但不幸的是 Mix_GetError() 没有返回错误代码。我什至开始查看 SDL 源代码,但我没有走多远。在 SDL_mixer.h:
#define Mix_LoadWAV(file) Mix_LoadWAV_RW(SDL_RWFromFile(file, "rb"), 1)
中,即 Mix_LoadWAV 被从 RWOPS 结构中读取文件的 Mix_LoadWAV_RW 调用替换。它主要委托SDL_RWFromFile(file, "rb")
实际打开文件并生成结构。也许这就是错误的来源。
两台计算机都使用 Windows 7 64 位操作系统、Visual Studio 2008 和 SDL2。我安装并检查了所有文件: 头文件:C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\gl 32 位库文件:C:\Program Files (x86)\Microsoft Visual Studio 11.0\ VC\lib 32 位 dll 文件:C:\WINDOWS\SysWOW64 64 位 lib 文件:C:\Program Files (x86)\Microsoft Visual Studio 11.0\Windows\VC\libs\amd64 64 位 dll 文件:C: \WINDOWS\System32
我为链接器设置了以下附加依赖项:SDL2.lib、SDL2main.lib、opengl32.lib、odbc32.lib、odbccp32.lib、SDL_mixer.lib
您将在下面找到我使用的代码。即使 SDL Mixer 无法加载波形文件,该文件也可以在这台计算机上播放,而其他程序不会出现任何问题。
如果有一些关于如何解决这个愚蠢问题的建议,我将不胜感激。我不是专家级程序员,最近才开始使用 SDL。
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <SDL/SDL.h> // SDL2 input
#include <SDL/SDL_mixer.h>
#include <GL/freeglut.h>
#include <ctime> // get time() function for date
#include <algorithm> // std::random_shuffle
#include <string.h>
#include <stdlib.h>
#include "randomc.h" // define classes for random number generators
//#include "userintf.cpp" // define system specific user interface
#include "mersenne.cpp" // members of class TRandomMersenne
int main(int argc, char *argv[])
{
// Initialize certain SDL units
if (SDL_Init( SDL_INIT_EVERYTHING ) < 0) {
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, BPP );
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 1); //1 bit for the stencil
screen = SDL_CreateWindow(
"SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
XMAX, // width, in pixels
YMAX, // height, in pixels
SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN
);
// Create an OpenGL context associated with the window.
screencontext = SDL_GL_CreateContext(screen);
if (screen == NULL) {
printf("Unable to set specified video format!\n");
exit(2);
}
glutInit(&argc, argv);
int audio_rate = 44100;
Uint16 audio_format = AUDIO_S16SYS;
int audio_channels = 2;
int audio_buffers = 1024;
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) {
fprintf(framelog, "Unable to initialize audio: %s\n", Mix_GetError());
exit(1);
}
Mix_Chunk *sound = NULL;
sound = Mix_LoadWAV("click.wav");
if(sound == NULL) {
fprintf(framelog, "Unable to load WAV file: %s\n", Mix_GetError());
exit(1);
}
}