User Tools

Site Tools


developer_center:recipe_book:extensions_and_core:media_library:media_list_listeners

Media List Listeners

To be informed of updates to a playlist or library, you can use an sbIMediaListListenerFIXME. These are added using sbIMediaList.addListener()FIXME. For example, if you want your listener to be triggered when items are added or removed:

Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
var myListener = {
    onItemAdded: function(list, item, i) {
        alert("Item with guid: " + item.guid + " added to: " + list.name);
    },
    onAfterItemRemoved: function(list, item, i) {
        alert("Item with guid: " + item.guid + " removed from: " + list.name);
    }
}

LibraryUtils.mainLibrary.addListener(myListener, false,
    LibraryUtils.mainLibrary.LISTENER_FLAGS_ITEMADDED |
    LibraryUtils.mainLibrary.LISTENER_FLAGS_AFTERITEMREMOVED, null);

The above listener (myListener) will be triggered anytime any item is added or removed. For more granular events where properties may be involved, you can pass a property filter as the 4th argument to addListener()FIXME. For instance, if you want to be triggered whenever a user changes the artist name in the main library:

Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
var Cc = Components.classes;
var Ci = Components.interfaces;

var myListener = {
    onItemUpdated: function(list, item, i) {
        alert("Item with guid: " + item.guid + " was updated!");
    },
}

var propertyArray = 
    Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
    .createInstance(Ci.sbIPropertyArray);
// The actual property value is ignored for mediaListListeners
propertyArray.appendProperty(SBProperties.artistName, "ignored");

LibraryUtils.mainLibrary.addListener(myListener, false,
    LibraryUtils.mainLibrary.LISTENER_FLAGS_ITEMUPDATED, propertyArray);

The full list of listener topics/flags (e.g. the LISTENER_FLAGS_*) can be found in the sbIMediaList API referenceFIXME while the interface for what your media list listener should look like can be found in the sbIMediaListListener API referenceFIXME.

developer_center/recipe_book/extensions_and_core/media_library/media_list_listeners.txt · Last modified: 2013/12/28 09:11 by geekshadow