Re: Playing a sound in GNOME



2006/10/30, Andreas Røsdal <andrearo pvv ntnu no>:
On Mon, 30 Oct 2006, Richard Hughes wrote:
> I think whatever is done, there needs to be a way for application "foo"
> to play file "ding.wav" without worrying about the intricacies of
> gstreamer. Don't get me wrong, the gstreamer API is great (and really
> powerful) but somewhat complicated when you just want a "alert" sound
> from the speakers.

I can confirm this. I've been trying to use gstreamer to play .ogg sounds
for gnome-games, but so far gstreamer has been far too complicated to use.
I have still not seen a way to play ogg files using gstreamer in only a
few lines of code. So this would be very much wanted...

If a simple fire-and-forget (well, apart of cleanup of course :) is
enough, see the attached file that has a minimal player app
constructed with the playbin automagic. It handled the wav clip and
ogg videos and MJPEG clips I threw at it just fine, though failed with
the only audio file I found with a "real" codec (an mp3 for which I
don't seem to have a decoder installed). In any case it has a "all
your money back" guarantee* so it should be safe to try out.

It's of course not "few lines of code", but things in C never are
"just few lines of code". The whole program is ~120 lines of which at
least half are comments or result of my verbose and loose style of
coding. I bet some python script could do it in three, but that's
another thing.

* Does not account for loss of income due to misspent work time. ;)

/me just wanting to say "it's really not that hard once you get the hang of it"

P.S. Note that I'm not against wrapping this in a simple sound-playing API

--
Kalle Vahlman, zuh iki fi
Powered by http://movial.fi
Interesting stuff at http://syslog.movial.fi
/*   Pling, a simplistic media file launcher.
 *   Copyright 2006 Kalle Vahlman, <zuh iki fi>
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
 *
 * NOTE: Yes, it is LGPL despite that it's not a library.
 *
 * Compile with 
 *     gcc -o pling pling.c `pkg-config --cflags --libs gstreamer-0.10` -Wall
 */

#include <glib.h>
#include <gst/gst.h>

/* Bus watch to catch messages like "finished playing" or
 * "oh, dear. The non-free codec is missing."
 */
static gboolean
bus_watch (GstBus *bus, GstMessage *message, gpointer data)
{
	GMainLoop *loop = (GMainLoop *) data;

  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR: {
      GError *err;
      gchar *debug;

      gst_message_parse_error (message, &err, &debug);
      g_print ("Encountered error: %s\n", err->message);
      g_error_free (err);
      g_free (debug);

      g_main_loop_quit (loop);
      break;
    }
    case GST_MESSAGE_EOS:
      /* end-of-stream */
      g_main_loop_quit (loop);
      break;
    default:
      /* unhandled message */
      break;
  }
}

gint main (gint argc, gchar **argv)
{
  GstElement *pipeline;
  GMainLoop *loop;

  gst_init (&argc, &argv);

  /* Look mommy, no GUI required! */
  loop = g_main_loop_new (NULL, FALSE);

  if (argc < 2 || argc > 3)
    {
      g_print ("I want exactly one media file URI as an argument.\n"
               "(for example, file:///home/%s/media/song.ogg)\n",
               getenv("USER"));
      exit(1);
    }

  if (argv[1] == NULL || !gst_uri_is_valid(argv[1]))
    {
      g_print ("The URI '%s' is not valid, perhaps there is a typo?.\n",
      argv[1]);
      exit(1);
    }

  /* Create a playbin, an automagic gst element that (tries to) handle
   * everything for us.
   */
  pipeline = gst_element_factory_make("playbin", "playbin");


  if (pipeline != NULL)
    {
      GstBus *bus;

      /* Attach the bus watch */
      bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
      gst_bus_add_watch (bus, bus_watch, loop);
      gst_object_unref (bus);

      /* Set the target URI */
      g_object_set (G_OBJECT(pipeline), "uri", argv[1], NULL);
      
      /* Start playing! */
      gst_element_set_state (pipeline, GST_STATE_PLAYING);

      g_main_loop_run (loop);

      /* Clear the resources (not really needed in this case though) */
      gst_element_set_state (pipeline, GST_STATE_NULL);
      g_object_unref(G_OBJECT(pipeline));
      pipeline = NULL;

    } else {
      /* Oopsy */
      g_warning ("Audio playback element could not be created!");
      exit (1);
    }

  return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]