AxWMPLib.AxWindowsMediaPlayer()出现异常是什么原因
代码如下:   
    wavfile="d:\\mms\\111.wav";
       double playtime;
    axWindowsMediaPlayer1 = new AxWMPLib.AxWindowsMediaPlayer();
    axWindowsMediaPlayer1.URL = wavfile;  
    playtime = Math.Floor(axwmp.currentMedia.duration); axWindowsMediaPlayer1.URL = wavfile;  //就是这句话有问题.
问题提示为:System.Windows.Forms.AxHost+InvalidActiveXStateException: 发生异常.
各位大虾,帮忙看看啊.

解决方案 »

  1.   

    http://www.cnblogs.com/Advance/archive/2005/03/15/119054.html
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;using AxWMPLib;
    using System.IO;
    using System.Xml.Linq;namespace PlayMusic
    {
        public partial class Form1 : Form
        {
            private FileInfo[] fileList;
            private  string fp = Environment.CurrentDirectory + @"\FolderPath.xml";
            private string _folderPath = String.Empty;
            private AxWindowsMediaPlayer player=new AxWindowsMediaPlayer();
            public Form1()
            {
                InitializeComponent();
                //player.BeginInit();
                if (File.Exists(fp)) {
                    var pathXml = XElement.Load(fp);
                    _folderPath = pathXml.Value;
                    Fill();
                    //激活控件
                    this.Controls.Add(player);
                    player.EndInit();
                   
                    
                                }
            }
            //获取文件夹路径并保存
            private void broButton_Click(object sender, EventArgs e)
            {
                var fbd = new FolderBrowserDialog();
                fbd.ShowNewFolderButton  =false;
                if (fbd.ShowDialog() == DialogResult.OK) {
                    checkedListBox1.Items.Clear();
                    if (!File.Exists(fp))
                    {
                        var pathXml = new XElement("MusicFolderPath", fbd.SelectedPath);
                        pathXml.Save(fp);
                    }
                    else {
                        var pathXml = XElement.Load(fp);
                        pathXml.SetValue(fbd.SelectedPath);
                        pathXml.Save(fp);
                    }
                    _folderPath = fbd.SelectedPath;
                    Fill();
                }
                            }
            //返回文件列表
            private List<string> GetAllFile(string folderPath) {
                var lis = new List<string>();
                var dir = new DirectoryInfo(folderPath);
                fileList = dir.GetFiles();
                foreach (FileInfo f in fileList)
                {
                    if (Path.GetExtension(f.Name).ToLower() == ".mp3" || Path.GetExtension(f.Name).ToLower() == ".wmv") 
                    {
                        lis.Add(f.Name);
                    }
                }
                return lis;        }
            //填充选择列表框
            private void Fill() {
                if (_folderPath == String.Empty)
                    return;
                if (GetAllFile(_folderPath).Count ==0 )
                    return;
                foreach (var i in GetAllFile(_folderPath)) 
                {
                    checkedListBox1.Items.Add(Path.GetFileNameWithoutExtension(i));
                }
                checkedListBox1.SelectedIndex = 0;
            }
           
            private void playButton_Click(object sender, EventArgs e)
            {
                PlayMusic(fileList[checkedListBox1.SelectedIndex+2].FullName);
            }
            //播放
            private void PlayMusic(string filePath) {
               
                try
                {
                    player.URL = filePath;
                    player.Ctlcontrols.play();            }
                catch(Exception exp) {
                    MessageBox.Show(exp.Message);
                }
            }     
      
            
        }
    }
      

  3.   

    我也 遇到同类的问题  后来 加上 这个 就好了
      //激活控件 
                    this.Controls.Add(player); 
                    player.EndInit();