Ubuntu下使用GStreamer开发简单的mp3播放器

一、GStreamer安装
$ sudo apt-get install libgstreamer0.10-dev gstreamer-tools gstreamer0.10-tools gstreamer0.10-doc
$ sudo apt-get install gstreamer0.10-plugins-base gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-plugins-bad-multiverse

还可以再安装如下gst插件:
gstreamer0.10-tools
gstreamer0.10-x
gstreamer0.10-plugins-base
gstreamer0.10-plugins-good
gstreamer0.10-plugins-ugly
gstreamer0.10-plugins-bad
gstreamer0.10-ffmpeg
gstreamer0.10-alsa
gstreamer0.10-schroedinger
gstreamer0.10-pulseaudio

有可能需要安装的软件:
sudo apt-get install bison
sudo apt-get install flex
sudo apt-get install zlib1g
mad解码插件
apt-get install libmad0-dev
apt-get install gstreamer0.10-plugins-ugly

二、GStreamer测试代码
#include <stdio.h>  
#include <gst/gst.h>  
  
int main (int   argc,   char *argv[])  
{  
  const gchar *nano_str;  
  guint major, minor, micro, nano;  
  
  gst_init (&argc, &argv);  
  
  gst_version (&major, &minor, µ, &nano);  
  
  if (nano == 1)  
    nano_str = "(CVS)";  
  else if (nano == 2)  
    nano_str = "(Prerelease)";  
  else  
    nano_str = "";  
  
  printf ("This program is linked against GStreamer %d.%d.%d %s\n",  
          major, minor, micro, nano_str);  
  
  return 0;  
}


编译运行
# gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) hello.c -o hello
./hello
运行结果:


This program is linked against GStreamer 0.10.22


三、命令行mp3播放器测试
#include <gst/gst.h>  
#include <glib.h>  
//定义消息处理函数,  
static gboolean bus_call(GstBus *bus,GstMessage *msg,gpointer data)  
{  
    GMainLoop *loop = (GMainLoop *) data;//这个是主循环的指针,在接受EOS消息时退出循环  
    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 <mp3 filename>\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 main.c -o mp3player  $(pkg-config --cflags --libs gstreamer-0.10)


注意!!!
很多地方写的这条命令 :
 gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) -g test2.c -o test2 ,根本编译不过!)


编译时借助了 pkg-config 
pkg-config --cflags --libs gstreamer-0.10 会把gstreamer-0.10编译所依赖的库的路径 
头文件的路径全部帮你找出来,不用你再依次写出。
(上面这条pkg-config --cflags --libs gstreamer-0.10 就相当于:
-pthread -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -I/usr/include/gstreamer-0.10 -I/usr/include/libxml2  -pthread -L/usr/lib/i386-linux-gnu -lgstreamer-0.10 -lgobject-2.0 -lgmodule-2.0 -lxml2 -lgthread-2.0 -lrt -lglib-2.0)


./mp3player  ./abc.mp3


四、播放视频
播放视频:
Ubuntu 12.04
$ gst-launch-0.10 filesrc location=/tmp/video_stream_pipe ! decodebin ! ffmpegcolorspace ! videoscale ! ximagesink sync=false


Ubuntu 13.10
$ gst-launch-1.0 filesrc location=/tmp/video_stream_pipe ! decodebin ! videoconvert ! xvimagesink sync=false
gst-launch-1.0 filesrc location=/tmp/video_stream_pipe ! decodebin ! videoconvert ! ximagesink sync=false

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值