程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 基於GStreamer編寫Mp3播放器,gstreamer編寫mp3

基於GStreamer編寫Mp3播放器,gstreamer編寫mp3

編輯:關於C語言

基於GStreamer編寫Mp3播放器,gstreamer編寫mp3


一、簡介

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

 

二、操作步驟

1)下載

wget ftp://ftp.pbone.net/mirror/atrpms.net/el5-x86_64/atrpms/stable/libmad0-0.15.1b-4.el5.x86_64.rpm
wget ftp://ftp.pbone.net/mirror/atrpms.net/el5-x86_64/atrpms/stable/libmad-0.15.1b-4.el5.x86_64.rpm
wget ftp://195.220.108.108/linux/centos/6.7/os/x86_64/Packages/libid3tag-0.15.1b-11.el6.x86_64.rpm
wget ftp://ftp.pbone.net/mirror/atrpms.net/el6-x86_64/atrpms/stable/libdvdread3-0.9.7-5.el6.x86_64.rpm
wget ftp://195.220.108.108/linux/Mandriva/official/2007.0/x86_64/media/main/release/gstreamer0.10-plugins-ugly-0.10.4-1mdv2007.0.x86_64.rpm

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

 

參考:http://blog.chinaunix.net/uid-25885064-id-3450774.html

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved