Windows Phone 上的 YouTube 与 MediaElement

发布于 2024-10-04 11:11:29 字数 535 浏览 0 评论 0原文

这篇博文建议或许可以直接使用 Silverlight MediaEelement 播放 YouTube 视频。

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

我想知道这是否适用于 Windows Phone 7。我有一个基于播放 YouTube 上托管的视频的应用程序,如果能够更好地控制视频体验,而不仅仅是使用以下命令启动浏览器,那就太好了YouTube 视频网址。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

高速公鹿 2024-10-11 11:11:29

除非您有视频内容的直接链接,否则无法在 Windows Phone 7 上显示 YouTube 视频。据我所知,get_video 不再可供公众访问。

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.

冷情 2024-10-11 11:11:29

引自 Windows Phone 开发人员常见问题解答

如何在我的应用中播放 YouTube 视频?

使用WebBrowserTask并在浏览器中打开目标URL;如果安装了youtube应用,就会播放,如果没有安装,会提示用户安装然后播放。

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.

雾里花 2024-10-11 11:11:29

对于其他仍然好奇的人来说,需要克服的问题是获得视频的直接链接,这确实需要一些小技巧,但它非常可靠且易于做到。首先,您需要视频 ID,以便获得可以使用 youtube api 的 youtube url。然后做这样的事情。我几乎将用户脚本转换为 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));

接下来的一点看起来很糟糕。

    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;
            }
        }
    }

最后一位 itag==18 根据此选择质量

    {'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)'};

,现在您可以对链接执行任何您想要的操作,例如使用 mediaplayerlauncher 或 mediaelement 打开它。就我个人而言,我很想将其下载到独立存储中并同时播放,但目前看来说起来容易做起来难。感谢您抽出宝贵的时间,抱歉发了这么长的帖子。

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.

疯了 2024-10-11 11:11:29

我确信您可以针对 Windows Phone 进行调整 http://www.codeproject.com/KB /WPF/YouViewer.aspx

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文