无法使用gstreamer apis播放.wav文件

时间:2010-09-07 05:41:10

标签: gstreamer

编写以下代码以播放.wav文件,但它似乎不起作用。 我想知道我是否遗漏了一些内容。

代码:

#include <gst/gst.h>
#include <glib.h>

int main(int argc , char *argv[])
{
GMainLoop *loop;
GstElement *source,*audioparser,*sink,*pipeline;
GstBus *bus;

gst_init(&argc,&argv);

// create a pipeline
loop = g_main_loop_new (NULL, FALSE);
pipeline = gst_pipeline_new ("wav-player");
source = gst_element_factory_make("filesrc","file-source");
audioparser = gst_element_factory_make("wavparse","wav-parser");
sink = gst_element_factory_make("alsasink","sink1");
g_object_set (G_OBJECT (source), "location", argv[1], NULL);
gst_element_set_state (pipeline, GST_STATE_NULL);
// set location to current sourceg_object_set(G_OBJECT(source),"location",argv[1],NULL);

// add elements to bin
gst_bin_add_many(GST_BIN(pipeline),source,audioparser,sink,NULL);

gst_element_link_many(source,audioparser,sink,NULL);

// create bus
bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
g_main_loop_run (loop);
return 1;
}

请使用以下命令编译:

 gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) wav.c -o wavparser

提前致谢

2 个答案:

答案 0 :(得分:5)

只需使用playbin2而不是手工制作的管道

即,将“pipeline = gst_pipeline_new()”中的所有内容替换为“gst_element_link_many”:

pipeline = gst_element_factory_make("playbin2", NULL);
g_object_set(pipeline, "uri", "file:///the/file/I/want.wav", NULL);

答案 1 :(得分:2)

也许playbin2正是你所需要的,但回答问题:wavparse没有静态src-pad,所以你必须在运行时处理来自这个元素的“pad-added”信号。像代码一样:

gst_bin_add_many (GST_BIN (pipeline), wavsrc, wavparse,audioconvert, audiosink, NULL);
g_object_set (G_OBJECT (wavsrc), "location", "sound.wav", NULL);
gst_element_link(wavsrc, wavparse);
gst_element_link(audioconvert, audiosink);
g_signal_connect (wavparse, "pad-added", G_CALLBACK (on_pad_added), audioconvert);

其中:wavsrc是filesrc,wavparse是wavparse,audioconvert是audioconvert,audiosink是alsasink(我不确定alsasink适合你,所以你可以选择另一个)

void on_pad_added (GstElement *src_element, GstPad *src_pad, gpointer data)
{
    g_print ("Linking dynamic pad...\n");

    GstElement *sink_element = (GstElement *) data; // is audioconvert
    GstPad *sink_pad = gst_element_get_static_pad (sink_element, "sink");
    gst_pad_link (src_pad, sink_pad);

    gst_object_unref (sink_pad);
    src_element = NULL; // yup, i don't want "unused" warning here
}