VideoApi

Public interfaces

IVideoWebsiteApi

namespace OpenWaves.Video
{
    [DefaultImplementation(typeof(DefaultVideoWebsiteApi))]
    public interface IVideoWebsiteApi
    {
        IVideo GetVideo(string url);
        bool IsSupported(string url);
    }

    public class DefaultVideoWebsiteApi : CompositeVideoWebsiteApi
    {
        public DefaultVideoWebsiteApi()
            : base(new IVideoWebsiteApi[] { new VimeoApi(), new YoutubeApi() })
        {
        }
    }
}

IVideo

public interface IVideo
    {
        IEnumerable<IVideoThumbnail> Thumbnails { get; }
        string Title { get; }
        string Id { get; }
    }

IVideoThumbnail


    public interface IVideoThumbnail
    {
        string Url { get; set; }
        int Height { get; set; }
        int Width { get; set; } 
    }

Usage example


var videoApi= ServiceLocator.Resolve<IVideoWebsiteApi>();
var videoInfo = videoApi.GetVideo(videoUrl);

Caching

Currently IVideoApi has two implementations: for Vimeo and Youtube. Both supports caching. You can inject cache in constructor. By default both of them do not cache requests.


public YoutubeApi() : this(new ConcurrentCache<IVideo>(Cache.Null<IVideo>()))
        {
        }

public YoutubeApi(IConcurrentCache<IVideo> videoCache): this(videoCache, GetDefaultWebClient())
        {
        }

Adding support for another service

If you want to add support for another video service you need to create new implementations of IVideoWebsiteApi, IVideo, IVideoThumbnail and create new IVideoEmbedControlFactory factory which is responsible of creating HTML controls for specified video.

Fragment of VideoEmbedControlFactory implementation


public class VideoEmbedControlFactory : IVideoEmbedControlFactory
    {
        public HtmlGenericControl CreateEmbedControl(IVideo video, string cliendID, bool includeClientSideApis)
        {
            if(video is YouTubeVideo)
            {
                var control = new HtmlGenericControl("iframe");
                if (includeClientSideApis)
                {
                    control.PreRender += (source, args) =>
                        {
                            control.Page.ClientScript.RegisterClientScriptInclude(typeof(VideoEmbedControlFactory), "YouTubeApi", "http://www.youtube.com/iframe_api");
                        };
                }

                control.Attributes.Add("src", string.Format(YoutubeApi.YoutubeEmbedUrlFormat, video.Id));
                control.Attributes.Add("id", cliendID);
                control.Attributes.Add("data", "youtube");
                return control;
            }

***


Remember that you will need to register your implementations of IVideoWebsiteApi and IVideoEmbedControlFactory in the ServiceLocator. Video and VideoThumbnail are using implementations which are registered in the ServiceLocator.


Last edited Feb 18, 2013 at 10:59 AM by radzio, version 7

Comments

No comments yet.