一、简介

作者系统为CentOS6,本文在此基础上对Mp3播放器进行开发,需要使用mp3解码库libmad和gstreamer0.10-plugins-ugly,详细步骤如下。

 

二、操作步骤

1)下载

2)安装

yum install gstreamer-devel
rpm -ivh libmad0-0.15.1b-4.el5.x86_64.rpm libmad-0.15.1b-4.el5.x86_64.rpm
rpm -ivh libid3tag-0.15.1b-11.el6.x86_64.rpm libdvdread3-0.9.7-5.el6.x86_64.rpm
rpm -ivh gstreamer0.10-plugins-ugly-0.10.4-1mdv2007.0.x86_64.rpm

 

三、源码

/**
 * http://files.cnblogs.com/files/274914765qq/mp3.zip
 */
 
#include <gst/gst.h>
#include <glib.h> //消息处理函数
static gboolean bus_call(GstBus * bus, GstMessage * msg, gpointer data)
{
GMainLoop *loop = (GMainLoop *) data; switch (GST_MESSAGE_TYPE(msg))
{ case GST_MESSAGE_EOS:
g_print("End of stream\n");
g_main_loop_quit(loop);
break;
case GST_MESSAGE_ERROR:
{
gchar *debug;
GError *error;
gst_message_parse_error(msg, &error, &debug);
g_free(debug);
g_printerr("ERROR:%s\n", error->message);
g_error_free(error);
g_main_loop_quit(loop);
break;
}
default:
break;
} return TRUE;
} int main(int argc, char *argv[])
{
GMainLoop *loop;
GstElement *pipeline, *source, *decoder, *sink; //定义组件
GstBus *bus; gst_init(&argc, &argv); loop = g_main_loop_new(NULL, FALSE); //创建主循环,在执行 g_main_loop_run后正式开始循环
if (argc != 2)
{
g_printerr("Usage:%s \n", argv[0]);
return -1;
} //创建管道和组件
pipeline = gst_pipeline_new("audio-player");
source = gst_element_factory_make("filesrc", "file-source");
decoder = gst_element_factory_make("mad", "mad-decoder");
sink = gst_element_factory_make("autoaudiosink", "audio-output");
if (!pipeline || !source || !decoder || !sink)
{
g_printerr("One element could not be created.Exiting.\n");
return -1;
} //设置source的location参数,即文件地址
g_object_set(G_OBJECT(source), "location", argv[1], NULL); //得到管道的消息总线
bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); //添加消息监视器
gst_bus_add_watch(bus, bus_call, loop);
gst_object_unref(bus); //把组件添加到管道中.管道是一个特殊的组件,可以更好的让数据流动
gst_bin_add_many(GST_BIN(pipeline), source, decoder, sink, NULL); //依次连接组件
gst_element_link_many(source, decoder, sink, NULL); //开始播放
gst_element_set_state(pipeline, GST_STATE_PLAYING); g_print("Running\n"); //开始循环
g_main_loop_run(loop); g_print("Returned,stopping playback\n");
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(GST_OBJECT(pipeline)); return 0;
}

编译

gcc -Wall -g -o mp3 mp3.c `pkg-config --cflags --libs gstreamer-0.10`

运行

./mp3 xxx.mp3

 

最新文章

  1. 【myEcplise2015 更换主题+字体颜色】
  2. 学习总结 HTML简单应用
  3. ASP.NET的错误处理机制之一(概念)
  4. hdu 2964 Prime Bases(简单数学题)
  5. mysql数据库之索引和分析索引
  6. 【HTML】Intermediate5:Definition Lists
  7. 在 Xcode中 修改文件中自动创建的Created by和Copyright
  8. mysql用户权限分配及主从同步复制
  9. ASP.NET控件GridView的使用&amp; Xml操作注意事项
  10. oracle数据库中的trim不起作用
  11. juniper srx 配置
  12. Oracle 《积累章》 根据身份证号码更新当前出生日期
  13. BZOJ3199 SDOI2013 逃考 半平面交、最短路
  14. tensorflow 笔记13:了解机器翻译,google NMT,Attention
  15. sublime text3 license
  16. 2018.07.06 BZOJ1208: HNOI2004宠物收养所(非旋treap)
  17. java版云笔记(九)之动态sql
  18. 文艺青年装B指南
  19. Linux - 进程控制 代码(C)
  20. inode与block

热门文章

  1. Count On A Tree II.
  2. HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)
  3. LeetCode Longest Continuous Increasing Subsequence
  4. 取余运算(mod)(分治)
  5. element table固定表头,表的高度自适应解决方法
  6. hdu 3932 Groundhog Build Home——模拟退火
  7. ov2640数据
  8. mycat 新增分片和字符集
  9. jdbcTemplate学习(一)
  10. RandomForestClassifier(随机森林检测每个特征的重要性及每个样例属于哪个类的概率)