A simple media player in Linux based on gstreamer

Source: Internet
Author: User
Tags gtk gstreamer

It is also based on gstreamer, a relatively simple media player in Linux,

The Code is as follows:

Main. c

/** Main. C * simple Media Player Based on gstreamer and GTK */# include <GST/GST. h> # include <gdk/gdkx. h> # include <GTK/GTK. h> # include <GST/interfaces/xoverlay. h> # include <string. h> # include "Main. H "static gtkwidget * main_window; static gtkwidget * play_button; static gtkwidget * pause_button; static gtkwidget * stop_button; static gtkwidget * status_label; static gtkwidget * time_label; static gtkwidget * seek_scale; Static gtkwidget * video_output; static gpointer window; static ststelement * play = NULL; static ststelement * bin; static guint timeout_source = 0; static char * current_filename = NULL; gboolean no_seek = false; // open the file static void file_open (gtkaction * Action) {gtkwidget * file_chooser = delimiter ("Open File", gtk_window (main_window), gtk_file_chooser_action_open, gtk_stock_cancel, gtk_res Ponse_cancel, gtk_stock_open, gtk_response_accept, null); If (gtk_dialog_run (gtk_dialog (file_chooser) = folder) {char * filename; filename = folder (file_chooser )); // g_signal_emit_by_name (g_object (stop_button), "clicked"); If (current_filename) g_free (current_filename); current_filename = filename; If (load_file (filename) gtk_widget_set_sensitive (Gtk_widget (play_button), true);} gtk_widget_destroy (file_chooser);} // exit static void file_quit (gtkaction * Action) {gtk_main_quit ();} // about static void help_about (gtkaction * Action) {gtkwidget * about_dialog = upload (); outputs (about_dialog), "mediaplayer"); outputs (gtk_about_dialog (about_dialog ), "0.0.0"); gtk_about_dialog_set_cop Yright (gtk_about_dialog (about_dialog), "Copyright @ 2011, Figo"); gtk_dialog_run (gtk_dialog (about_dialog); gtk_widget_destroy (about_dialog );} static gtkactionentry mainwindow_action_entries [] = {"filemenu", "null", "file" },{ "openfile", gtk_stock_open, "open (o )", "<control> O", "open a file for playback", g_callback (file_open)}, {"quitplayer", gtk_stock_quit, "exit (q )", "<control> q", "quit the med IA player ", g_callback (file_quit)}, {" helpmenu "," null "," help "},{" helpabout ", gtk_stock_about," about ","", "about the media player", g_callback (help_about) }}; static void play_clicked (gtkwidget * widget, gpointer data) {If (current_filename) {If (play_file ()) {gtk_widget_set_sensitive (gtk_widget (stop_button), true); gtk_widget_set_sensitive (gtk_widget (pause_button), true); // g_print ("play was Pressed \ n "); gui_status_update (state_play); g_print (" Play \ n ");} else {g_print (" failed to play \ n ");}}} static void pause_clicked (gtkwidget * widget, gpointer data) {If (complete (gtk_toggle_button (widget) {publish (play, stst_state_paused); Publish statechange; statechange = publish (play, stst_state_paused); gui_status_update (state_pause); g_pr INT ("Pause \ n"); // g_print ("pause was pressed \ n");} else {// g_print ("pause was pressed \ n "); ststst_element_set_state (play, stst_state_playing); gui_status_update (state_play); g_print ("resume to play \ n") ;}} static void stop_clicked (gtkwidget * widget, gpointer data) {If (timeout_source) g_source_remove (timeout_source); timeout_source = 0; // g_print ("Stop was pressed \ n"); maid (play, stst_st Ate_null); // ststst_object_unref (maid (play); // The gui_status_update (state_stop) pipe does not need to be destroyed; g_print ("deleting pipeline \ n "); g_print ("Stop \ n");}/* handler for user moving seek bar */static void seek_value_changed (gtkrange * range, gpointer data) {If (no_seek) return; gdouble val = gtk_range_get_value (range); seek_to (VAL);} gtkwidget * build_gui () {gtkwidget * main_vbox; gtkwidget * status_hbox; gtkwidget * C Ontrols_hbox; gtkwidget * Rule; gtkactiongroup * Actiongroup; gtkuimanager * ui_manager; Actiongroup = gtk_action_group_new ("mainwindowactiongroup"); rule (Actiongroup, rule, g_n_elements (rule), null ); ui_manager = gtk_ui_manager_new (); gtk_ui_manager_insert_action_group (ui_manager, Actiongroup, 0); gtk_ui_manager_ad D_ui_from_string (ui_manager, "<UI>" "<menubar name = 'mainmenu '>" "<menu action = 'filemenus'>" "<menuitem action = 'openfile'/>" "<separator name = 'fsep1'/> "" <menuitem action = 'quitplayer'/> "" </menu> "" <menu action = 'helpmenu'> "" <menuitem action =' helpabout '/> "" </menu> "" </menubar> "" </UI> ", -1, null); // create the master gtkvbox. others are in it. // 0: the height of each component may be different. 6: the distance between components is 6 pixels. main_vbox = gtk_vbox_new (0, 0 );// Add the menu bar gtk_box_pack_start (gtk_box (main_vbox), gtk_ui_manager_get_widget (ui_manager, "/UI/mainmenu"), false, false, 0 ); // * // video_output = gtk_drawing_area_new (); gtk_box_pack_start (gtk_box (main_vbox), video_output, true, true, 0); // video_output, 0x200, 0x100); gtk_widget_set_size_request (video_output, 672,378); gtk_widget_show (video_output); * // slider Control Seek_scale = hour (0,100, 1); hour (gtk_scale (seek_scale), false); hour (gtk_range (seek_scale), gtk_update_discontinuous); g_signal_connect (g_object (seek_scale ), "value-changed", g_callback (seek_value_changed), null); Round (gtk_box (main_vbox), seek_scale, false, false, 0); // controls_hbox = gtk_hbox_new (true, 6); gtk_box_pack_start_defaults (gtk_box (main_vbox), controls_hbox); // play_button = playback (gtk_stock_media_play); // set the "sensitive" attribute. False indicates that it is gray, does not respond to mouse and keyboard events (play_button, false); g_signal_connect (g_object (play_button), "clicked", g_callback (play_clicked), null); gtk_box_pack_start_defaults (gtk_box (controls_hbox ), play_button); // click the pause button. To stop the task from being pressed, use gtktogg. Lebutton pause_button = Success (gtk_stock_media_pause); // set the button to solid (gtk_button (pause_button), true); success (pause_button, false); g_signal_connect (g_object (pause_button ), "clicked", g_callback (pause_clicked), null); gtk_box_pack_start_defaults (gtk_box (controls_hbox), pause_button); // stop button stop_button = gtk_button_new_from_stock (Gtk_stock_media_stop); values (stop_button, false); g_signal_connect (g_object (stop_button), "clicked", g_callback (stop_clicked), null); values (gtk_box (controls_hbox), stop_button ); // status_hbox = gtk_hbox_new (true, 0); gtk_box_pack_start (gtk_box (main_vbox), status_hbox, false, false, 0 ); // status label status_label = gtk_label_new ("<B> stopped </B>"); gtk_la Struct (gtk_label (status_label), true); struct (gtk_misc (status_label), 0.0, 0.5); gtk_box_pack_start (gtk_box (status_hbox), status_label, true, true, 0 ); // Time Label time_label = gtk_label_new ("00:00:00"); hour (gtk_misc (time_label), 0.5, 1.0); gtk_box_pack_start (gtk_box (status_hbox), time_label, true, 0); Return main_vbox;}/* void stop_playback () {If (T Imeout_source) g_source_remove (timeout_source); timeout_source = 0; If (play) {maid (play, stst_state_null); maid (play); play = NULL ;} gui_status_update (state_stop);} * // destory main windowstatic void destroy (gtkwidget * widget, gpointer data) {gtk_main_quit ();} // update the playback time void gui_update_time (const gchar * time, const gint64 position, const gint64 length) {GTK _ Label_set_text (gtk_label (time_label), time); If (length> 0) {no_seek = true; gtk_range_set_value (gtk_range (seek_scale), (gdouble) position/(gdouble) length) * 100.0); no_seek = false ;}// update the playback status void gui_status_update (playerstate state) {Switch (state) {Case state_stop: gtk_widget_set_sensitive (gtk_widget (stop_button), false ); gtk_widget_set_sensitive (gtk_widget (pause_button), false); gtk_lab El_set_markup (gtk_label (status_label), "<B> stopped </B>"); gtk_range_set_value (gtk_range (seek_scale), 0.0); gtk_label_set_text (gtk_label (time_label ), "00:00:00"); break; Case state_play: outputs (gtk_widget (stop_button), true); outputs (gtk_widget (pause_button), true); gtk_label_set_markup (gtk_label (status_label ), "<B> playing </B>"); break; Case state_pause: gtk_label_set_mar Kup (gtk_label (status_label), "<B> paused </B>"); break; default: Break ;}} static gboolean bus_callback (ststbus * bus, ststmessage * message, gpointer data) {Switch (maid (Message) {Case maid error: {gerror * err; gchar * debug; maid (message, & err, & Debug); g_print ("error: % s \ n ", err-> message); g_error_free (ERR); g_free (Debug); gtk_main_quit (); break;} case ststst_message _ EOS: g_print ("End of stream \ n"); // stop_playback (); // stop_clicked (gtkwidget * widget, gpointer data); If (timeout_source) g_source_remove (timeout_source); timeout_source = 0; maid (play, maid); gui_status_update (state_stop); break; default: break;} return true ;} static gboolean build_gstreamer_pipeline (const gchar * URI) {// create a playbin element play = maid ("Media Player "); bin = maid (" playbin "," bin "); maid (play), BIN); {maid * bus; bus = maid (play); maid (bus, bus_callback, null); maid (bus); g_object_unref (bus);} g_object_set (g_object (BIN), "Uri", Uri, Uri, null); Return true;} // Load file to playgboolean load_file (const gchar * URI) {If (build_gstreamer_pipeline (URI) return true; Return false;} static gboolean update_time_callback (ststelement * pipeline) {ststformat FMt = maid; gint64 position; gint64 length; gchar time_buffer [25]; If (maid (pipeline, & FMT, & position) & maid (pipeline, & FMT, & length) {g_snprintf (time_buffer, 24, "% u: % 02u: % 02u", maid (position )); gui_update_time (time_buffer, position, length);} re Turn true;} gboolean play_file () {If (play) {/* start playing */maid (play, stst_state_playing); gui_status_update (state_play ); /* connect a callback to trigger every 200 milliseconds to * update the GUI with the playback progress. we remember * the ID of this source so that we can remove it when we stop * Playing */timeout_source = g_timeout_add (200, (gsourcefunc) update_time_call Back, play); Return true;} return false;}/* attempt to seek to the given percentage through the file */void seek_to (gdouble percentage) {maid = maid; gint64 length;/* if it seems safe to attempt a seek... */If (play & ststst_element_query_duration (play, & FMT, & length )){/*... calculate where to seek to */gint64 target = (gdouble) length * (percentage/100.0 ));/*... and ATT Empt the seek */If (! Ststst_element_seek (play, 1.0, stst_format_time, hour, average, target, stst_seek_type_none, stst_clock_time_none) g_print ("failed to seek to desired position \ n ");}} int main (INT argc, char * argv []) {// initialize GTK + gtk_init (& argc, & argv); maid (& argc, & argv ); // create a window main_window = gtk_window_new (gtk_window_toplevel); // set the window title gtk_window_set_title (gtk_window (main_window), "mediaplayer"); // destroy g_signal_connect (g_object (main_window ), "Destroy", g_callback (destroy), null); // create the main GUI gtk_container_add (gtk_container (main_window), build_gui (); // display gtk_widget_show_all (gtk_widget (main_window )); // start the main loop gtk_main (); Return 0 ;}

Main. h

/* * main.h */#ifndef MAIN_H#define MAIN_H#include <gtk/gtk.h>typedef enum {    STATE_STOP,    STATE_PLAY,    STATE_PAUSE} PlayerState;void gui_status_update(PlayerState state);void gui_update_time(const gchar *time, const gint64 position, const gint64 length);// void gui_update_metadata(const gchar *title, const gchar *artist);gboolean load_file(const gchar *uri);void seek_to(gdouble percentage);#endif

Makifile

CFLAGS=`pkg-config --cflags gtk+-2.0 gstreamer-0.10`LIBS=`pkg-config --libs gtk+-2.0 gstreamer-0.10`all: playerplayer: gcc -o MediaPlayer main.c main.h $(LIBS) $(CFLAGS) -l gstinterfaces-0.10clean:rm *.o player

Compile, input in the terminal:

make

Generate an executable file and run:

./MediaPlayer 

Running interface:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.