1

我正在构建一个基于 gstreamer-0.10 的音乐播放器。我能够成功播放,但是当我更改管道状态时出现问题。我已经发布了代码来初始化和启动下面的管道:

    void start_gstreamer()
{
    gst_init(0,NULL);//call to initialise gstreamer
    time_val=0;//set to default value
    volume = 1.0;//set volume to default value
    player = gst_element_factory_make ("playbin2", "player");//get pipeline
    equalizer = gst_element_factory_make ("equalizer-10bands", "equalizer");//get the 10band equalizer
    convert = gst_element_factory_make ("audioconvert", "convert");
    sink = gst_element_factory_make ("autoaudiosink", "audio_sink");//get the audio-sink
    if (!equalizer || !convert || !sink)//check is all elements were created
    {
        g_printerr ("Not all elements could be created.\n");
        //return -1;
    }
    //int i=0;
    /* Create the sink bin, add the elements and link them */
    bin = gst_bin_new ("audio_sink_bin");//get new bin
    gst_bin_add_many (GST_BIN (bin), equalizer, convert, sink, NULL);//add elements to bin
    if(!(gst_element_link_many (equalizer, convert, sink, NULL)))//link all elements
        g_print("Could not link all elements\n");
    pad = gst_element_get_static_pad (equalizer, "sink");//set equalizer to sink
    ghost_pad = gst_ghost_pad_new ("sink", pad);//get a ghost pad to sink
    gst_pad_set_active (ghost_pad, TRUE);
    gst_element_add_pad (bin, ghost_pad);//add ghost pad to the bin
    gst_object_unref (pad);//unreference pad
    gst_element_set_state (player, GST_STATE_READY);//set pipeline to ready state
    //gst_element_set_state (player, GST_STATE_PAUSED);
    /* Configure the equalizer */
    g_object_set (G_OBJECT (equalizer), "band0",(gdouble) 0.0, NULL);
    g_object_set (G_OBJECT (equalizer), "band1",(gdouble) 0.0, NULL);
    g_object_set (G_OBJECT (equalizer), "band2",(gdouble) 0.0, NULL);
    g_object_set (G_OBJECT (equalizer), "band3",(gdouble) 0.0, NULL);
    g_object_set (G_OBJECT (equalizer), "band4",(gdouble) 0.0, NULL);
    g_object_set (G_OBJECT (equalizer), "band5",(gdouble) 0.0, NULL);
    g_object_set (G_OBJECT (equalizer), "band6",(gdouble) 0.0, NULL);
    g_object_set (G_OBJECT (equalizer), "band7",(gdouble) 0.0, NULL);
    g_object_set (G_OBJECT (equalizer), "band8",(gdouble) 0.0, NULL);
    g_object_set (G_OBJECT (equalizer), "band9",(gdouble) 0.0, NULL);
    /* Set playbin2's audio sink to be our sink bin */
    g_object_set (GST_OBJECT (player), "audio-sink", bin, NULL);
}

上面的代码将初始化 gstreamer 管道。我正在使用playbin2。

void start_playbin (char *gargv1)
{
    time_t rawtime;
    struct tm * timeinfo;
    static gboolean i=TRUE;


    gchar* uri;//to hold the path temporarily
    gchar* dname;//to hold the directory name
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    printf ( "START:%s\n", asctime (timeinfo) );

    uri = (gchar *)g_malloc( sizeof("file://") + strlen(gargv1) + 1);//get complete path
    strcpy(uri,"file://");
    strcat(uri,gargv1);//add path with the file path
    dname = g_uri_escape_string(uri,G_URI_RESERVED_CHARS_ALLOWED_IN_PATH,TRUE);

    g_free(uri);
    uri = dname;

    g_object_set(player,"uri",uri,NULL);//set file path for playback in the pipeline
    g_print("\n\nPlaying %s\n", gargv1);
    g_free(uri);//free path
    /* start playback */

    gst_element_set_state (GST_ELEMENT (player), GST_STATE_READY);//set pipeline to ready state
    if(i==TRUE)
    {
        unsigned int count=0;
        while(gst_element_set_state (GST_ELEMENT (player), GST_STATE_PLAYING)!=GST_STATE_CHANGE_SUCCESS);
        //g_print("here:%d\n",count++);
        i=FALSE;    
    }
    else
        play_playbin();//start playback
    bus = gst_pipeline_get_bus(GST_PIPELINE(player));//get bus reference
    gst_bus_add_watch(bus,(GstBusFunc)cb_message,player);//add bus to be monitored by  process events
    g_object_unref(bus);//unreference bus

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    printf ( "AFTER START:%s\n", asctime (timeinfo) );
    }

我不得不使用 if(i == TRUE) 条件,因为管道第一次没有改变状态。

void stop_playbin()
{
    if(gst_element_set_state (GST_ELEMENT (player), GST_STATE_NULL)!=GST_STATE_CHANGE_SUCCESS)
        g_print("Playbin could not be stopped\n");
    //g_assert(gst_element_set_state (GST_ELEMENT (player),GST_STATE_NULL)==GST_STATE_CHANGE_SUCCESS);
}

void pause_playbin()
{
    if(gst_element_set_state (GST_ELEMENT (player), GST_STATE_PAUSED)!=GST_STATE_CHANGE_SUCCESS)
        g_print("Playbin could not be paused\n");
    //g_assert(gst_element_set_state (GST_ELEMENT (player), GST_STATE_PAUSED)==GST_STATE_CHANGE_SUCCESS);

}

void play_playbin()
{


    if(gst_element_set_state (GST_ELEMENT (player), GST_STATE_PLAYING)!=GST_STATE_CHANGE_SUCCESS)
        g_print("Playbin could not be played\n");
    //g_assert(gst_element_set_state (GST_ELEMENT (player),GST_STATE_PLAYING)==GST_STATE_CHANGE_SUCCESS);
}

上述函数分别执行播放、暂停和停止功能。问题是,当我将文件的路径发送到函数start_playbin()时,在将管道设置为就绪后,我无法更改文件的路径。我必须让它 1)准备好 2)暂停或播放(无法设置状态) 3)停止然后播放(设置状态成功)歌曲。

请帮我。如果我将管道设置为仅就绪但程序挂起,我不会收到任何错误。可能是什么问题呢?是否有任何特定的顺序来设置管道和设置文件的路径?

4

1 回答 1

2

请查看 gstreamer 附带的示例。几点:

  • 您需要将管道的状态设置回准备好才能更改 uri
  • 状态变化是异步的。请阅读 GstBus 并使用它。特别是不要在玩完后依附在公共汽车上。创建您的 playbin 管道并连接到总线。然后你可以去播放,总线会通知你播放开始的时间和结束的时间(除其他外)。

还可以考虑将 1.0 用于新应用程序,0.10 处于维护模式。

于 2013-08-29T19:39:53.977 回答