Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube on Windows Phone with MediaElement

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.

like image 855
Joel Martinez Avatar asked Nov 24 '10 22:11

Joel Martinez


People also ask

Does Windows Phone have YouTube?

Watch YouTube on Windows 10 Mobile heading into 2022. One of the biggest services that is still easily accessible under Windows 10 Mobile at the end of 2021 is YouTube.

How do I download YouTube videos to my Windows phone?

YouTube App Download for Windows 11/10 PCGo to YouTube official website in your Google Chrome browser. Then you can click the Install YouTube icon at the right corner of the address bar. It will install the YouTube app on your Windows 11/10 PC. You can tap the YouTube icon to launch YouTube app directly next time.


3 Answers

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.

like image 117
Den Delimarsky Avatar answered Nov 13 '22 14:11

Den Delimarsky


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.

like image 4
Derek Lakin Avatar answered Nov 13 '22 13:11

Derek Lakin


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.

like image 4
Cadell Christo Avatar answered Nov 13 '22 15:11

Cadell Christo