简体   繁体   English

ncurses + SLD2和SDL2_Mixer:尝试播放mp3时无声音

[英]ncurses + SLD2 and SDL2_Mixer: No sound when trying to play mp3

I am writing a program in C++ that uses the ncurses library and also plays sound effects. 我正在用C ++编写一个使用ncurses库的程序,并且还可以播放声音效果。 While the build compiles, there is no sound. 编译时,没有声音。

I have installed and added both the SDL2 and SDL2_Mixer frameworks to my IDE project (I'm using Xcode). 我已经将SDL2和SDL2_Mixer框架安装并添加到我的IDE项目中(我正在使用Xcode)。 I have also added the mp3 file to the same directory as my other project files. 我还将mp3文件添加到了与其他项目文件相同的目录中。 Here's my code: 这是我的代码:

#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;
}

The build compiles successfully, I go to the terminal and open my project, and then... no sound!? 构建成功编译,我进入终端并打开我的项目,然后……没有声音!? What am I doing wrong? 我究竟做错了什么?

It turned out that what was needed was an absolute path to the music file: 原来,所需的是音乐文件的绝对路径:

Mix_AllocateChannels(1); 
music = Mix_LoadMUS("/.../sound.mp3");
if (music) {
Mix_PlayMusic(music, -1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM