使用SDL2_mixer库播放MP3音乐

原文链接:
https://blog.csdn.net/oyoung_2012/article/details/102696213

运行环境:Ubuntu:16.04

开发环境准备

安装libsdl2-mixer-dev

1
~$ sudo apt install libsdl2-mixer-dev

需要包含的头文件

1
2
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>

需要链接的库文件

1
2
3
4
5
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include <oyoung/defer.hpp>
#include <sspdlog/sspdlog.h>

#include <csignal>
#include <functional>

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

控制台输出:

1
2
3
4
5
6
7
8
9
[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())
感谢您对本站的支持.