1

第一次尝试:我通过在我的项目中添加 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();
        }
    }

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

4

1 回答 1

1

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

 <body>
<form id="form1" runat="server">

    <%-- Video DIV --%>
    <div runat="server" id="VidDiv" class="fullscreen-bg">
    </div>
    <%-- IMAGE DIV --%>
    <div runat="server" id="container">
        <div runat="server" id="containers">
        </div>
        <%-- this is where the code gets dynamically created --%>
    </div>
</form>
 </body>
   <script src="Scripts/jquery-3.1.1.js"></script>
  <script id="CallMyFunction" type="text/javascript">
          var index = 1;
    document.getElementById("VidDiv").style.display = "none";

var ImageCount = 1;
autoSlide();

function autoSlide() {

    var x = document.getElementsByClassName("Images");
    var video_player = document.getElementsByClassName("fullscreen-bd__video");
    var count;
    var video;
    var videoSource = new Array(), vids, i;

    for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
    }
    if (index > x.length) {
        index = 1
    }

    x[index - 1].style.display = "block";

    count = x.length;

    if (ImageCount <= count) {
        index++;
        ImageCount = 1 + ImageCount;
        setTimeout(autoSlide, 8000);
    }
    else {
        //this is where we should switch from image div to video div 
        document.getElementById("container").style.display = "none";
        document.getElementById("VidDiv").style.display = "block";

        //create a counter to check the number of video tags
        video = document.getElementsByTagName('video'), numVideos = video.length;

        for (i = 0; i < numVideos; i++) {

            videoSource[i] = video.item(i).src;
            document.getElementById("myVideo" + i).style.display = "none";
        }

        var videoCount = 0;
        if (videoCount <= numVideos -1) {

            function videoPlay(videoNum) {
                if (videoCount > 0)
                {
                    document.getElementById("myVideo" + (videoCount - 1)).style.display = "none";
                }
                document.getElementById("myVideo" + videoCount).style.display = "block";

                document.getElementById("myVideo" + videoCount).setAttribute("src", "" + videoSource[videoCount] + "");
                document.getElementById("myVideo" + videoCount).load();
                document.getElementById("myVideo" + videoCount).play();
                onEndedVid = document.getElementById("myVideo" + videoCount);

                var onEndedVid;
                onEndedVid.onended = function () {
                    //at the end of the video, close full screen
                    myHandler();
                };

                videoCount = videoCount + 1;

            }

            function myHandler() {

                if (videoCount == numVideos) {
                    //this is where we should switch from image div to video div
                    document.getElementById("container").style.display = "none";
                    document.getElementById("VidDiv").style.display = "none";

                    location.reload();
                }
                else {
                    videoPlay(videoCount);
                }
            }
            myHandler();

        }
        else
        {
            ///back to images
            //refresh the page
            location.reload();
        }           
    }
}

后面的代码:

// 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;
        }
    }

代码显然可以改进和优化,但这是我们用来让我们的应用程序运行的基础。感谢所有的建议和意见!

于 2017-11-13T09:47:43.820 回答