6

This blog post suggests that it might be possible to play YouTube videos with the Silverlight MediaEelement directly.

<MediaElement HorizontalAlignment="Left"
VerticalAlignment="Top"
Source="http://www.youtube.com/get_video?
video_id=8yuIw_0ejLs&t=vjVQa1PpcFPrX3tFoahhu4DbniDIqTLkwybdm8xuCt8%3D&fmt=22"/>

I was wondering if this holds true for the Windows Phone 7. I have an application that is based on playing videos hosted on YouTube, and it would be nice to be able to have more control over the video experience other than just launching the browser with the YouTube video URL.

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
Joel Martinez
  • 46,929
  • 26
  • 130
  • 185

4 Answers4

4

For everyone else still curious the problem to overcome is getting a direct link to the video which does require a small hack but it's very reliable and easy to do. Firstly you need the video id so you can get the youtube url which you can use the youtube api for. Then do something like this. I pretty much converted a userscript to silverlight.

WebClient client = new WebClient();
string url = "http://www.youtube.com/watch?v=HLQqOpILDcI";
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(ClientDownloadStringCompleted);
client.DownloadStringAsync(new Uri(url, UriKind.Absolute));

the next bit looks bad.

    private void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        rx = new Regex("(?<=url_encoded_fmt_stream_map=)([^(\\\\)]*)(?=\\\\)", RegexOptions.IgnoreCase);
        match = rx.Matches(flashvars);
        string video_format = match[0].ToString();

        string sep1="%2C"; 
        string sep2="%26"; 
        string sep3="%3D";
        string link = "";
        string[] videoFormatsGroup = Regex.Split(video_format, sep1);
        for (var i=0;i<videoFormatsGroup.Length;i++){
            string[] videoFormatsElem = Regex.Split(videoFormatsGroup[i], sep2);
            if (videoFormatsElem.Length<5) continue;
            string[] partialResult1 = Regex.Split(videoFormatsElem[0], sep3);
            if (partialResult1.Length<2) continue;
            string url = partialResult1[1];
            url = HttpUtility.UrlDecode(HttpUtility.UrlDecode(url));
            string[] partialResult2 = Regex.Split(videoFormatsElem[4], sep3);
            if (partialResult2.Length<2) continue;    
            int itag = Convert.ToInt32(partialResult2[1]);
            if (itag == 18){
                link = url;
            }
        }
    }

the last bit itag==18 selects the quality according to this

    {'5':'FLV 240p','18':'MP4 360p','22':'MP4 720p (HD)','34':'FLV 360p','35':'FLV 480p','37':'MP4 1080p (HD)','38':'MP4 4K (HD)','43':'WebM 360p','44':'WebM 480p','45':'WebM 720p (HD)','46':'WebM 1080p (HD)'};

now you can do whatever you want with the link like open it with mediaplayerlauncher or mediaelement. personally i'd love to download it to isolated storage and play it at the same time but at the moment that seems easier said than done. thanks for your time sorry for the long post.

Cadell Christo
  • 3,105
  • 3
  • 21
  • 19
  • He forgot to define flashvars, which is, in the movie html page, just a segment of text that contains what is interesting one who's trying to get the direct link. If you open a movie page (http://www.youtube.com/watch?v=[id] and check the source, you'll notice a place where there's a variable called flashvars. It's up to you to isolate it, then to find the direct link from the code above. – Léon Pelletier Mar 17 '13 at 05:32
  • Needs to write Regex rx = ... and MatchCollection match = ... and add a using System.Text.RegularExpressions; (can't edit the code since I've waiting for other fixes I sent to be processed) – George Birbilis Aug 13 '13 at 17:24
4

Unless you have a direct link to video content, you cannot display YouTube videos on Windows Phone 7. As far as I know, get_video is no longer available for public access.

Den
  • 16,686
  • 4
  • 47
  • 87
  • Hmm, so I guess the question then shifts to, is there an allowed method of getting access to the video's direct link with youtube's official API? or is that generally not allowed? – Joel Martinez Nov 25 '10 at 16:56
  • There is no official API method to download the video. There are workarounds and "hacks" but those aren't widespread and are in no way well-documented. – Den Nov 26 '10 at 03:02
  • Ok, thanks ... this helps me set directions and expectations for my app :-) – Joel Martinez Dec 16 '10 at 20:16
4

Quoting from the Windows Phone Developer FAQ

How can I play youtube videos in my app?

Use the WebBrowserTask and open the target URL in the browser; if the youtube app is installed, it will play, if not installed, it will prompt the user to install and then play.

Community
  • 1
  • 1
Derek Lakin
  • 16,179
  • 36
  • 51
0

I am sure you can adjust it for Windows Phone http://www.codeproject.com/KB/WPF/YouViewer.aspx

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • no that won't work, because it assumes that you'll be able to play a .swf file in the web browser control. This is not possible in windows phone – Joel Martinez Nov 25 '11 at 14:31