Get MediaItem object from a MediaItemId (Guid)

Discussion in 'Plugin Development' started by McGoober, December 11, 2011.

  1. McGoober
    • Team MediaPortal

    McGoober Retired Team Member

    Joined:
    August 13, 2006
    Messages:
    122
    Likes Received:
    103
    Gender:
    Male
    Occupation:
    Staff Software Developement Engineer
    Location:
    Cambridge, UK
    Ratings:
    +105 / 0
    Home Country:
    United Kingdom United Kingdom
    I'm coding against IMediaLibrary in a plugin. I have exposed MediaItem.MediaItemId (guid) to a remote application as a unique reference to a media item, but when that app calls back again with this guid I want to turn it back into a MediaItem object. Doing a search is the only way I can think of doing it, but I can't figure out the filter I need since there doesn't seem to be an attribute I can use.


    What I want to do is...



    public MediaItem GetItemFromMediaItemId(Guid ObjectId)
    {
    var library = ServiceRegistration.Get<IMediaLibrary>();
    return library.GetMediaItem(ObjectId, necessaryMIATypeIDs, optionalMIATypeIDs);
    }
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Albert
    • Team MediaPortal

    Albert MP2 Developer

    Joined:
    February 18, 2008
    Messages:
    1,297
    Likes Received:
    1,125
    Gender:
    Male
    Occupation:
    Dipl.-Inform.
    Location:
    Freiburg im Breisgau, Germany
    Ratings:
    +1,130 / 1
    Home Country:
    Germany Germany
    AW: Get MediaItem object from a MediaItemId (Guid)

    Is it a sort of playlist what you're exposing? You can use IList<MediaItem> IMediaLibrary.LoadCustomPlaylist(IList<Guid> mediaItemIds, IEnumerable<Guid> necessaryMIATypes, IEnumerable<Guid> optionalMIATypes) or please describe why you need to expose the reference to that item.
     
  4. McGoober
    • Team MediaPortal

    McGoober Retired Team Member

    Joined:
    August 13, 2006
    Messages:
    122
    Likes Received:
    103
    Gender:
    Male
    Occupation:
    Staff Software Developement Engineer
    Location:
    Cambridge, UK
    Ratings:
    +105 / 0
    Home Country:
    United Kingdom United Kingdom
    I'm developing a UPNP DLNA plugin. I need to expose to the connecting device some form of unique id so that the device and the plugin can keep track of where in the media tree the device is. I was thinking of doing this with the media item ID (GUID). This way I know what the child items are by calling library.Browse, but the other thing the device could ask for is "give me more information about this item", and for that I need to turn a media item guid back into a MediaItem object.

    Example...
    So the connecting device might say, give me all items from the root node, and I return..

    var library = ServiceRegistration.Get<IMediaLibrary>();
    var shares = library.GetShares(null);
    var result = from share in shares select library.LoadItem(share.Value.SystemId, share.Value.BaseResourcePath, necessaryMiaTypes, null).MediaItemId;

    The connecting device might then say, give me more information about item: 466bc397-d646-4f3d-bca6-0de2e7ce38ae
    I want to load the media item to get a MediaItem object again.

    Guid mediaItemGuid = new Guid("466bc397-d646-4f3d-bca6-0de2e7ce38ae");
    var library = ServiceRegistration.Get<IMediaLibrary>();
    var item = library.GetMediaItem(mediaItemGuid, necessaryMIATypeIDs, optionalMIATypeIDs);

    I hope that makes more sense :)

    The device could return me any media item and would be of any aspect.
     
    • Like Like x 4
  5. Albert
    • Team MediaPortal

    Albert MP2 Developer

    Joined:
    February 18, 2008
    Messages:
    1,297
    Likes Received:
    1,125
    Gender:
    Male
    Occupation:
    Dipl.-Inform.
    Location:
    Freiburg im Breisgau, Germany
    Ratings:
    +1,130 / 1
    Home Country:
    Germany Germany
    AW: Get MediaItem object from a MediaItemId (Guid)

    Cool, a UPnP/AV MediaServer! :)

    Ok, to achieve that, you can call the method I said above: IList<MediaItem> IMediaLibrary.LoadCustomPlaylist(IList<Guid> mediaItemIds, IEnumerable<Guid> necessaryMIATypes, IEnumerable<Guid> optionalMIATypes). It would also be possible to write an extra method for that in the MediaLibrary, something like MediaItem IMediaLibrary.LoadMediaItem(Guid mediaItemId, IEnumerable<Guid> necessaryMIATypes, IEnumerable<Guid> optionalMIATypes). We can decide that later, when you're finished. Until you're finished, you can use the LoadCustomPlaylist method. You could also write an extension method for the MediaLibrary with the new signature and we replace it later with the final method.
     
    • Like Like x 1
  6. McGoober
    • Team MediaPortal

    McGoober Retired Team Member

    Joined:
    August 13, 2006
    Messages:
    122
    Likes Received:
    103
    Gender:
    Male
    Occupation:
    Staff Software Developement Engineer
    Location:
    Cambridge, UK
    Ratings:
    +105 / 0
    Home Country:
    United Kingdom United Kingdom
    Super, that's just what I wanted :D
    Extension method sound like a good plan.

    Just as an aside... I was playing with the code a few nights ago when my wife was watching a DVD on my sony DLNA compliant player. I noticed the next night that it showed my MediaPortal 2 upnp device and I was able to browse some of the shares from the media library... cool hu?, lots of work to go still.
     
    • Like Like x 1
  7. McGoober
    • Team MediaPortal

    McGoober Retired Team Member

    Joined:
    August 13, 2006
    Messages:
    122
    Likes Received:
    103
    Gender:
    Male
    Occupation:
    Staff Software Developement Engineer
    Location:
    Cambridge, UK
    Ratings:
    +105 / 0
    Home Country:
    United Kingdom United Kingdom
    Thanks all, LoadCustomPlaylist() works a treat.
    For completeness of this thread, here is the extension method I used.

    Code (Text):
    1. public static MediaItem GetMediaItem(this IMediaLibrary mediaLibrary, Guid mediaItemId, IEnumerable<Guid> necessaryMIATypes, IEnumerable<Guid> optionalMIATypes)
    2. {
    3.     var items = mediaLibrary.LoadCustomPlaylist(new List<Guid>() {mediaItemId}, necessaryMIATypes, optionalMIATypes);
    4.     return items.Count > 0 ? items[0] : null;
    5. }
    6.  
    So browsing the MediaPortal 2 Media Library works from a DLNA device, now on to streaming the content :D
     
    • Like Like x 3
  8. Albert
    • Team MediaPortal

    Albert MP2 Developer

    Joined:
    February 18, 2008
    Messages:
    1,297
    Likes Received:
    1,125
    Gender:
    Male
    Occupation:
    Dipl.-Inform.
    Location:
    Freiburg im Breisgau, Germany
    Ratings:
    +1,130 / 1
    Home Country:
    Germany Germany
    AW: Get MediaItem object from a MediaItemId (Guid)

    Perfect! :)
     
Loading...

Users Viewing Thread (Users: 0, Guests: 0)

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice
  • About The Project

    The vision of the MediaPortal project is to create a free open source media centre application, which supports all advanced media centre functions, and is accessible to all Windows users.

    In reaching this goal we are working every day to make sure our software is one of the best.

             

  • Support MediaPortal!

    The team works very hard to make sure the community is running the best HTPC-software. We give away MediaPortal for free but hosting and software is not for us.

    Care to support our work with a few bucks? We'd really appreciate it!