Using Bonobo-Media

Using the Control

To display a multimedia playback widget in your application, just load the data you want to play into a Bonobo::Moniker and resolve it for the Bonobo::Control interface (you can do this automatically via the BonoboWidget API)

 GtkWidget *playback = bonobo_widget_new_control ("welcome.mp3", 0);

Screenshot of the player control This will create a Bonobo_Media_PlayerControl, and create a Bonobo::Media::Stream from the supplied stream via the Moniker system.


Using streams directly

If you want to play back multimedia streams from your application (for example, in a snazzy About dialog, as a cut scene in a game, or just to play a notification sound), you should use the Stream interface directly. The following example shows you how you can play a single audio stream from your application.

void create_stream ()
{
    CORBA_Environment    ev;
    Bonobo_Media_Stream  stream;
    Bonobo_EventSource   event_source;

    /* Create a stream via a file moniker */
    stream = bonobo_get_object ("intro_music.ogg",
				"IDL:Bonobo/Media/Stream:1.0");

    /* Check for the EventSource interface */
    CORBA_exception_init (&ev);
    event_source = Bonobo_Unknown_queryInterface (stream, "IDL:Bonobo/EventSource:1.0", &ev);

    if (ev._major == CORBA_NO_EXCEPTION &&
	stream != CORBA_OBJECT_NIL)
    {
        /* Add a Listener to the EventSource */
        BonoboListener *listener = bonobo_listener_new (listener_cb, stream);
        Bonobo_Listener corba_listener = bonobo_object_corba_objref (BONOBO_OBJECT (listener));

        Bonobo_EventSource_addListener (event_source, corba_listener, &ev);

        /* queryInterface increases the reference count on the return value */
        Bonobo_Unknown_unref (event_listener, &ev);
    }
    CORBA_exception_free (&ev);

    /* Start playback */
    Bonobo_Media_Stream_play (stream, &ev);
    
}

void listener_cb (BonoboListener    *listener,
                  char              *event_name,
                  BonoboArg         *event_data,
                  CORBA_Environment *ev,
                  gpointer           closure)
{
    Bonobo_Media_Stream stream = (Bonobo_Media_Stream) closure;

    /* Check for the event's name */
    if (!strcmp (event_name,
                 bonobo_event_make_name ("Bonobo/Media/Stream",
                                         "end_notify", 0)))
    {
        CORBA_Environment my_ev;

        g_warning ("Stream ended");

        /* Free the stream after usage */
        CORBA_exception_init (&my_ev);
        Bonobo_Unknown_unref (stream, &my_ev);
        CORBA_exception_free (&my_ev);
    }
}

First, you load your stream into a Bonobo::Media::Stream with the help of a moniker (in this example, the file moniker). Of course, you can also use OAF directly to get a Stream component, then load it via a Persist interface. In the above example, the stream is only needed for a single playback, so you attach a Listener to its EventSource (when available) to get notified when the stream finished. After all is set, start the playback of the stream. Don't forget to free (unref) the stream after you're done with it.


Back to main page

Home | Open source | Diary | Articles | Photos | Files
This XHTML page is created by Gergõ "Cactus" Érdi. Send your comments to cactus@cactus.rulez.org.
Hosted by BME.