现在的位置: 首页 > 综合 > 正文

使用GStreamer的插件playbin开发一个简单的媒体播放器 视频与音乐

2012年08月05日 ⁄ 综合 ⁄ 共 2215字 ⁄ 字号 评论关闭

原文出处:http://blog.163.com/lixiangqiu_9202/blog/static/535750372012111911544314/

首先需要安装GStreamer开发环境

至于如何安装请另行搜索

下面只是一个简单的示例,其中有我注释掉的一些代码

这些注释掉的代码有些部分是我练习的时候用的,有些部分是为设置打开文件uri路径设置的

但这样设置了以后就无法播放来自网络的媒体了,所以给注释掉了

下面上代码

#include <gst/gst.h>
#include <string.h>
void error_quit(GstBus *bus,GstMessage *message,GMainLoop *loop)
{
    GError *error;
    gst_message_parse_error(message,&error,NULL);
    printf("Error:%s\n",error->message);
    g_main_loop_quit(loop);
}
void end_of_streamer(GstBus *bus,GstMessage *message,GMainLoop *loop)
{
    printf("End Of Streamer. . .\n");
    g_main_loop_quit(loop);
}
void print_pos(GstElement *pipe)
{
    GstFormat fm=GST_FORMAT_TIME;
    gint64 pos,len;
    gst_element_query_position(pipe,&fm,&pos);
    gst_element_query_duration(pipe,&fm,&len);
    printf("Time:%u:%02u:%02u/%u:%02u:%02u:%02u\n",GST_TIME_ARGS(pos),GST_TIME_ARGS(len));
}
int main(int argc,char **argv)
{
    GMainLoop *loop;
    GstElement *playpipe;
    //GstElement *source;
    GstBus *bus;
    //gchar *file_name;
    gst_init(&argc,&argv);
    loop=g_main_loop_new(NULL,FALSE);
    playpipe=gst_element_factory_make("playbin","play-source");
    //playpipe=gst_pipeline_new("play-pipe");
    //gst_bin_add(GST_BIN(playpipe),source);
    //gst_element_link(playpipe,source);
    //g_object_set(G_OBJECT(playpipe),"location",argv[1],NULL);
    //file_name=malloc(sizeof(gchar)*7+strlen(argv[1])+1);
    //strcpy(file_name,"file://");
    //strcat(file_name,argv[1]);
    g_object_set(G_OBJECT(playpipe),"uri",argv[1],NULL);
    bus=gst_pipeline_get_bus(GST_PIPELINE(playpipe));
    gst_bus_add_signal_watch(bus);
    g_signal_connect(G_OBJECT(bus),"message::error",G_CALLBACK(error_quit),loop);
    g_signal_connect(G_OBJECT(bus),"message::eos",G_CALLBACK(end_of_streamer),loop);
    printf("Start. . .\n");
    gst_element_set_state(playpipe,GST_STATE_PLAYING);
    g_timeout_add(1000,(void *)print_pos,playpipe);
    g_main_loop_run(loop);
    gst_element_set_state(playpipe,GST_STATE_NULL);
    return 0;
}

编译

gcc -o playbin playbin.c `pkg-config --cflags --libs gstreamer-0.10`

要注意:执行时媒体路径设置如下:

#./playbin file:///root/Media/jiangnan.avi

GStreamer 支持"file:///<path>/<file>", "http://<host>/<path>/<file>", "mms://<host>/<path>/<file>"等路径。
为了让源元件或者接收元件支持特定的 URI,使用函数gst_element_make_from_uri ()可以达到此目的。其中对
源元件使用GST_URI_SRC类型,接收元件使用GST_URI_SINK类型。

抱歉!评论已关闭.