织梦CMS - 轻松建站从此开始!

罗索

SDL_mixer 播放mp3

jackyhwei 发布于 2011-04-14 15:04 点击:次 
A sample code using SDL to play MP3 file.
TAG:

A sample code using SDL to play MP3 file.

  1. #include <SDL.h> 
  2. #include <SDL_mixer.h> 
  3. // 
  4. //Code irrelevant to the situation 
  5. // 
  6. //Code irrelevant to the situation 
  7. // 
  8.  
  9. void musicFinished() { 
  10.     int musicPlaying = 0; 
  11.  
  12. int main(int argc, char *argv[]) { 
  13.     // Initialize SDL's subsystems 
  14.     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO) < 0) 
  15.     { 
  16.         fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); 
  17.         exit(1); 
  18.     } 
  19.  
  20.     int audio_rate = 44100; 
  21.     Uint16 audio_format = AUDIO_S16SYS; 
  22.     int audio_channel = 1; 
  23.     int audio_buffer = 2048; 
  24.  
  25.     if (Mix_OpenAudio(audio_rate, audio_format, audio_channel, audio_buffer) != 0) { 
  26.         fprintf(stderr, "Unable to initialize audio: %s\n", Mix_GetError()); 
  27.         exit(1); 
  28.     } 
  29.  
  30.  
  31.     Mix_Music *music; 
  32.     music = Mix_LoadMUS("test3.mp3"); 
  33.     if (music == NULL) { 
  34.         // This is where the error occurs. 
  35.         fprintf(stderr, "Unable to load mp3 file: %s\n", Mix_GetError()); 
  36.         exit(1); 
  37.     } 
  38.  
  39.     if (Mix_PlayMusic(music, 0) == -1) 
  40.     { 
  41.         fprintf(stderr, "Unable to play mp3 file: %s\n", Mix_GetError()); 
  42.         exit(1); 
  43.     } 
  44.  
  45.     int musicPlaying = 1; 
  46.     Mix_HookMusicFinished(musicFinished); 
  47.  
  48.     while (musicPlaying) { 
  49.         // do nothing 
  50.         SDL_Delay(2500); 
  51.     } 
  52.     musicFinished(); 
  53.     Mix_HaltMusic(); 
  54.     Mix_FreeMusic(music); 
  55.     Mix_CloseAudio(); 
  56.  
  57.     atexit(SDL_Quit); 
  58.  
  59.     return 0; 

 

(foxhengxing)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201104/11210.html]
本文出处:cnblogs.com/foxhengxing 作者:foxhengxing
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容