A Youtube Connector for Sitecore Media Framework Part 6 – Preview Youtube Video in the Content Editor

I am at the end of the series of blog posts on Youtube Connector for Sitecore, at least for now. I want to write one more blog on how DMS can be used to personalize users video watching preference. But, that I will do later. I want to venture on other areas of Sitecore. Don’t know what that will be, yet.

In the last post, I discussed how to get the Youtube video data and create Sitecore Items using Google Apis. In this post, I will show how we can preview the video in the Content Editor.

When I go to the /sitecore/media library/Media Framework/Accounts/MyYoutubeVideos Account/Media Content folder and do a search, the search result returns all the items I imported from my Youtube account. We will preview the video from this search result screen. To preview the video from the search result screen, I have to add the Quick Action called Media Preview to the items returned by the search. If you want to know more about Quick Action read this article by John West. The Quick Action for Bucketed Items are located in /sitecore/system/Settings/Buckets/Settings/Quick Actions. To add the Media Preview to the Vidoe Items, I need to go to the Video Template (MyYoutubeVideos Video) and select the Media Preview Quick Action in Item Buckets section of _Standard Values.

Sitecore Media Framework

Now if I search the Video items, I see the Media Preview Quick Action below each item in the search result in the Content Editor.

Sitecore Media Framework

If we click on the Media Preview Quick Action, Media Framework should play the Youtube video for that item. But, for that I have to add some code. The first thing I need to do is to tell Media Framework which type (class) to use for generating the markup for the video preview. Media Framework will read this information from my connector’s config file. So, I added the following section in the config.

<playerMarkupGenerators>
   <add name="youtube_video" templateId="{D5DCE266-13E8-4583-A318-F3C19212754C}" type="Sitecore.MediaFramework.Youtube.Players.YoutubePlayerMarkupGenerator,Sitecore.MediaFramework.Youtube" />
</playerMarkupGenerators>

As the config shows, I need to create a class called YoutubePlayerMarkupGenerator. This class’s methods will be called by the Media Framework when I click on the Media Preview Quick Action. YoutubePlayerMarkupGenerator class is derived from PlayerMarkupGeneratorBase class of Media Framework and following are methods implemented in this class.

 public class YoutubePlayerMarkupGenerator : PlayerMarkupGeneratorBase
 {
   public override PlayerMarkupResult Generate(MediaGenerateMarkupArgs args)
   {
     var playerMarkupResult = new PlayerMarkupResult();
     var width = args.Properties.Width.ToString();
     var height = args.Properties.Height.ToString();
     var videoId = args.MediaItem[FieldIDs.MediaElement.Id];
     playerMarkupResult.Html = string.Format("", width, height, videoId);
     return playerMarkupResult;
   }

   public override string GetPreviewImage(MediaGenerateMarkupArgs args)
   {
     return PlayerManager.GetPreviewImage(args, FieldIDs.Video.ThumbnailUrl);
   }

   public override Item GetDefaultPlayer(MediaGenerateMarkupArgs args)
   {
     ID fieldId = args.MediaItem.TemplateID == TemplateIDs.Video ?     FieldIDs.AccountSettings.DefaultVideoPlayer : FieldIDs.AccountSettings.DefaultPlaylistPlayer;
     var referenceField = (ReferenceField)AccountManager.GetSettingsField(args.AccountItem, fieldId);
     if (referenceField == null)
       return (Sitecore.Data.Items.Item)null;
     else
       return referenceField.TargetItem;
   }

   public override string GetMediaId(Item item)
   {
     return item[FieldIDs.MediaElement.Id];
   }
 }

The method GetMediaId gets called first. The Media Framework calls this method to know the Id of the Video and based on that finds the Item. The GetPreviewImage method is used to return the preview image url for the video. In my case I returned the Thumbnail Url I imported from Youtube. The GetDefaultPlayer returns the Player Item. This Item contains Player specific configuration that the Media Framework can use when it plays the video. The main method though is  Generate. This method returns the HTML markup to be used by the Media Framework to play the video. My implementation of this method uses the Youtube embedding markup. For the calling video item, I construct this embedding markup using VideoId, Width and Height of the Video. That’s it. Now, if I click on the Media Preview in the search result page, I can watch my video. Isn’t that cool! Below is a video of the Video I am watching in the Content Editor :). That’s my daughter practicing in the Skating rink.

That’s the end of this series of blog posts. There are lot more in the Media Framework than what I discussed in this series, but covering all of Media Framework will take months of writing blogs. I hope, what I discussed here that will give a good start to anyone intend to create a Media Framework Connector. I will share the code in a GitHub project and I am also planning to share the application in Sitecore Marketplace soon. Happy coding!!!

About Himadri Chakrabarti (he/him)

I am a solutions architect and technology enthusiast. I am Sitecore and Optimizely MVP. When I am not working with technology I spend time on photography https://www.himadriphoto.com/. Opinions expressed in my blogs are my own.
This entry was posted in Connnector, Media Framework, Sitecore and tagged , , , . Bookmark the permalink.

8 Responses to A Youtube Connector for Sitecore Media Framework Part 6 – Preview Youtube Video in the Content Editor

  1. hua says:

    Have you share this in a github?

  2. Simon says:

    Hi Himadri, great blog series. Very informative. Did you ever get a chance to share the code on GitHub? I’m investigating a similar approach for videos stored on a file share.
    Simon

  3. Rachel says:

    Hi Himandri. I have currently been using your youtube connector and it has been very resourceful. My question is how to use a player in content editor to render the media. I noticed this was your last blog post and I was wondering if you cover this information elsewhere?

    Thanks,
    Rachel

  4. Patrick says:

    Hi Himandri, great work. Do you know if this connector will work with Media Framework 2.1 and Sitecore 8?

    • Hi Patrick, I haven’t tested it with Sitecore 8 yet but, I yo going to do that soon. If anything needs to change for porting to Sitecore 8, I will put a new version in the Marketplace and Github. Thanks.

Leave a reply to Himadri Chakrabarti Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.