SDL(Simple DirectMedia Layer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成。
SDL提供了数种控制图像、声音、输出入的函数,让开发者只要用相同或是相似的代码就可以开发

出跨多个平台(Linux、Windows、Mac OS X等)的应用软件。目前SDL多用于开发游戏、模拟器、媒体播放器等多媒体应用领域。


SDL本身只支持WAV, 其他个数可以使用SDL_mixer扩展库
而SDL_mixer 本身也只提供WAV的解码播放功能.其余格式都是封装第三方库来实现的.
其中对于常见的MP3格式, requiring SMPEG or MAD library on system

经过摸索, 成功结合SMPEG, 编译SDL_mixer库, 并播放mp3文件, 下面是过程

环境: centos5  gcc4.1.2

1. 安装SDL

官方下载地址: http://www.libsdl.org/
centos5下安装比较简单, 一般步骤即可
./configure --prefix=Dir
make
make install

2. 安装SDL_mixer需要的库

SMPEG被SDL_mixer用于对MP3文件解码

下载地址: http://www.libsdl.org/projects/SDL_mixer/

该网页提供的相应的smpeg的tar包, 直接下载

安装方法:

./configure --prefix=Dir
make 
make install

3. 安装SDL_mixer

在安装SDL_mixer时, 需要指定SMPEG的安装路径(上一个步骤中指定的prefix)
SDL_mixer下载地址: http://www.libsdl.org/projects/SDL_mixer/

查看configure文件发现:

--with-sdl-prefix=PFX   Prefix where SDL is installed (optional)
--with-smpeg-prefix=PFX   Prefix where SMPEG is installed (optional), 找到这个不容易呀:)

因为我的centos5自带了SDL, 不太确定是否必须指定--with-sdl-prefix.
安装

./configure --prefix=Dir --with-smpeg-prefix=smpeg-home-dir --with-sdl-prefix=sdl-home-dir
make
make install

4. 测试

测试代码(摘自网络):

#include <SDL.h> 
#include <SDL_mixer.h> 
// 
//Code irrelevant to the situation 
// 
//Code irrelevant to the situation 
// 

int musicPlaying = 1; 
void musicFinished() { 
    musicPlaying = 0; 
} 
 
int main(int argc, char *argv[]) { 
    // Initialize SDL's subsystems 
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO) < 0) 
    { 
        fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); 
        exit(1); 
    } 
 
    int audio_rate = 44100; 
    Uint16 audio_format = AUDIO_S16SYS; 
    int audio_channel = 1; 
    int audio_buffer = 2048; 
 
    if (Mix_OpenAudio(audio_rate, audio_format, audio_channel, audio_buffer) != 0) { 
        fprintf(stderr, "Unable to initialize audio: %s\n", Mix_GetError()); 
        exit(1); 
    } 
 
 
    Mix_Music *music; 
    music = Mix_LoadMUS(argv[1]); 
    if (music == NULL) { 
        // This is where the error occurs. 
        fprintf(stderr, "Unable to load mp3 file: %s\n", Mix_GetError()); 
        exit(1); 
    } 
 
    if (Mix_PlayMusic(music, 0) == -1) 
    { 
        fprintf(stderr, "Unable to play mp3 file: %s\n", Mix_GetError()); 
        exit(1); 
    } 
 
    musicPlaying = 1; 
    Mix_HookMusicFinished(musicFinished); 
 
    while (musicPlaying) { 
        // do nothing 
        SDL_Delay(2500); 
    } 
    Mix_HaltMusic(); 
    Mix_FreeMusic(music); 
    Mix_CloseAudio(); 
 
    atexit(SDL_Quit); 
 
    return 0; 
}

编译运行test.c

假设:
SDL 目录为: SDL_HOME
SDL_mixer目录为: SDL_MIXER_HOME

gcc -o test test.c -L${SDL_HOME}/lib -L${SDL_MIXER_HOME}/lib -lSDL -lSDLmain -lSDL_mixer -I${SDL_HOME}/include/SDL -I${SDL_MIXER_HOME}/include/SDL
./test test.mp3

如果在运行时遇到找不到SDL_mixer共享库的问题, 可以通过指定LD_LIBRARY_PATH=${SDL_MIXER_HOME}/lib解决
或者在/etc/ld.so.conf.d/下添加SDL.conf

export LD_LIBRARY_PATH=${SDL_MIXER_HOME}/lib
#或者在SDL.conf中添加
${SDL_MIXER_HOME}/lib

ok, listen~

你的反馈就是博主进步的最大动力


Logo

更多推荐