LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1310|回复: 11

Linux编程新手做单文件播放器

[复制链接]
发表于 2006-12-6 09:33:14 | 显示全部楼层 |阅读模式

  1. /***************************************************************************
  2. *   Copyright (C) 2006 by root   *
  3. *   root@localhost.localdomain   *
  4. *                                                                         *
  5. *   This program is free software; you can redistribute it and/or modify  *
  6. *   it under the terms of the GNU General Public License as published by  *
  7. *   the Free Software Foundation; either version 2 of the License, or     *
  8. *   (at your option) any later version.                                   *
  9. *                                                                         *
  10. *   This program is distributed in the hope that it will be useful,       *
  11. *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12. *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13. *   GNU General Public License for more details.                          *
  14. *                                                                         *
  15. *   You should have received a copy of the GNU General Public License     *
  16. *   along with this program; if not, write to the                         *
  17. *   Free Software Foundation, Inc.,                                       *
  18. *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19. ***************************************************************************/
  20. #include < gtk/gtk.h >  //gtk/gtk.h
  21. #include < fmod.h >      //fmod. h
  22. #include < fmod_errors.h >  // fmod_errors.h
  23. #include < stdio.h >              //stdio.h

  24. FMOD_SYSTEM  *FModsystem = NULL;
  25. FMOD_SOUND   *sound1 = NULL;
  26. FMOD_RESULT  result = FMOD_OK;
  27. FMOD_CHANNEL *channel = NULL;

  28. void ERRCHECK(FMOD_RESULT result)
  29. {
  30.    if(result != FMOD_OK)
  31.    {
  32.      printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  33.      exit(-1);
  34.    }
  35. }

  36. //----------
  37. // 初始化FMOD对象
  38. //----------
  39. int FMOD_Init()
  40. {
  41.    unsigned int   version = 0;
  42.    
  43.    result = FMOD_System_Create(&FModsystem);
  44.    ERRCHECK(result);

  45.    result = FMOD_System_GetVersion(FModsystem, &version);
  46.    ERRCHECK(result);

  47.    if(version < FMOD_VERSION)
  48.    {
  49.       printf("Error! You are using an ole version of FMOD %08x. This program requires %08x\n",\
  50.             version, FMOD_VERSION);
  51.       getch();
  52.       return 0;
  53.    }
  54.    
  55.    result = FMOD_System_Init(FModsystem, 32, FMOD_INIT_NORMAL, NULL);
  56.    ERRCHECK(result);
  57.    return 0;
  58. }

  59. //-------------
  60. //播放
  61. //-------------
  62. int FMOD_PlayFile()
  63. {
  64.    
  65.    FMOD_SOUND *sound2, *sound3;
  66.    
  67.    
  68.    int           key = 0;
  69.    
  70.    int            playing = 0;
  71.    if( channel != NULL)
  72.    {
  73.         FMOD_Channel_IsPlaying(channel, &playing);
  74.         
  75.         if(playing)
  76.         {
  77.          int paused = 0;
  78.          FMOD_Channel_GetPaused(channel, &paused);
  79.          FMOD_Channel_SetPaused(channel, !paused);
  80.          return 0;
  81.         }
  82.    }

  83.    result = FMOD_System_CreateStream(FModsystem, "/windows/Server/03.ogg", FMOD_DEFAULT, 0, &sound1);
  84.    ERRCHECK(result);
  85.    
  86.    printf("=====================================================\n");
  87.    printf("PlayStream Example Copyright (c) Firelight Technologies 2004-2005.\n");
  88.    
  89.    result = FMOD_System_PlaySound(FModsystem, FMOD_CHANNEL_FREE, sound1, 0, &channel);
  90.    ERRCHECK(result);

  91.    return 1;
  92. }

  93. //---------------
  94. // 释放FMOD音频对象
  95. //---------------
  96. void ReleaseFMOD()
  97. {
  98.    result = FMOD_Sound_Release(sound1);
  99.    ERRCHECK(result);
  100.    result = FMOD_System_Close(FModsystem);
  101.    ERRCHECK(result);
  102.    result = FMOD_System_Release(FModsystem);
  103.    ERRCHECK(result);
  104. }

  105. //-----------------------
  106. // 打开
  107. //-----------------------
  108. void callback(GtkWidget *widget,
  109.             gpointer data)
  110. {
  111.    g_print("Hello again - %s was pressed\n", (gchar*)data);
  112. }

  113. //-------------------------------
  114. // 打开
  115. //-------------------------------
  116. void Open(GtkWidget *widget, gpointer data)
  117. {
  118.   g_print("Open File - %s \n", (gchar*)data);
  119. }

  120. void Pause(GtkWidget *widget, gpointer data)
  121. {
  122.   
  123. }

  124. void Stop(GtkWidget *widget, gpointer data)
  125. {
  126.   g_print("Stop Play - %s was pressed\n", (gchar*)data);
  127. }

  128. void Close(GtkWidget *widget, gpointer data)
  129. {
  130.   g_print("Close Play - %s was pressed\n", (gchar*)data);
  131. }

  132. void Play(GtkWidget *widget, gpointer data)
  133. {
  134.    FMOD_PlayFile();
  135. }

  136. //--------------------------
  137. // 删除事件
  138. //--------------------------
  139. gint delete_event(GtkWidget *widget,
  140.                 GdkEvent  *event,
  141.                 gpointer data)
  142. {
  143.    gtk_main_quit();
  144.    return FALSE;
  145. }

  146. //----------------------
  147. // 入口函数
  148. //----------------------
  149. int main(int argc, char *argv[])
  150. {
  151.   GtkWidget *window = NULL;
  152.   GtkWidget *button = NULL;
  153.   GtkWidget *box1   = NULL;
  154.   
  155.   FMOD_Init();
  156.   gtk_init(&argc, &argv);
  157.    
  158.   //显示窗口 并设置窗口标题
  159.   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  160.   gtk_window_set_title(GTK_WINDOW(window), "PlayAudioer");

  161.   g_signal_connect(G_OBJECT(window),  "delete_event",
  162.                  G_CALLBACK(delete_event), NULL);
  163.   gtk_container_set_border_width(GTK_CONTAINER(window), 10);
  164.   box1 = gtk_hbox_new(FALSE, 0);
  165.   gtk_container_add(GTK_CONTAINER(window), box1);

  166.   //第一个按钮
  167.   button = gtk_button_new_with_label("打开");
  168.   g_signal_connect(G_OBJECT(button), "clicked",
  169.                  G_CALLBACK(Open), "button1");
  170.   gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
  171.   gtk_widget_show(button);
  172.   
  173.   //第二个按钮
  174.   button = gtk_button_new_with_label("暂停");
  175.   g_signal_connect(G_OBJECT(button), "clicked",
  176.                  G_CALLBACK(Pause), "button2");
  177.   gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
  178.   gtk_widget_show(button);  

  179.   //第三个按钮
  180.   button = gtk_button_new_with_label("停止");
  181.   g_signal_connect(G_OBJECT(button), "clicked",
  182.                  G_CALLBACK(Stop), "button3");
  183.   gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
  184.   gtk_widget_show(button);

  185.   //第四个按钮
  186.   button = gtk_button_new_with_label("关闭");
  187.   g_signal_connect(G_OBJECT(button), "clicked",
  188.                  G_CALLBACK(Close), "button4");
  189.   gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
  190.   gtk_widget_show(button);

  191.   //第五个按钮
  192.   button = gtk_button_new_with_label("播放");
  193.   g_signal_connect(G_OBJECT(button), "clicked",
  194.                  G_CALLBACK(Play), "button5");
  195.   gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
  196.   gtk_widget_show(button);
  197.   //--------------------
  198.   // 显示包装盒和窗口
  199.   //--------------------
  200.   gtk_widget_show(box1);
  201.   gtk_widget_show(window);
  202.   gtk_main();
  203.   ReleaseFMOD();
  204.   return 0;
  205. }
