Use GStreamer to develop a simple MP3 player under Ubuntu

Source: Internet
Author: User
Tags gstreamer
First, GStreamer installation
$ sudo apt-get install Libgstreamer0.10-dev gstreamer-tools gstreamer0.10-tools Gstreamer0.10-doc
$ sudo apt-get install gstreamer0.10-plugins-base gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly Gstreamer0.10-plugins-bad Gstreamer0.10-plugins-bad-multiverse

You can also install the following GST plugins:
Gstreamer0.10-tools
Gstreamer0.10-x
Gstreamer0.10-plugins-base
Gstreamer0.10-plugins-good
gstreamer0.10-plugins-ugly
Gstreamer0.10-plugins-bad
Gstreamer0.10-ffmpeg
Gstreamer0.10-alsa
Gstreamer0.10-schroedinger
Gstreamer0.10-pulseaudio

There may be software that needs to be installed:
sudo apt-get install Bison
sudo apt-get install Flex
sudo apt-get install zlib1g
Mad Decode Plugin
Apt-get Install Libmad0-dev
Apt-get Install gstreamer0.10-plugins-ugly

Second, GStreamer test code
#include <stdio.h>
#include <gst/gst.h>

int main (int argc, char *argv[])
{
Const Gchar *NANO_STR;
Guint major, minor, Micro, nano;

Gst_init (&AMP;ARGC, &AMP;ARGV);

Gst_version (&major, &minor,µ, &nano);

if (nano = = 1)
Nano_str = "(CVS)";
else if (nano = = 2)
Nano_str = "(prerelease)";
Else
Nano_str = "";

printf ("This program is linked against GStreamer%d.%d.%d%s\n",
Major, minor, Micro, NANO_STR);

return 0;
}


Compile run
# Gcc-wall $ (pkg-config--cflags--libs gstreamer-0.10) hello.c-o Hello
./hello
Operation Result:


This program is linked against GStreamer 0.10.22


Third, command line MP3 player test
#include <gst/gst.h>
#include <glib.h>
Define the message handler function,
Static Gboolean Bus_call (Gstbus *bus,gstmessage *msg,gpointer data)
{
Gmainloop *loop = (Gmainloop *) data;//This is a pointer to the main loop that exits the loop when the EOS message is accepted
Switch (Gst_message_type (msg))
{
Case Gst_message_eos:
G_print ("End of stream\n");
G_main_loop_quit (loop);
Break
Case GST_MESSAGE_ERROR:
{
Gchar *debug;
Gerror *error;
Gst_message_parse_error (Msg,&error,&debug);
G_free (Debug);
G_printerr ("error:%s\n", error->message);
G_error_free (Error);
G_main_loop_quit (loop);

Break
}
Default
Break
}
return TRUE;
}

int main (int argc,char *argv[])
{
Gmainloop *loop;
Gstelement *pipeline,*source,*decoder,*sink;//Defining components
Gstbus *bus;

Gst_init (&AMP;ARGC,&AMP;ARGV);
loop = G_main_loop_new (null,false);//create main loop, formally start loop after executing G_main_loop_run

if (argc! = 2)
{
G_printerr ("usage:%s <mp3 filename>\n", argv[0]);
return-1;
}

Creating Pipelines and Components
Pipeline = gst_pipeline_new ("Audio-player");
Source = Gst_element_factory_make ("Filesrc", "File-source");
Decoder = Gst_element_factory_make ("Mad", "Mad-decoder");
Sink = Gst_element_factory_make ("Autoaudiosink", "audio-output");

if (!pipeline| |! source| |! decoder| |! Sink) {
G_printerr ("one element could not being created. Exiting.\n ");
return-1;
}


Sets the location parameter of the source. That is, the file address.
G_object_set (G_object (source), "location", argv[1],null);


Get the message bus for the pipeline
Bus = Gst_pipeline_get_bus (Gst_pipeline (pipeline));

Add Message Monitor
Gst_bus_add_watch (Bus,bus_call,loop);
Gst_object_unref (bus);


Add components to the pipeline. A pipeline is a special component that allows for better flow of data
Gst_bin_add_many (Gst_bin (pipeline), source,decoder,sink,null);


Connecting components in turn
Gst_element_link_many (Source,decoder,sink,null);

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.