My goal is to make a open source YouTube player that can be controlled via global media keys.
The global key issue I got it covered but the communication between the YouTube player and my Windows Forms application just doesn’t work for some reason.
So far this is what I have:
private AxShockwaveFlashObjects.AxShockwaveFlash player;
player.movie = "http://youtube.googleapis.com/v/9bZkp7q19f0"
...
private void playBtn_Click(object sender, EventArgs e)
{
player.CallFunction("<invoke name=\"playVideo\" returntype=\"xml\"></invoke>");
}
Unfortunately this returns:
"Error HRESULT E_FAIL has been returned from a call to a COM component."
What am I missing? Should I load a different URL?
The documentation states that YouTube player uses ExternalInterface class to control it from JavaScript or AS3 so it should work with c#.
UPDATED:
Method used to embed the player: http://www.youtube.com/watch?v=kg-z8JfOIKw
Also tried to use the JavaScript-API in the WebBrowser control but no luck (player just didn’t respond to JavaScript commands, tried even to set WebBrowser.url to a working demo, all that I succeeded is to get the onYouTubePlayerReady() to fire using the simple embedded object version )
I think there might be some security issues that I’m overseeing, don’t know.
UPDATE 2:
fond solution, see my answer below.
After a lot of tries and head-hammering, I’ve found a solution:
Seems that the
Error HRESULT E_FAIL...happens when the flash dosen’t understand the requested flash call. Also for the youtube external api to work, the js api needs to be enabled:As I said in the question the whole program is open source, so you will find the full code at bitbucket.
Any advice, suggestions or collaborators are highly appreciated.
The complete solution:
Here is the complete guide for embedding and interacting with the YouTube player or any other flash object.
After following the video tutorial
, set the flash player’s
FlashCallevent to the function that will handle the flash->c# interaction (in my example it’sYTplayer_FlashCall)the generated `InitializeComponent()` should be:
the FlashCall event handler
this will resolve the flash->c# communication
calling the flash external functions (c#->flash):
adding Youtube event handlers:
usage ex:
The functions
YTplayer_CallFlash,YTplayer_FlashCallshould work for any flash-C# communication with minor adjustments like theYTplayer_CallFlash‘s switch (command).