与在 Winforms c# 中播放 youtube 视频的 axSockwavePlayer 交互?

Interacting with axSockwavePlayer playing a youtube video in Winforms c#?

小问题:

我如何从播放 youtube 视频的 axShockwavePlayer 获取事件?

(“视频播放”和“暂停”等事件)


长问题:

我有一个带有 shockwave 播放器的 winform 应用程序

我在其中播放 YouTube 视频,我想知道我是否可以在我的应用程序中与 YouTube 播放器互动。

我正在尝试获取播放器的状态并根据它更改我的背景音乐。

四处搜索我发现了这个问题: How to control the youtube flash player with c#? 和伟大的 answer.

(均为 2012 年)

玩弄它我发现答案似乎太旧了,对我不起作用。

发布的答案包括简单的调试,这就是我点击播放器(并开始播放视频)得到的结果:

ytbPlayer FlashCall: raw: <invoke name="isNaN" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "isNaN()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="
        (function() {
          try {
            yt.www.watch.ads.checkInit();
            return true;
          } catch (e) {
            window['adModuleLoaded'] = true;
            return false;
          }
        })" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "
        (function() {
          try {
            yt.www.watch.ads.checkInit();
            return true;
          } catch (e) {
            window['adModuleLoaded'] = true;
            return false;
          }
        })()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.document.referrer.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.document.referrer.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.top.location.href.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.top.location.href.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.document.referrer.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.document.referrer.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="googletag.cmd.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "googletag.cmd.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="googleGetCompanionAdSlots" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "googleGetCompanionAdSlots()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="google_get_companion_slot_params" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "google_get_companion_slot_params()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.top.location.href.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.top.location.href.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.document.referrer.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.document.referrer.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.document.referrer.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.document.referrer.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.top.location.href.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.top.location.href.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.screen.height.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.screen.height.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.screen.width.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.screen.width.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="navigator.javaEnabled" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "navigator.javaEnabled()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.history.length.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.history.length.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.screen.availHeight.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.screen.availHeight.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.screen.availWidth.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.screen.availWidth.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.screen.colorDepth.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.screen.colorDepth.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.navigator.plugins.length.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.navigator.plugins.length.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.navigator.mimeTypes.length.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.navigator.mimeTypes.length.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="yt.util.activity.getTimeSinceActive" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "yt.util.activity.getTimeSinceActive()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.top.location.href.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.top.location.href.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.document.referrer.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.document.referrer.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="google_show_companion_ad" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "google_show_companion_ad()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="Goog_AdSense_Lidar_sendVastMessage" returntype="xml"><arguments><string>expandedChanged</string><string>741024059.2435002</string><object><property id="opt_bounds"><object><property id="width
"><number>431</number></property><property id="height"><number>62</number></property><property id="expanded"><false/></property><property id="left"><number>20</number></property><property id="top"><number>128</number></property></o
bject></property></object></arguments></invoke>
ytbPlayer_FlashCall: "Goog_AdSense_Lidar_sendVastMessage(expandedChanged741024059.24350024316220128)
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="Goog_AdSense_Lidar_getUrlSignalsList" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "Goog_AdSense_Lidar_getUrlSignalsList()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="Goog_AdSense_Lidar_sendVastMessage" returntype="xml"><arguments><string>expandedChanged</string><string>741024059.2435002</string><object><property id="opt_fullscreen"><false/></property><pro
perty id="opt_bounds"><object><property id="width"><number>431</number></property><property id="height"><number>62</number></property><property id="expanded"><true/></property><property id="left"><number>20</number></property><prop
erty id="top"><number>128</number></property></object></property></object></arguments></invoke>
ytbPlayer_FlashCall: "Goog_AdSense_Lidar_sendVastMessage(expandedChanged741024059.24350024316220128)
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="googletag.cmd.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "googletag.cmd.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="googleGetCompanionAdSlots" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "googleGetCompanionAdSlots()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="google_get_companion_slot_params" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "google_get_companion_slot_params()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.top.location.href.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.top.location.href.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.document.referrer.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.document.referrer.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.document.referrer.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.document.referrer.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.screen.height.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.screen.height.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.screen.width.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.screen.width.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="navigator.javaEnabled" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "navigator.javaEnabled()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.history.length.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.history.length.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.screen.availHeight.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.screen.availHeight.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.screen.availWidth.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.screen.availWidth.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.screen.colorDepth.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.screen.colorDepth.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.navigator.plugins.length.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.navigator.plugins.length.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.navigator.mimeTypes.length.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.navigator.mimeTypes.length.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="yt.util.activity.getTimeSinceActive" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "yt.util.activity.getTimeSinceActive()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.top.location.href.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.top.location.href.toString()
YTplayer_FlashCall: unknownCommand
ytbPlayer FlashCall: raw: <invoke name="window.document.referrer.toString" returntype="xml"><arguments></arguments></invoke>
ytbPlayer_FlashCall: "window.document.referrer.toString()
YTplayer_FlashCall: unknownCommand

这是我使用的代码:

 private void ytbPlayer_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e) //asigned as an event handler for FlashCall
        {
            Console.Write("ytbPlayer 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("ytbPlayer_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;
            }
        }

        private string ytbPlayer_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 = parseDelimitedString(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 = ytbPlayer.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;
        }

        private void YTready(string playerID)
        {
            //YTState = true;
            //start eventHandlers
            ytbPlayer_CallFlash("addEventListener(\"onStateChange\",\"YTStateChange\")");
            ytbPlayer_CallFlash("addEventListener(\"onError\",\"YTError\")");
        }

        private void YTStateChange(string YTplayState)
        {
            switch (int.Parse(YTplayState))
            {
                case -1: gm_Name.Text = "not started"; break; //not started yet
                case 1: Console.WriteLine("playing");  break; //playing
                case 2: Console.WriteLine("paused"); break; //paused
                                                  //case 3: ; break; //buffering
                case 0: Console.WriteLine("YOU"); break; //ended
            }
        }
        private void YTStateError(string error)
        {
            Console.Write("YTplayer_error: " + error + "\r\n");
        }

查看调试代码,没有找到可以让我知道用户点击了shockwaveplayer的关键字。

如何更改代码以匹配当前的 youtube api?

我的问题原来是 youtube API 没有打开,因为我将 ShockwavePlayer 定向到错误的 youtube 视频地址。

而不是:

ytbPlayer.Movie = @"http://www.youtube.com/v/" + video;

我应该写:

ytbPlayer.Movie = @"http://www.youtube.com/v/" + video + "?version=3&enablejsapi=1";