YouTube on Windows Phone with MediaElement

2019-01-25 14:17发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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