复制代码



编译:

gcc -O3 -o playsound -I/usr/local/include/fmodex -include/work/BzipB/fmodapi40453linux64/examples/common/wincompat.h hell.c /usr/local/lib/libfmodex64.so -pthread `pkg-config --cflags --libs gtk+-2.0`

具体路径替换为自己的。
发表于 2006-12-6 12:10:30 | 显示全部楼层
就这点代码?要不要用到什么库啊?能播放什么类型的文件?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-12-7 15:03:16 | 显示全部楼层
fmod 库用的, 能播放 wma  mp3 wav  等,ogg 一般的都支持。
回复 支持 反对

使用道具 举报

发表于 2006-12-8 16:57:08 | 显示全部楼层
Copyright (C) 2006 by root   *
*   root@localhost.localdomain   *

老大写上你自己的名字或者ID。。。。。。。。
回复 支持 反对

使用道具 举报

发表于 2006-12-8 16:58:11 | 显示全部楼层
Copyright (C) 2006 by root   *
*   root@localhost.localdomain   *

老大写上你自己的名字或者ID。。。。。。。。
回复 支持 反对

使用道具 举报

发表于 2006-12-21 19:39:21 | 显示全部楼层
我顺便研究了一下fmode的官方例子,发现了一个简单的,大家学习一下
  1. /*===============================================================================================
  2. PlaySound Example
  3. Copyright (c), Firelight Technologies Pty, Ltd 2004-2005.
  4. This example shows how to simply load and play multiple sounds.  This is about the simplest
  5. use of FMOD.
  6. This makes FMOD decode the into memory when it loads.  If the sounds are big and possibly take
  7. up a lot of ram, then it would be better to use the FMOD_CREATESTREAM flag so that it is
  8. streamed in realtime as it plays.
  9. ===============================================================================================*/
  10. #include "../../api/inc/fmod.h"
  11. #include "../../api/inc/fmod_errors.h"
  12. #include "../common/wincompat.h"
  13. #include <stdio.h>
  14. void ERRCHECK(FMOD_RESULT result)
  15. {
  16.     if (result != FMOD_OK)
  17.     {
  18.         printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  19.         exit(-1);
  20.     }
  21. }
  22. int main(int argc, char *argv[])
  23. {
  24.     FMOD_SYSTEM      *system;
  25.     FMOD_SOUND       *sound1, *sound2, *sound3;
  26.     FMOD_CHANNEL     *channel = 0;
  27.     FMOD_RESULT       result;
  28.     int               key;
  29.     unsigned int      version;
  30.     /*
  31.         Create a System object and initialize.
  32.     */
  33.     result = FMOD_System_Create(&system);
  34.     ERRCHECK(result);
  35.     result = FMOD_System_GetVersion(system, &version);
  36.     ERRCHECK(result);
  37.     if (version < FMOD_VERSION)
  38.     {
  39.         printf("Error!  You are using an old version of FMOD %08x.  This program requires %08x\n", version, FMOD_VERSION);
  40.         getch();
  41.         return 0;
  42.     }
  43.     result = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, NULL);
  44.     ERRCHECK(result);
  45.     result = FMOD_System_CreateSound(system, "../media/drumloop.wav", FMOD_SOFTWARE, 0, &sound1);
  46.     ERRCHECK(result);
  47.     result = FMOD_Sound_SetMode(sound1, FMOD_LOOP_OFF);
  48.     ERRCHECK(result);
  49.     result = FMOD_System_CreateSound(system, "../media/jaguar.wav", FMOD_SOFTWARE, 0, &sound2);
  50.     ERRCHECK(result);
  51.     result = FMOD_System_CreateSound(system, "../media/swish.wav", FMOD_SOFTWARE, 0, &sound3);
  52.     ERRCHECK(result);
  53.     printf("===================================================================\n");
  54.     printf("PlaySound Example.  Copyright (c) Firelight Technologies 2004-2005.\n");
  55.     printf("===================================================================\n");
  56.     printf("\n");
  57.     printf("Press '1' to Play a mono sound using hardware mixing\n");
  58.     printf("Press '2' to Play a mono sound using software mixing\n");
  59.     printf("Press '3' to Play a stereo sound using hardware mixing\n");
  60.     printf("Press 'Esc' to quit\n");
  61.     printf("\n");
  62.     /*
  63.         Main loop.
  64.     */
  65.     do
  66.     {
  67.         if (kbhit())
  68.         {
  69.             key = getch();
  70.             switch (key)
  71.             {
  72.                 case '1' :
  73.                 {
  74.                     result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound1, 0, &channel);
  75.                     ERRCHECK(result);
  76.                     break;
  77.                 }
  78.                 case '2' :
  79.                 {
  80.                     result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound2, 0, &channel);
  81.                     ERRCHECK(result);
  82.                     break;
  83.                 }
  84.                 case '3' :
  85.                 {
  86.                     result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound3, 0, &channel);
  87.                     ERRCHECK(result);
  88.                     break;
  89.                 }
  90.             }
  91.         }
  92.         FMOD_System_Update(system);
  93.         {
  94.             unsigned int ms = 0;
  95.             unsigned int lenms = 0;
  96.             int          playing = 0;
  97.             int          paused = 0;
  98.             int          channelsplaying = 0;
  99.             if (channel)
  100.             {
  101.                 FMOD_SOUND *currentsound = 0;
  102.                 result = FMOD_Channel_IsPlaying(channel, &playing);
  103.                 if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  104.                 {
  105.                     ERRCHECK(result);
  106.                 }
  107.                 result = FMOD_Channel_GetPaused(channel, &paused);
  108.                 if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  109.                 {
  110.                     ERRCHECK(result);
  111.                 }
  112.                 result = FMOD_Channel_GetPosition(channel, &ms, FMOD_TIMEUNIT_MS);
  113.                 if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  114.                 {
  115.                     ERRCHECK(result);
  116.                 }
  117.                 FMOD_Channel_GetCurrentSound(channel, &currentsound);
  118.                 if (currentsound)
  119.                 {
  120.                     result = FMOD_Sound_GetLength(currentsound, &lenms, FMOD_TIMEUNIT_MS);
  121.                     if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  122.                     {
  123.                         ERRCHECK(result);
  124.                     }
  125.                 }
  126.             }
  127.             result = FMOD_Sound_GetLength(sound1, &lenms, FMOD_TIMEUNIT_MS);
  128.             if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  129.             {
  130.                 ERRCHECK(result);
  131.             }
  132.             FMOD_System_GetChannelsPlaying(system, &channelsplaying);
  133.             printf("Time %02d:%02d:%02d/%02d:%02d:%02d : %s : Channels Playing %2d\r", ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100, paused ? "Paused " : playing ? "Playing" : "Stopped", channelsplaying);
  134.             fflush(stdout);
  135.         }
  136.         Sleep(10);
  137.     } while (key != 27);
  138.     printf("\n");
  139.     /*
  140.         Shut down
  141.     */
  142.     result = FMOD_Sound_Release(sound1);
  143.     ERRCHECK(result);
  144.     result = FMOD_Sound_Release(sound2);
  145.     ERRCHECK(result);
  146.     result = FMOD_Sound_Release(sound3);
  147.     ERRCHECK(result);
  148.     result = FMOD_System_Close(system);
  149.     ERRCHECK(result);
  150.     result = FMOD_System_Release(system);
  151.     ERRCHECK(result);
  152.     return 0;
  153. }
  154. ·
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-12-21 19:58:07 | 显示全部楼层
什么问题?
$ sudo ./playsound
FMOD error! (56) Error initializing output device.
回复 支持 反对

使用道具 举报

发表于 2006-12-21 20:19:05 | 显示全部楼层
补充说明我的环境
我用的是ubuntu
用的是fmodapi40601linux64
管方例子没有这个问题。。。。。
好象是和gtk一起用就不行了?


~$ gcc -v
使用内建 specs。
目标:x86_64-linux-gnu
配置为:../src/configure -v --enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.0 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-java-awt=gtk-default --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0-1.4.2.0/jre --enable-mpfr --disable-werror --enable-checking=release x86_64-linux-gnu
线程模型:posix
gcc 版本 4.0.3 (Ubuntu 4.0.3-1ubuntu5)


$ uname -a
Linux myubuntu 2.6.15-26-amd64-generic #1 SMP PREEMPT Thu Aug 3 02:52:35 UTC 2006 x86_64 GNU/Linux
回复 支持 反对

使用道具 举报

发表于 2006-12-23 05:45:45 | 显示全部楼层
呵呵,C++高手哦都是!!!!!
回复 支持 反对

使用道具 举报

发表于 2006-12-24 01:49:57 | 显示全部楼层
Post by chezz99
呵呵,C++高手哦都是!!!!!

明显是C程序。。。(吹毛求疵下…… )
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表