博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用SDL2_mixer库播放MP3音乐
阅读量:3575 次
发布时间:2019-05-20

本文共 3166 字,大约阅读时间需要 10 分钟。

使用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
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())

转载地址:http://zyagj.baihongyu.com/

你可能感兴趣的文章
JAVA学习笔记
查看>>
IDEA-Tips
查看>>
微信小程序 - 遇到的问题
查看>>
Vue+Elementui实现多个表格table合并
查看>>
SpringMVC入门项目
查看>>
初玩Activiti7一套组合拳,连连败退之下竟突破神功,且看SpringBoot整治Activiti7小老弟!
查看>>
Error creating bean with name ‘processEngine‘: FactoryBean threw exception on object creation; nest
查看>>
Tomcat找不到指定的路径D\apache-tomcat-9.0.12\backup\catalina.policy (系统找不到指定的路径).........
查看>>
spring连接c3p0出现错误 Connections could not be acquired from the underlying database!
查看>>
The server cannot or will not process the request due to something that is perceived to be a client
查看>>
unable to process parts as no multi-part configuration has been provided
查看>>
windows按照在idea上使用svn连接服务器遇到E120108: Unable to connect to a repository at URL
查看>>
如果不管你这么换jar包都不行,看这里No converter found for return value of type:
查看>>
unity自学笔记--光照
查看>>
java语法一些易忘知识点
查看>>
笔试DAY3
查看>>
笔试DAY4
查看>>
笔试DAY5
查看>>
笔试DAY6
查看>>
p3-Regression-Case Study
查看>>