使用SDL2_mixer库播放MP3音乐

使用SDL2_mixer库播放MP3音乐

运行环境:Ubuntu:16.04

开发环境准备

  1. 安装libsdl2-mixer-dev
~$ sudo apt install libsdl2-mixer-dev

需要包含的头文件

#include 
#include 

需要链接的库文件

set(CMAKE_CXX_FLAGS "-pthread ${CMAKE_CXX_FLAGS}")
target_link_libraries(app
    SDL2
    SDL2_mixer
    )

流程

-> Mix_Init
-> Mix_OpenAudio
-> Mix_LoadMUS / Mix_LoadWAV
-> Mix_PlayMusic / Mix_FadeInMusic / Mix_PlayChanel / Mix_FadeInChannel
-> Others User Operations
-> Mix_HaltMusic / Mix_FadeOutMusic / Mix_HaltChannel / Mix_FaleOutChannel
-> Mix_CloseAudio
-> Mix_Quit

钩子函数

  • Mix_HookMusic: 用于设置音乐播放过程中的回调
  • Mix_HookMusicFinished: 用于设置音乐播放结束时的回调

示例代码(C++11)

#include 
#include 
#include 
#include 

#include 
#include 

static std::function<void()> on_signal;

void signal_TERM_handle(int) {
    if(on_signal) on_signal();
}

int main(int,char**)
{
    USE_DEFER;

    on_signal = [&] {
        __defer__.~Defer();
    };

    std::signal(SIGTERM, signal_TERM_handle);

    auto n = Mix_Init(MIX_INIT_OGG | MIX_INIT_MP3 | MIX_INIT_FLAC);

    if(n == 0) {
        SSPD_LOG_ERROR << "[Mix_Init] Failed initialization: " << Mix_GetError();
        return n;
    }

    SSPD_LOG_INFO << "[Mix_Init] Mix initialize successfully";

    defer [=] {
        SSPD_LOG_INFO << "[Mix_Quit] Mix will quit";
        Mix_Quit();
    };

    n = Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024);

    if(n < 0) {
        SSPD_LOG_ERROR << "[Mix_OpenAudio] Failed open audio: " << Mix_GetError();
        return n;
    }

    SSPD_LOG_INFO << "[Mix_OpenAudio] Open Audio successfully";

    defer [=] {
        SSPD_LOG_INFO << "[Mix_CloseAudio] Mix will close audio";
        Mix_CloseAudio();
    };

    auto music = Mix_LoadMUS("/home/oyoung/Music/8.mp3");

    if(music == nullptr) {
        SSPD_LOG_ERROR << "[Mix_LoadMUS] Failed load music: " << Mix_GetError();
        return -1;
    }

    SSPD_LOG_INFO << "[Mix_LoadMUS] Load music successfully";

    defer [=] {
       SSPD_LOG_INFO << "[Mix_FreeMusic] Music will be free";
       Mix_FreeMusic(music);
    };

    n = Mix_FadeInMusic(music, 0, 1000);

    Mix_HookMusicFinished([] {
        SSPD_LOG_INFO << "[Main] Music play finished";
    });


    if(n < 0) {
        SSPD_LOG_ERROR << "[Mix_FadeInMusic] Failed fade in music: " << Mix_GetError();
        return n;
    }

    SSPD_LOG_INFO << "[Mix_FadeInMusic] Music start playing";

    defer [=] {
        SSPD_LOG_INFO << "[Mix_FadeOutMusic] Mix will fade out music";
        Mix_FadeOutMusic(1000);
    };

    while (Mix_PlayingMusic()) {
        SDL_Delay(1000);
    }



    return 0;
}

控制台输出:

[2019-10-23 10:30:55.443]-[info]- [Mix_Init] Mix initialize successfully (main.cc #32 main)
[2019-10-23 10:30:55.458]-[info]- [Mix_OpenAudio] Open Audio successfully (main.cc #46 main)
[2019-10-23 10:30:55.458]-[info]- [Mix_LoadMUS] Load music successfully (main.cc #60 main)
[2019-10-23 10:30:55.458]-[info]- [Mix_FadeInMusic] Music start playing (main.cc #79 main)
[2019-10-23 10:31:00.712]-[info]- [Mix_FadeOutMusic] Mix will fade out music (main.cc #82 operator())
[2019-10-23 10:31:00.713]-[info]- [Mix_FreeMusic] Music will be free (main.cc #63 operator())
[2019-10-23 10:31:01.724]-[info]- [Main] Music play finished (main.cc #70 operator())
[2019-10-23 10:31:01.739]-[info]- [Mix_CloseAudio] Mix will close audio (main.cc #49 operator())
[2019-10-23 10:31:01.755]-[info]- [Mix_Quit] Mix will quit (main.cc #35 operator())

你可能感兴趣的:(C++-三方库接入,C++,音频,Ubuntu)