尝试使用 MediaPlayerLauncher 播放 YouTube 视频

发布于 2024-12-29 11:59:43 字数 1850 浏览 4 评论 0原文

我正在编写一个 Windows Phone 7.1 应用程序,当用户单击特定按钮时,将使用 MediaPlayerLauncher 播放 YouTube 视频。

问题是,MediaPlayerLauncher 无法仅通过提供视频的 url 来播放视频;视频的链接本身位于页面的 html 中。现在,我设法使用 WebClient() 下载 html 并从中提取视频链接,通过附加“客户端”事件(我的 WebClient)来提取该 html:

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        pageHtml = e.Result;

        stringBuilder = new StringBuilder(pageHtml);
        if (pageHtml != null)
        {
            if (pageHtml.Contains("<html"))
            {
                if (pageHtml.Contains("<script"))
                {
                    stringBuilder.Replace("</script>", string.Format("{0}{1}</script>", NOTIFY_SCRIPT, Environment.NewLine));
                }
                else if (pageHtml.Contains("<head"))
                {
                    stringBuilder.Replace("</head>", string.Format("{0}{1}</head>", NOTIFY_SCRIPT, Environment.NewLine));
                }
                else
                {
                    stringBuilder.Replace("</html>", string.Format("{0}{1}</html>", NOTIFY_SCRIPT, Environment.NewLine));
                }
            }
            else
            {
                    //Just skip it or display an error message or whatever
            }

                rssBrowser.NavigateToString(stringBuilder.ToString());
        }
    }

基本上,我添加一个脚本“NOTIFY_SCRIPT”,它检测 YouTube 视频的存在(如果您想了解更多详细信息,视频的链接基本上位于标签中,因此我只需获取所有标签,找到带有链接的标签,然后获取其内容(链接))。

但这仍然行不通。我尝试放置一个 WebBrowser 并使其导航并在每次 WebBrowser 导航时触发事件,以确保它导航到正确的页面。但有时,它无法正确导航;它卡在中间页面或转到原始的 YouTube 页面。所以,我决定看一下传入的 html。由于某种原因,传入的 html 缺少 youtube 的脚本。我使用浏览器检查了 youtube 页面上的脚本(我转到移动网页并“检查了元素”),那里有一个脚本,但是当 WebClient 获取 html 时,脚本丢失了。

那么也许这就是问题所在?有谁知道如何解决这个问题,或者也许您已经以不同的方式做了类似的事情?

抱歉问了这么长的问题,谢谢!

I'm coding a windows phone 7.1 app, and when the user clicks a specific button, a youtube video would be played using a MediaPlayerLauncher.

Problem is, MediaPlayerLauncher can't play the video just by giving it the video's url; the video's link itself is in the page's html. Now, I managed to pull out that html by using a WebClient() to download the html and extract the video's link from it, by attaching this event for 'client', my WebClient:

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        pageHtml = e.Result;

        stringBuilder = new StringBuilder(pageHtml);
        if (pageHtml != null)
        {
            if (pageHtml.Contains("<html"))
            {
                if (pageHtml.Contains("<script"))
                {
                    stringBuilder.Replace("</script>", string.Format("{0}{1}</script>", NOTIFY_SCRIPT, Environment.NewLine));
                }
                else if (pageHtml.Contains("<head"))
                {
                    stringBuilder.Replace("</head>", string.Format("{0}{1}</head>", NOTIFY_SCRIPT, Environment.NewLine));
                }
                else
                {
                    stringBuilder.Replace("</html>", string.Format("{0}{1}</html>", NOTIFY_SCRIPT, Environment.NewLine));
                }
            }
            else
            {
                    //Just skip it or display an error message or whatever
            }

                rssBrowser.NavigateToString(stringBuilder.ToString());
        }
    }

Basically, I add a script, 'NOTIFY_SCRIPT', which detects the presence of the youtube video(if you want more details about this, the video's link is basically in an tag, so I just get all the tags, find the one with the link, and get it's contents (the link)).

But still, this just doesn't work. I tried putting up a WebBrowser and making it navigate and triggering the event every time the WebBrowser navigates, in order to make sure that it's navigating to the correct page. But sometimes, it doesn't navigate properly; it gets stuck on an intermediate page or goes to the original youtube page. So, I decided to take a look at the incoming html. For some reason, the incoming html is missing youtube's script. I checked the script on the youtube page using my browser (I went to the mobile web page and 'inspected the element'), and it has a script there, but when the WebClient gets the html the script is missing.

So maybe that's the problem? Does anyone know how to solve this problem, or maybe you have already done something like that in a different way?

Sorry for the long question, and thanks!

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

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

发布评论

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

评论(1

作死小能手 2025-01-05 11:59:43

您可能最好使用 WebBrowserTaskAPI 获取 URL。

You may be better of using the WebBrowserTask or the API to get the URL.

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