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

罗索

使用SDL , mpg123例子的mp3播放器

jackyhwei 发布于 2011-05-07 21:12 点击:次 
由于使用SDL所以可以在多数平台下编译通过。
TAG:

由于使用SDL所以可以在多数平台下编译通过。

  1. #include <stdlib.h> 
  2. #include <math.h> 
  3. #include <SDL/SDL.h> 
  4. #include <SDL/SDL_main.h> 
  5. #include <assert.h> 
  6. #include <unistd.h> 
  7. #include <sys/select.h> 
  8. #include <sys/time.h> 
  9. #include <sys/types.h> 
  10. #include <time.h> 
  11. #include "mpg123.h" 
  12.  
  13. static Uint8 buffer[8192]; 
  14. static int offset = 0; 
  15. static int length = 0; 
  16. static int tryexit(); 
  17. static Uint8 __outbuf[1638400]; 
  18.  
  19. void example_mixaudio(void *unused, Uint8 * stream, int len) 
  20.     int count = len; 
  21.     if (offset + count > length){ 
  22.         count = length - offset; 
  23.     } 
  24.     memcpy(stream, __outbuf+offset, count); 
  25.     offset += count; 
  26.  
  27. static void play_setup(long rate, int chanel) 
  28.     SDL_AudioSpec *desired, *obtained; 
  29.     obtained = (SDL_AudioSpec *) malloc(sizeof(SDL_AudioSpec)); 
  30.     desired = (SDL_AudioSpec *) malloc(sizeof(SDL_AudioSpec)); 
  31.     desired->freq = rate; 
  32.     desired->format = AUDIO_S16; 
  33.     desired->samples = 4096; 
  34.     desired->callback = example_mixaudio; 
  35.     desired->userdata = NULL; 
  36.     desired->channels = chanel; 
  37.     if (SDL_OpenAudio(desired, obtained) < 0) { 
  38.         fprintf(stderr, "AudioMixer, Unable to open audio: %s\n"
  39.                 SDL_GetError()); 
  40.         exit(-1); 
  41.     } 
  42.     /* 
  43.     audioBufferSize = obtained->samples; 
  44.     samplesFrequency = obtained->freq; 
  45.     outputAudioBufferSize = audioBufferSize; 
  46.     if (obtained->format == AUDIO_U16 || obtained->format == AUDIO_U16) { 
  47.         outputAudioBufferSize += audioBufferSize; 
  48.     }*/ 
  49.     SDL_PauseAudio(0); 
  50.     free(desired); 
  51.     free(obtained); 
  52.  
  53. static int play_audio(Uint8 buf[], int len) 
  54.     int cut = 0; 
  55.     int count = 0; 
  56.     timeval tv; 
  57.     do
  58.          SDL_LockAudio(); 
  59.          length -= offset; 
  60.          memmove(__outbuf, __outbuf+offset, length); 
  61.         offset = 0; 
  62.         count = sizeof(__outbuf)-length; 
  63.         if (cut + count > len){ 
  64.             count = len - cut; 
  65.         } 
  66.          memcpy(__outbuf+length, buf+cut, count); 
  67.         length += count; 
  68.         cut += count; 
  69.          SDL_UnlockAudio(); 
  70.         if (cut < len && tryexit()==true){ 
  71.              tv.tv_usec = 20000; 
  72.              tv.tv_sec = 0; 
  73.              select(0, NULL, NULL, NULL, &tv); 
  74.         } 
  75.        }while(cut < len && tryexit()==true); 
  76.     return 0; 
  77.  
  78. static int tryexit() 
  79.       /* Initialize defaults, Video and Audio */ 
  80.     static bool running = true
  81.     SDL_Event event; 
  82.     if(running) { 
  83.         if(SDL_PollEvent(&event)) { 
  84.             switch (event.type) { 
  85.             case SDL_KEYDOWN: 
  86.                 switch (event.key.keysym.sym) { 
  87.                 case SDLK_ESCAPE: 
  88.                     running = false
  89.                     break
  90.                 default
  91.                     break
  92.                 } 
  93.                 break
  94.             case SDL_QUIT: 
  95.                 running = false
  96.                 break
  97.             } 
  98.             SDL_Delay(1); 
  99.         } 
  100.         SDL_Delay(1); 
  101.     } 
  102.     /* Shutdown all subsystem */ 
  103.     if (running == false){ 
  104.          SDL_Quit(); 
  105.     } 
  106.     return running; 
  107.  
  108. static void load_file(mpg123_handle *mh, const char *path) 
  109.     int err; 
  110.     unsigned int n; 
  111.     unsigned int done; 
  112.     Uint8 outbuf[1638400]; 
  113.  
  114.     FILE *fp = fopen(path, "rb"); 
  115.     if (fp == NULL){ 
  116.         return
  117.     } 
  118.     while(feof(fp) == 0 && tryexit()==true){ 
  119.         n = fread(buffer, 1, sizeof(buffer), fp); 
  120.         err = mpg123_decode(mh, buffer, n, outbuf, 163840, &done); 
  121.         if (err == MPG123_NEW_FORMAT){ 
  122.             long rate; 
  123.             int channels, enc; 
  124.             mpg123_getformat(mh, &rate, &channels, &enc); 
  125.             printf("new format\n"); 
  126.             play_setup(rate, channels); 
  127.         } 
  128.         play_audio(outbuf, done); 
  129.         while(err!=MPG123_NEED_MORE && err!=MPG123_ERR && tryexit()==true){ 
  130.             err = mpg123_decode(mh, NULL, 0, outbuf, 163840, &done); 
  131.             play_audio(outbuf, done); 
  132.         } 
  133.         if (err == MPG123_ERR){ 
  134.             printf("err = %s\n", mpg123_strerror(mh)); 
  135.             break
  136.         } 
  137.     } 
  138.     fclose(fp); 
  139.  
  140. int main(int argc, char *argv[]) 
  141.     int i; 
  142.     int err = 0; 
  143.     printf("Initializing SDL.\n"); 
  144.     if ((SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1)) { 
  145.         printf("Could not initialize SDL: %s.\n", SDL_GetError()); 
  146.         exit(-1); 
  147.     } 
  148.     SDL_Surface *screen = SDL_SetVideoMode(200, 200, 16, SDL_SWSURFACE); 
  149.     SDL_WM_SetCaption("Audio Example", 0); 
  150.     printf("SDL initialized.\n"); 
  151.     for (i=1; i<argc; i++){ 
  152.          mpg123_init(); 
  153.          mpg123_handle *mh = mpg123_new(NULL, &err); 
  154.          assert(mh != NULL); 
  155.          err = mpg123_open_feed(mh); 
  156.          assert(err == MPG123_OK); 
  157.         load_file(mh, argv[i]); 
  158.         mpg123_close(mh); 
  159.         mpg123_delete(mh); 
  160.         mpg123_exit(); 
  161.     } 
  162.     while (tryexit() == true){ 
  163.         ; 
  164.     } 
  165.     return 0; 
  166. }
(pagxir)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201105/11511.html]
本文出处:pagxir.cublog.cn 作者:pagxir
顶一下
(2)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容