Unable to play .wav file using gstreamer apis

為{幸葍}努か 提交于 2019-12-23 02:17:32

问题


Following code is written to play a .wav file but it doesn't seem to work. I would like to know if i am missing something in it.

Code:

#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;
}

Please compile this using following command:

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

Thanks in advance


回答1:


Just use playbin2 instead of a hand-crafted pipeline

that is, replace everything from "pipeline = gst_pipeline_new()" to "gst_element_link_many" by:

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



回答2:


maybe playbin2 is exactly what you need, but answerring question: wavparse does not have static src-pad, so you must handle "pad-added" signal from this element on runtime. something like the code:

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);

where: wavsrc is filesrc, wavparse is wavparse, audioconvert is audioconvert, audiosink is alsasink (i'm not sure alsasink works for you, so you can choose another)

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
}


来源:https://stackoverflow.com/questions/3656051/unable-to-play-wav-file-using-gstreamer-apis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!