Trying to play a youtube video using MediaPlayerLauncher - c#

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!

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

Related

Disable runtime cache in Microsoft.Web.WebView2.Wpf

I am creating a webview2 wpf application, everything works well till now, but when I tried to load HTML file which is having youtube link, It's worked well along with autoplay .
But when I change the HTML file to some another site , the audio of youtube video still playing in background and webview2 load the new content.
I want something to disable the cache to be stored will runtime instead of clearing the cache.
If someone is having any idea related to above issue please help
This is the common issue faced by the webview2 when external site media player is played.
To overcome this issue you have to create a new instance of webview2 before you load second content to webView, this will create new Webview Page but still the UI will not stop the audio, so use Webview.dispose() as well.
WebView.Dispose()
Note : this will give you issue related to Null object reference for the 1st time .
So you have the check the the object before doing dispose.
Code should be like this :
MainPage :
WebViewPage webpage;
private void updateNewContent ()
{
...
WebView2 webView2 = Mywebvew2; // MYwebview2 is UI object from webviewPage,
//you have to pass this object from webview Page
if (webView2 != null)
{
webView2.Dispose();
}
webpage = null;
webpage = new WebViewPage();
GridPrincipal.Children.Add(webpage);
...
}

Stop multiple WebBrowsers from starting yt-videos?

