Linux基于Gstreamer的傻瓜组装式mp3播放

#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 <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); //等同于callback处理函数
    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 $(pkg-config --cflags --libs gstreamer-0.10) test.c -o test `pkg-config --libs glib-2.0` `pkg-config --libs gstreamer-0.10`

$ ./test test.mp3

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值