免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3828 | 回复: 4
打印 上一主题 下一主题

怎样写一段代码播放MP3 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-07 09:31 |只看该作者 |倒序浏览
本帖最后由 fanimeng 于 2011-03-07 09:40 编辑

纠结了好几天了,有没有什么库或是对声卡操作也行,可以支持播放mp3,怎样写一段代码播放指定的MP3文件,谢了!!!

论坛徽章:
0
2 [报告]
发表于 2011-03-07 11:10 |只看该作者
调用madpaly

论坛徽章:
0
3 [报告]
发表于 2011-03-07 14:11 |只看该作者
谢了

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2011-03-08 11:47 |只看该作者

  1. leos@leos-laptop:gstreamer-code $ cat gst.c
  2. /*
  3. * gcc gst.c -o gst `pkg-config --libs --cflags gstreamer-0.10`
  4. * */
  5. #include <gst/gst.h>
  6. #include <glib.h>

  7. GstElement *pipeline;//定义组件
  8. gchar *curtime,*altime;

  9. //定义消息处理函数,
  10. static gboolean bus_call(GstBus *bus,GstMessage *msg,gpointer data)
  11. {
  12.     GMainLoop *loop = (GMainLoop *) data;//这个是主循环的指针,在接受EOS消息时退出循环
  13.     switch (GST_MESSAGE_TYPE(msg))
  14.     {
  15.         case GST_MESSAGE_EOS:
  16.             g_print("End of stream\n");
  17.             g_main_loop_quit(loop);
  18.             break;
  19.         case GST_MESSAGE_ERROR:
  20.         {
  21.             gchar *debug;
  22.             GError *error;

  23.             gst_message_parse_error(msg,&error,&debug);
  24.             g_free(debug);
  25.             g_printerr("ERROR:%s\n",error->message);
  26.             g_error_free(error);
  27.             g_main_loop_quit(loop);
  28.             break;
  29.         }
  30.         default:
  31.              break;
  32.     }
  33.     return TRUE;
  34. }
  35. gboolean hello (gpointer data)
  36. {
  37.     GstFormat fm = GST_FORMAT_TIME;
  38.     gint64 pos,len;
  39.         gst_element_query_duration (pipeline, &fm, &len);
  40.     g_print("length == %"G_GINT64_FORMAT,len);
  41.     g_print("\n");
  42.     gst_element_query_position (pipeline, &fm, &pos);
  43.     g_print("Current ... is %"G_GINT64_FORMAT,pos);
  44.     g_print("\n");
  45.     g_print("rate of progress : %f%\n",1.0*pos/len*100);
  46.     g_print("===================================\n");
  47.     g_snprintf(curtime, 24, "%u:%02u:%02u", GST_TIME_ARGS(pos));
  48.     g_snprintf(altime, 24, "%u:%02u:%02u", GST_TIME_ARGS(len));
  49.     g_print("The time is %s / %s\n",curtime,altime);

  50.     return TRUE;//继续往下执行,返回FALSE 不再继续
  51. }
  52. int main(int argc,char *argv[])
  53. {
  54.     GMainLoop *loop;
  55.     GstBus *bus;
  56.     curtime = (gchar *)malloc(25);
  57.     altime = (gchar *)malloc(25);

  58.     gst_init(&argc,&argv);
  59.     loop = g_main_loop_new(NULL,FALSE);//创建主循环,在执行 g_main_loop_run后正式开始循环

  60.     if(argc != 2)
  61.     {
  62.         g_printerr("Usage:%s <mp3 filename>\n",argv[0]);
  63.         return -1;
  64.     }
  65.     //创建管道和组件
  66.     pipeline = gst_element_factory_make("playbin","play");

  67.     if(!pipeline){
  68.         g_printerr("One element could not be created.Exiting.\n");
  69.         return -1;
  70.     }
  71.     //设置 source的location 参数。即 文件地址.
  72.     //g_object_set(G_OBJECT(source),"location",argv[1],NULL);
  73.     g_object_set(G_OBJECT(pipeline),"uri",argv[1],NULL);
  74.     //得到 管道的消息总线
  75.     bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
  76.     //添加消息监视器
  77.     gst_bus_add_watch(bus,bus_call,loop);
  78.     gst_object_unref(bus);

  79.     //开始播放
  80.     gst_element_set_state(pipeline,GST_STATE_PLAYING);
  81.     g_print("\nRunning\n");

  82.     g_timeout_add(1000,hello,NULL);

  83.     //开始循环
  84.     g_main_loop_run(loop);
  85.     g_print("Returned,stopping playback\n");
  86.     gst_element_set_state(pipeline,GST_STATE_NULL);
  87.     gst_object_unref(GST_OBJECT(pipeline));
  88.     return 0;
  89. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2011-03-11 20:35 |只看该作者
多谢各位!

我现在用SDL_mixer库,已经解决此问题了!  SDL_mixer库很强大,跨平台的,支持多种音频格式
(Wav,MP3,OGG,FAC,MIDI)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP