基於GStreamer編寫Mp3播放器

1、簡介html

做者系統爲CentOS6,本文在此基礎上對Mp3播放器進行開發,須要使用mp3解碼庫libmad和gstreamer0.10-plugins-ugly,詳細步驟以下。linux

 

2、操做步驟ios

1)下載centos

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

 

3、源碼oop

/**
 * 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;
}

編譯ui

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

運行spa

./mp3 xxx.mp3

 

相關文章
相關標籤/搜索