Working with a Media Collection

Applications can create media collections of audio and video items to play, similar to a playlist. These media collections can be created and modified dynamically, in real time, offering basic queue management features.

Using a media collection, applications can:

  • Specify the order in which items are played.
  • Play an item at any location within the collection.
  • Append, insert, and remove items from the collection.
  • Determine which item is currently playing.
  • Specify the playback modes (fast forward, rewind, previous, next, and seek) that are allowed for each item in the collection.
  • Specify a time index from which to start playback of an item (for example, start playback of a video at an offset of five minutes from the beginning.)
  • Specify the length of time for which to play an item.
  • Play any of the video types that are supported natively in Windows Media Center (WMV, WTV, DVR-MS).
  • Override the title, creation time, and duration fields that are included in the media item with "friendly" data.
  • Use mixed http:// and file:// protocols within the same collection.

Creating and Modifying a Media Collection

Two classes are used to create and modify media collections:

  • The MediaCollectionItem class manages individual media items. The application can get or set various properties for a media item (such as its playback capabilities, length, URI, and so forth).
  • The MediaCollection class manages the overall media collection. The application can add, insert, and remove media items from the collection; determine which item is currently active; and indicate whether to continue playback if an error occurs.

Media items can be created with a MediaCollectionItem object and by setting individual properties for the media item, or by calling the MediaCollection.AddItem method and using the parameters to specify the properties.

The following example shows a button that adds a media item to a collection using variables for the media item URI and index, which were defined in the Locals section of the UI:

<ctrl:Button ButtonLabel="Add Item">
    <Command>
        <InvokeCommand Target="[TestCollection.AddItem]"
            media="[RandomUri.Value!cor:String]"
            index="[AddIndex.Value!cor:Int32]"/>
    </Command>
</ctrl:Button>

Playing a Media Collection

To play a media collection, use the MediaCenterEnvironment.PlayMedia method using the "MediaCollection" parameter for mediaType, and the name of the media collection for media: :

<ctrl:Button ButtonLabel="Play Collection">
    <Command>
        <InvokeCommand Target="[AddInHost.MediaCenterEnvironment.PlayMedia]"
            mediaType="MediaCollection"
            media="[TestCollection]"
            addToQueue="false"/>
    </Command>
</ctrl:Button>

The media collection is played from the first item in the collection index, and IsActive property for the media collection is true.

To start playback at a specific index (in other words, to play a particular item), set the MediaCollection.CurrentIndex property to the index of the item to play before calling PlayMedia.

The application can make other calls during playback, but calls are ignored if they attempt to modify the active item. For example, you can remove any item in the media collection except the item that is currently playing.

Events

A PropertyChanged event is fired whenever a property changes for the MediaCollection or MediaCollectionItem objects.

Lifetime

If the user leaves and then returns to the application, the application can use the MediaExperience.GetMediaCollection method to retrieve the media collection that was last played. Only the application that created this media collection can modify it.

For more information about media collections, see the ObjectModelMediaCenterMediaCollection samples in the Sample Explorer.

Sample Explorer

  • MediaCenter > all samples

See Also