ncurses + SLD2 和 SDL2_Mixer:尝试播放 mp3 时没有声音
ncurses + SLD2 and SDL2_Mixer: No sound when trying to play mp3
我正在用 C++ 编写一个程序,它使用 ncurses 库并播放声音效果。构建编译时,没有声音。
我已经安装并添加了 SDL2 和 SDL2_Mixer 框架到我的 IDE 项目(我正在使用 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;
}
构建编译成功,我去终端打开我的项目,然后...没有声音!?我做错了什么?
原来需要的是音乐文件的绝对路径:
Mix_AllocateChannels(1);
music = Mix_LoadMUS("/.../sound.mp3");
if (music) {
Mix_PlayMusic(music, -1);
}
我正在用 C++ 编写一个程序,它使用 ncurses 库并播放声音效果。构建编译时,没有声音。
我已经安装并添加了 SDL2 和 SDL2_Mixer 框架到我的 IDE 项目(我正在使用 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;
}
构建编译成功,我去终端打开我的项目,然后...没有声音!?我做错了什么?
原来需要的是音乐文件的绝对路径:
Mix_AllocateChannels(1);
music = Mix_LoadMUS("/.../sound.mp3");
if (music) {
Mix_PlayMusic(music, -1);
}