I'm currently creating a WPF app and have a Gecko Browser navigated to an embedded youtube video (www.youtube.com/embed/YId_6G-YLpQ).
So far I've managed to do the following through javascript commands:
document.getElementsByTagName('video')[0].play()
document.getElementsByTagName('video')[0].pause()
However, I couldn't figure out how do I start the video for the very first time. Using the play method doesn't work. I need to run it through the code and not through the physical click since it'll trigger the dispatch timer that will frequently raise the property changed to return the current video time.
So far I have a few dirty approaches just to get by:
Manual
It's not at seamless as I wanted it to be but provided that I don't change videos, I only have to do this once.
Semi-Manual
Here's my code for reference
public RelayCommand PlayYouTubeCommand { get; private set; }
public RelayCommand PauseYouTubeCommand { get; private set; }
public bool PlayYouTubeCanUse(object msg)
{
if (!YouTubeIsPlaying)
return true;
return false;
}
public void PlayYouTube(object msg)
{
try
{
using (Gecko.AutoJSContext js = new Gecko.AutoJSContext(YouTubeBrowser.Window))
{
js.EvaluateScript("document.getElementsByTagName('video')[0].play()");
}
RaisePropertyChanged("YouTubeCurrentTime");
YouTubeIsPlaying = true;
}
catch { MessageBox.Show("Error playing video"); }
}
public bool PauseYouTubeCanUse(object msg)
{
if (YouTubeIsPlaying)
return true;
return false;
}
public void PauseYouTube(object msg)
{
try
{
using (Gecko.AutoJSContext js = new Gecko.AutoJSContext(YouTubeBrowser.Window))
{
js.EvaluateScript("document.getElementsByTagName('video')[0].pause()");
}
RaisePropertyChanged("YouTubeCurrentTime");
YouTubeIsPlaying = false;
}
catch { MessageBox.Show("Error pausing video"); }
}
private GeckoWebBrowser youTubeBrowser;
public GeckoWebBrowser YouTubeBrowser
{
get { return youTubeBrowser; }
set { youTubeBrowser = value; RaisePropertyChanged("YouTubeBrowser"); }
}
Here's the list of properties and methods used:
I used document.getElementsByTagName('video')[0].click()