ASP.NET C#中的Windows Media Player

时间:2017-04-05 10:04:24

标签: c# asp.net windows-media-player wmplib

首次尝试:我通过在项目中添加WMPLib作为参考,以编程方式创建了Windows Media Player。我试图在ASP.Net网页(Visual Studio 2015)中使用Windows Media Player播放播放列表。我无法使用HTML 5示例中使用的视频标记,因为我需要在控件中显示.wmv,.mp4,.jpg格式。当我运行代码时,没有显示错误,我看到一个空的浏览器,我错过了什么? 这是我的示例代码:

    WMPLib.WindowsMediaPlayer Player;

    protected void Page_Load(object sender, EventArgs e)
    {
        FileNames();            
    }

    public void FileNames()
    {
        String[] extentions = { "*.wmv", "*.mp4", "*.jpg" };
        List<string> files = new List<string>();
        foreach (string filter in extentions)
        {
            files.AddRange(System.IO.Directory.GetFiles(@"C:\Documents\", filter));
        }   

        foreach (string ss in files)
        {
            String name = System.IO.Path.GetFileName(ss);
            Player = new WMPLib.WindowsMediaPlayer();

            WMPLib.IWMPPlaylist playList = Player.newPlaylist("myPlayList", "");
            playList.appendItem(Player.newMedia(name));
            Player.currentPlaylist = playList;
            Player.controls.play();
        }
    }

我知道硬编码路径不是很好的做法,但我只需要在本地机器上显示。 谢谢!

1 个答案:

答案 0 :(得分:0)

经过两周的研究,我们设法通过根据需要显示的内容在不同类型的控件之间切换来提出解决方案。为了显示PowerPoint幻灯片,我们将所有幻灯片转换为图像,然后循环播放该集合。以下是代码片段,以防其他人需要一些类似问题的指导:

// this will be a watcher that checks if is new content... if there is, delete the existing .wpl file and recreate the .wpl with new content links included        
    private void CreateNewPlayList(string folder)
    {
        try
        {
            System.Threading.Thread.Sleep(5000);
            fileName = getDrive(folder) + @"\" + folder + "Playlist.wpl";
            FileInfo fileInfo = new FileInfo(fileName);

            String f = @"<?wpl version=""1.0""?> 
           <smil>
           <head><meta name=""Generator"" content=""Microsoft Windows Media 
         Player -- 10.0.0.3646""/>   
           <author/>
          <title> a title goes here </title>
          </head>
          <body>
          <seq> ";
              String ff = @"
         </seq> 
         </body> 
         </smil>";
               using (FileStream fs = fileInfo.Create())
            {
                Byte[] txt = new UTF8Encoding(true).GetBytes(f);
                fs.Write(txt, 0, txt.Length);

                ////write paths and load only certain file types according to requirements into array
                String[] extentions = { "*.mp4", "*.wmv", "*.JPG".ToLower(), "*.ppt", "*.png" };

                List<string> files = new List<string>();

                foreach (string filter in extentions)
                {
                    files.AddRange(System.IO.Directory.GetFiles(getDrive(folder) + @"\", filter));
                }

                int filecount = files.Count;
                string[] video_lists = new string[files.Count];

                int counts = 0;
                foreach (string file in files)
                {
                    video_lists[counts] = file.ToString();
                    string PathfileName = Path.GetFileName(file);
                    Byte[] author;
                    //use the ppt to be able to go into the folder and add each slide as part of the playlist
                    if (Path.GetExtension(PathfileName) == ".ppt" || Path.GetExtension(PathfileName) == ".pptx")
                    {
                        //create a loop to loop through the folder that has the same name as ppt/pptx(PathFileName)
                        string pptDrive = getDrive(folder) + @"\" + Path.GetFileNameWithoutExtension(PathfileName) + @"\";
                        if (Directory.Exists(pptDrive))
                        {
                            string[] pptFilesFolder = Directory.GetFiles(pptDrive);
                            int counter = 1;
                            while (counter <= pptFilesFolder.Length)
                            {
                                foreach (string pptFile in pptFilesFolder)
                                {
                                    string pptFileName = Path.GetFileName(pptFile);
                                    string pptFileNameNoExt = Path.GetFileNameWithoutExtension(pptFile);                                                                         

                                    int i = pptFilesFolder.Length;
                                    int ss = Convert.ToInt16(new String(pptFileNameNoExt.Where(Char.IsDigit).ToArray()));
                                    if (ss <= i && ss == counter)
                                    {
                                        author = new UTF8Encoding(true).GetBytes(@"<media src=""" + pptDrive + @"\" + pptFileName + "\"/>");
                                        fs.Write(author, 0, author.Length);
                                        counter++;
                                    }
                                }
                            }
                        }
                        else
                        {
                            //do something...
                        }

                    }
                    else
                    {
                        author = new UTF8Encoding(true).GetBytes(@"<media src=""" + getDrive(folder) + @"\" + PathfileName + "\"/>");
                        fs.Write(author, 0, author.Length);
                    }
                    counts = counts + 1;
                }

                Byte[] toptxt = new UTF8Encoding(true).GetBytes(ff);
                fs.Write(toptxt, 0, toptxt.Length);

            }

        }
        catch (IOException io)
        {
            //error handling....

            return;
        }
        catch (Exception ex)
        {
            //error handling...
            return;
        }
    }

代码背后:

AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectClass", "ldapsubentry"));
filter.and(new EqualsFilter("cn", "ABCD"));
List<ModelandRulsetVO> result = ldapTemplate.search(userSearchBase, filter.encode(), 2,new ModelAndRuleSetMapper());

显然可以改进和优化代码,但这是我们用来使应用程序正常工作的基础。感谢您的所有建议和意见!