Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:17:03+00:00 2026-06-15T03:17:03+00:00

My goal is to make a open source YouTube player that can be controlled

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:17:04+00:00Added an answer on June 15, 2026 at 3:17 am

    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:

    player.movie = "http://www.youtube.com/v/VIDEO_ID?version=3&enablejsapi=1"
    

    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 FlashCall event to the function that will handle the flash->c# interaction (in my example it’s YTplayer_FlashCall )

    the generated `InitializeComponent()` should be:

    ...
    this.YTplayer = new AxShockwaveFlashObjects.AxShockwaveFlash();
    this.YTplayer.Name = "YTplayer";
    this.YTplayer.Enabled = true;
    this.YTplayer.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("YTplayer.OcxState")));
    this.YTplayer.FlashCall += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(this.YTplayer_FlashCall);
    ...
    

    the FlashCall event handler

    private void YTplayer_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)
    {
        Console.Write("YTplayer_FlashCall: raw: "+e.request.ToString()+"\r\n");
        // message is in xml format so we need to parse it
        XmlDocument document = new XmlDocument();
        document.LoadXml(e.request);
        // get attributes to see which command flash is trying to call
        XmlAttributeCollection attributes = document.FirstChild.Attributes;
        String command = attributes.Item(0).InnerText;
        // get parameters
        XmlNodeList list = document.GetElementsByTagName("arguments");
        List<string> listS = new List<string>();
        foreach (XmlNode l in list){
            listS.Add(l.InnerText);
        }
        Console.Write("YTplayer_FlashCall: \"" + command.ToString() + "(" + string.Join(",", listS) + ")\r\n");
        // Interpret command
        switch (command)
        {
            case "onYouTubePlayerReady": YTready(listS[0]); break;
            case "YTStateChange": YTStateChange(listS[0]); break;
            case "YTError": YTStateError(listS[0]);  break;
            default: Console.Write("YTplayer_FlashCall: (unknownCommand)\r\n"); break;
        }
    }
    

    this will resolve the flash->c# communication

    calling the flash external functions (c#->flash):

    private string YTplayer_CallFlash(string ytFunction){
        string flashXMLrequest = "";
        string response="";
        string flashFunction="";
        List<string> flashFunctionArgs = new List<string>();
    
        Regex func2xml = new Regex(@"([a-z][a-z0-9]*)(\(([^)]*)\))?", RegexOptions.Compiled | RegexOptions.IgnoreCase);
        Match fmatch = func2xml.Match(ytFunction);
    
        if(fmatch.Captures.Count != 1){
            Console.Write("bad function request string");
            return "";
        }
    
        flashFunction=fmatch.Groups[1].Value.ToString();
        flashXMLrequest = "<invoke name=\"" + flashFunction + "\" returntype=\"xml\">";
        if (fmatch.Groups[3].Value.Length > 0)
        {
            flashFunctionArgs = pars*emphasized text*eDelimitedString(fmatch.Groups[3].Value);
            if (flashFunctionArgs.Count > 0)
            {
                flashXMLrequest += "<arguments><string>";
                flashXMLrequest += string.Join("</string><string>", flashFunctionArgs);
                flashXMLrequest += "</string></arguments>";
            }
        }
        flashXMLrequest += "</invoke>";
    
        try
        {
            Console.Write("YTplayer_CallFlash: \"" + flashXMLrequest + "\"\r\n");
            response = YTplayer.CallFunction(flashXMLrequest);                
            Console.Write("YTplayer_CallFlash_response: \"" + response + "\"\r\n");
        }
        catch
        {
            Console.Write("YTplayer_CallFlash: error \"" + flashXMLrequest + "\"\r\n");
        }
    
        return response;
    }
    
    private static List<string> parseDelimitedString (string arguments, char delim = ',')
    {
        bool inQuotes = false;
        bool inNonQuotes = false;
        int whiteSpaceCount = 0;
    
        List<string> strings = new List<string>();
    
        StringBuilder sb = new StringBuilder();
        foreach (char c in arguments)
        {
            if (c == '\'' || c == '"')
            {
                if (!inQuotes)
                    inQuotes = true;
                else
                    inQuotes = false;
    
                whiteSpaceCount = 0;
            }else if (c == delim)
            {
                if (!inQuotes)
                {
                    if (whiteSpaceCount > 0 && inQuotes)
                    {
                        sb.Remove(sb.Length - whiteSpaceCount, whiteSpaceCount);
                        inNonQuotes = false;
                    }
                    strings.Add(sb.Replace("'", string.Empty).Replace("\"", string.Empty).ToString());
                    sb.Remove(0, sb.Length);                       
                }
                else
                {
                    sb.Append(c);
                }
                whiteSpaceCount = 0;
            }
            else if (char.IsWhiteSpace(c))
            {                    
                if (inNonQuotes || inQuotes)
                {
                    sb.Append(c);
                    whiteSpaceCount++;
                }
            }
            else
            {
                if (!inQuotes) inNonQuotes = true;
                sb.Append(c);
                whiteSpaceCount = 0;
            }
        }
        strings.Add(sb.Replace("'", string.Empty).Replace("\"", string.Empty).ToString());
    
    
        return strings;
    }
    

    adding Youtube event handlers:

    private void YTready(string playerID)
    {
        YTState = true;
        //start eventHandlers
        YTplayer_CallFlash("addEventListener(\"onStateChange\",\"YTStateChange\")");
        YTplayer_CallFlash("addEventListener(\"onError\",\"YTError\")");
    }
    private void YTStateChange(string YTplayState)
    {
        switch (int.Parse(YTplayState))
        {
            case -1: playState = false; break; //not started yet
            case 1: playState = true; break; //playing
            case 2: playState = false; break; //paused
            //case 3: ; break; //buffering
            case 0: playState = false; if (!loopFile) mediaNext(); else YTplayer_CallFlash("seekTo(0)"); break; //ended
        }
    }
    private void YTStateError(string error)
    {
        Console.Write("YTplayer_error: "+error+"\r\n");
    }
    

    usage ex:

    YTplayer_CallFlash("playVideo()");
    YTplayer_CallFlash("pauseVideo()");
    YTplayer_CallFlash("loadVideoById(KuNQgln6TL0)");
    string currentVideoId = YTplayer_CallFlash("getPlaylist()");
    string currentDuration = YTplayer_CallFlash("getDuration()");
    

    The functions YTplayer_CallFlash, YTplayer_FlashCall should work for any flash-C# communication with minor adjustments like the YTplayer_CallFlash‘s switch (command).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal is to make a dialog that appears on menu_key pressed, but it keeps
My goal is to make a layout that is 200% width and height, with
My goal is to make the Delete key (without any modifiers) move selected files
Goal I am trying to create a port (Macports) for an open source tool
My goal is to make a module that will make a grid on a
My goal here is to make a button. I want the text to sit
I'm re-writing alibrary with a mandate to make it totally allocation free. The goal
Goal : I wants when I drag image it become fade so we can
Goal: Produce an Excel document with information from 3 associated models that is similar
My ultimate goal is to create an eclipse plugin that sets up a PDT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.