Assigning media to a renderer

You can play media in a renderer by assigning a playlist to it. For this purpose you have to use mafw_renderer_assign_playlist. Once a playlist has been assigned to the renderer the user may choose to start playback right away (starting from the first item in the playlist) or choose a specific item to start from. For the latter case, the renderer offers the methods mafw_renderer_next, mafw_renderer_previous and mafw_renderer_goto_index, that allow the user to select the next and previous items to the one currently selected or move directly to a particular item in the playlist. Also, the current media may change due to other factors, for example, when the playback of the current item in the playlist finishes the renderer will move to the next, it might also happen in the case of errors that do not allow to play the current item, in this scenario the renderer may choose to automatically move to the next item in the playlist too.

No matter the reason that has triggered a change in the current media assigned to the renderer, it will notify the application about the new media by emitting a "media-changed" signal. Application developers should connect to this signal to update their UIs accordingly whenever new media is selected in the renderer. It is also possible to query a renderer about its current media by using mafw_renderer_get_status.

Here is a small code snippet:

static void
media_changed_cb(MafwRenderer *renderer, gint index, gchar *object_id, 
                 gpointer user_data)
{
        if (index >= 0) {
                /* Update UI: select item at position 'index' */
        } else {
               /* Playlist playback interrupted, maybe inform the user
                  about this and then wait until it is resumed, then we 
                  will get a new media-changed signal */
        }
}

static void 
assign_playlist_cb(MafwRenderer *renderer, gpointer user_data, const GError *error)
{
        if (error != NULL) {
                /* error management code here */
        }
}

static void
assign_playlist(MafwRenderer *renderer, MafwPlaylist *playlist) 
{     
        g_signal_connect(renderer, "media-changed",
                         G_CALLBACK(media_changed_cb), NULL);

        mafw_renderer_assign_playlist(renderer, playlist, assign_playlist_cb, NULL);
}

In the code above, the "media-changed" handler checks if the index is positive, if that is the case it could now use this information to update the UI, ensuring that the user knows what the item selected in the renderer is. This index parameter can be negative, this happens when the playlist playback has been interrupted. This may happen, for example, if someone has issued a mafw_renderer_play_object command, which puts off the playlist playback to play another individual item and resumes the playlist playback once that individual item has finished.