Mohamed Moustafa
Mohamed Moustafa

Reputation: 289

Trying to play a youtube video using MediaPlayerLauncher

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!

Upvotes: 1

Views: 1001

Answers (1)

keyboardP
keyboardP

Reputation: 69372

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

Upvotes: 2

Related Questions