I'm working with a winform TabControl showing WebBrowsercontrols to display youtube videos.
However with two videos or more it becomes really annoying as all videos start directly.
I basically need to find out if there is a JS function, html code or a simple WebBrowser property to change, so videos are paused.
It might come in handy to find something like that for video quality too.
Has anybody ever heard of/seen where this option is stored? Or maybe the Js function itself being invoked when manually setting the quality?
EDIT:
b.DocumentCompleted += delegate { b.DocumentText=b.DocumentText.Insert(b.DocumentText.IndexOf("class=\"video-stream html5-main-video\""), "autoplay=false ");};
b.Url = new System.Uri(inp[s], System.UriKind.Absolute);
Basically this should add a new Event handler on each webbrowser form that modifies the DocumentText when the Uri that is called during creation has loaded.
Even though the browser debugger shows
<video tabindex="-1" class="video-stream html5-main-video" controlslist="nodownload" style=... src=...></video>
this isn't in the actual source code.
However I found
$oa=function(a){g.S(a.o,"video-stream");g.S(a.o,"html5-main-video");var b=a.app.g;b.zc&&a.o.setAttribute("data-no-fullscreen",!0);b.Oh&&(a.o.setAttribute("webkit-playsinline",""),a.o.setAttribute("playsinline",""));b.Nr&&a.o&&a.P(a.o,"click",a.o.play,a.o)};
in the base.js. Is it possible that youtube generates the html from the js?
How can I modify the video-tag attributes then?
I tried to modify when the event handler manipulates the video tag, since there may be DocumentCompleted events thrown from scripts or anything.
delegate (object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e){
if (e.Url.AbsolutePath != ((System.Windows.Forms.WebBrowser)sender).Url.AbsolutePath){
//...
However it still fails as there is no occurance of the specific class that on the video tag.
I now dodged this by loading the Url only when the browser tab is selected, if someone finds a real solution, feel free to share

C# webBrowser navigate to another page in pdf

I have a C# winforms application that opens a pdf file in a webbrowser control. It opens to whatever page I want just fine however if I want to change page (go to a bookmark) the webbrowser stops working. I found this article which explains "Basically Webbrowser.Navigate(url) ONLY fires if the url changes. If it
doesn't change it uses a cached version of the web page." however I am calling navigate with a Uri, not a string url like this:
webBrowser.Navigate(new Uri(url));
My question is simply: how can I navigate to another page in the same pdf file, after I have opened the file in the web browser?
Of course I figure out the problem 2 mins after posting a question. I'll post my solution in hopes it helps someone else anyway;
So to make this work I used this workaround:
webBrowser.AllowNavigation = true;
webBrowser.Hide();
webBrowser.Navigate("about:blank");
await Task.Delay(1000);
webBrowser.Navigate(new Uri(url));
webBrowser.Show();
This code may be useful to you.
public static void GetAllText(WebBrowser webBrowser,int toPageNum)
{
webBrowser.Focus();
for(int i = 0; i < toPageNum; i++)
SendKeys.Send("{PGDN}");
}

Get "real" HTML source from website

So, I've come across an issue where my favorite radio station plays a song I don't know while I'm driving. They don't have one of those pages that shows a list of songs that they've played; however, they do have a "Now Playing" section on their site that shows what's currently playing and by who. So, I am trying to write a small program that will poll the site ever 2 minutes to retrieve the name of the song and the artist. Using Chrome dev tools, I can see the song title and artist in the source. But when I view the page source, it doesn't show up. They are using a javascript to run display that info. I've tried the following:
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(#"http://www.thebuzz.com/main.html");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
do
{
// Do nothing while we wait for the page to load
}
while (webBrowser1.ReadyState == WebBrowserReadyState.Loading);
var test = webBrowser1.DocumentText;
textBox1.Text = test.ToString();
}
Essentially, I'm loading it into a WebBrowser and trying to get the source this way. But I'm still not getting the part after the javascript is run. Is there a way to actually retrieve the rendered HTML after the fact?
EDIT
Also, is there a way in the WebBrowser to allow scripts to run? I get popups asking me if I want to allow them to run. I don't want to suppress them, I need them to run.
As Jay Tomten said in the comments, you're trying to fix the result of your problem, not the cause. The cause of the problem is that they're using Javascript to update that part of the page. Instead of working around that by letting the Javascript do its update and then reading what it wrote, ask yourself where the Javascript is getting the info from and whether you can go to the same place. Open up something that lets you see web traffic - Fiddler, or Chrome's dev console, for example. Watch for POST calls. One of them will likely be an AJAX request in which the Javascript on the page is getting the current song. Note the URL, inspect the call to see what parameters it sends and what data it gets back. You can use Postman or something like it to assemble a POST request and work out how the Javascript on that site is getting its data, and then write a little code to make your own call to that URL and parse what comes back.

WebBrowser control must reload content every time app page is revisited (Windows Phone 7, Silverlight)

I'm writing an app for Windows Phone 7/Silverlight. When the app is either tombstoned and reactivated while on the app page containing the WebBrowser control (I've saved the Uri in app state) or that same app page is navigated to by NavigationService.GoBack() or the phone back button, it seems that as long as the control still has the webbrowser.source value, it should then render just fine, but this is not the case. Unless I use the Navigate() method, it shows a white/blank screen, no matter what I try. Unfortunately, using the Navigate() method causes the web content to download again, unnecessarily. It's especially frustrating when only a GoBack() is used to get back to the application page with the WebBrowser control, which is quite frequent in my app.
private void OnWebBrowserLoaded(object sender, RoutedEventArgs e)
{
//webBrowser1.Source = CurrentUri; //does not work, results in white/blank browser page
webBrowser1.Navigate(CurrentUri); //works, but page has to reload from web, bad UX
}
Any suggestions on a way around this problem? I've also tried putting this same code in the page loaded handler. It behaves in the same poor manner.
I've also tried saving off the HTML (SaveToString) and reloading it from app state (NavigateToString), but the web page does not render completely for some reason, even though the HTML appears fine. Also, I'd like to have access to the Host and Uri properties. I could probably work around that, if I could get the HTML to render OK from NavigateToString.
Thanks,
Jay
You should use browsertask:
using Microsoft.Phone.Tasks;
WebBrowserTask browse = new WebBrowserTask();
browse.Uri = new Uri(URL, UriKind.RelativeOrAbsolute);
//new Uri(URL,UriKind.RelativeOrAbsolute);
browse.Show();
This should solve your issue.
URL will be the URL of the page you want to visit.

Categories