我正在用C ++编写一个程序,它使用ncurses库并播放声音效果。构建编译时,没有声音。
我已经在我的IDE项目中安装并添加了SDL2和SDL2_Mixer框架(我正在使用Xcode)。我还将mp3文件添加到与其他项目文件相同的目录中。这是我的代码:
#include <SDL2/SDL.h>
#include <SDL2_Mixer/SDL_Mixer.h>
#if defined(WIN32)
#include "curses.h"
#else
#include <curses.h>
#endif
...
int main(int argc, char * argv[]) {
Mix_Music *music;
// Initialize music.
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
fprintf(stderr, "unable to initialize SDL\n");
exit(EXIT_FAILURE);
}
if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3) {
fprintf(stderr, "unable to initialize SDL_mixer\n");
exit(EXIT_FAILURE);
}
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024) != 0) {
fprintf(stderr, "unable to initialize audio\n");
exit(EXIT_FAILURE);
}
Mix_AllocateChannels(1); // only need background music
music = Mix_LoadMUS("sound.mp3");
if (music) {
Mix_PlayMusic(music, -1);
}
...
Mix_HaltMusic();
Mix_FreeMusic(music);
Mix_CloseAudio();
Mix_Quit();
return 0;
}
构建编译成功,我去终端打开我的项目,然后......没有声音!?我做错了什么?
答案 0 :(得分:0)
事实证明,所需要的是音乐文件的绝对路径:
Mix_AllocateChannels(1);
music = Mix_LoadMUS("/.../sound.mp3");
if (music) {
Mix_PlayMusic(music, -1);
}