我编写了一个演示程序来执行 seek 。这是代码
#define BOOST_ALL_NO_LIB
#include <iostream>
#include <gst/gst.h>
#include <glib/gmain.h>
#include <boost/thread.hpp>
using namespace std;
int bus_state_changed(GstBus *bus, GstMessage *msg, gpointer loop)
{
std::cerr << "bus message: " << GST_MESSAGE_TYPE_NAME(msg) << std::endl;
GMainLoop *ptrLoop = (GMainLoop *) loop;
switch (GST_MESSAGE_TYPE(msg))
{
case GST_MESSAGE_EOS:
std::cout<<"End of stream\n";
g_main_loop_quit(ptrLoop);
break;
case GST_MESSAGE_ERROR:
{
GError *err=0;
gst_message_parse_error(msg, &err, 0);
std::cerr<< "GST_MESSAGE_ERROR: " << err->message << ": "
<< GST_MESSAGE_SRC_NAME(msg);
g_error_free(err);
g_main_loop_quit(ptrLoop);
break;
}
case GST_MESSAGE_STATE_CHANGED: {
GstState old_state, new_state;
gst_message_parse_state_changed (msg, &old_state, &new_state, NULL);
g_print ("Element %s changed state from %s to %s.\n",
GST_OBJECT_NAME (msg->src),
gst_element_state_get_name (old_state),
gst_element_state_get_name (new_state));
break;
}
default:
break;
}
return true;
}
int main()
{
GMainLoop *loop;
GstElement *pipeline=0;
GstBus *bus=0;
guint busWatchId;
GError *err = 0;
if(!gst_init_check(0, 0,&err))
{
std::string errmsg(err->message);
g_error_free(err);
}
loop = g_main_loop_new(NULL, FALSE);
//create a pipeline for audio/video playback
pipeline = gst_element_factory_make("playbin2", "player");
if(!pipeline)
{
std::cout<<"Can not get pipeline";
}
//get a bus to receive GSTMessages from pipeline.
bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
if(!bus)
{
std::cout<<"Can not get bus";
}
//when the state of bus changes call bus_state_changed function.
busWatchId = gst_bus_add_watch(bus, bus_state_changed, loop);
//make the pipeline ready to use.
gst_element_set_state(pipeline, GST_STATE_READY);
boost::thread gloopThread([&](){g_main_loop_run(loop);});
g_object_set(G_OBJECT(pipeline), "uri","file:///C:/song.mp3" , 0);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
std::cout<<"Seek will be perfomed now "<<std::endl;
gint64 pos =-1;
GstFormat fmt = GST_FORMAT_TIME;
while(true)
{
if (!gst_element_query_position(pipeline, &fmt, &pos))
{
std::cerr << "gst_element_query_position failed" << std::endl;
}
//std::cout<<"\nCurrent Position"<<pos<<std::endl;
if(pos > 2807641720) // 3 seconds
{
if (!gst_element_seek_simple(pipeline, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,44531201814 ))
{
std::cerr << "gst_element_seek failed" << std::endl;
}
GstStateChangeReturn r = gst_element_get_state(pipeline, 0, 0, -1);
std::cout << "state change return = " << r << std::endl;
if (!gst_element_query_position(pipeline, &fmt, &pos))
{
std::cerr << "gst_element_query_position failed 2" << std::endl;
}
//std::cout<<"\nCurrent Position :"<<pos<<std::endl;
break;
}
}
while(true)
{
gst_element_query_position(pipeline, &fmt, &pos);
//std::cout<<"\nCurrent Position"<<pos<<std::endl;
if(pos > 47531201814) // 3 seconds
{
gst_element_seek_simple(pipeline, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,50531201814 );
gst_element_query_position(pipeline, &fmt, &pos);
//std::cout<<"\nCurrent Position :"<<pos<<std::endl;
break;
}
}
while(true);
return 0;
}
这是我的 .pro 文件
TEMPLATE = app
CONFIG += console
CONFIG -= qt
SOURCES += main.cpp
QMAKE_CXXFLAGS += -std=c++0x
CONFIG += link_pkgconfig
PKGCONFIG += gstreamer-0.10
win32: INCLUDEPATH += c:\boost_1_48_0 \
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../boost_1_48_0/stage/lib/ -lboost_thread-vc100-mt-1_48
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../boost_1_48_0/stage/lib/ -lboost_thread-vc100-mt-gd-1_48
INCLUDEPATH += $$PWD/../../../boost_1_48_0/stage
DEPENDPATH += $$PWD/../../../boost_1_48_0/stage
运行代码时,歌曲播放良好,但触发搜索时存在搜索间隙。相同的代码在 linux 上完美运行。我无法理解问题出在哪里。谢谢