AxMediaPlayer控件使用方法(非AxWindowsMediaPlayer控件)

一:c#中播放MP3文件

首先,我们要生成MediaPlayer.dll 和 AxMediaPlayer.dll控件:

   方法如下:命令为:aximp c:\winnt\system32\msdxm.ocx 而通常                         msdxm.ocx中的ActiveX控件都未注册! 则先运行regsvr32                        msdxm.ocx手动注册便生成需要的动态连接库文件。

其次:填写程序如下:

private void button1_Click(object sender, System.EventArgs e)
  {//浏览MP3文件
   if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
   {
    this.listView1.Items.Clear();
    string []FileNames=this.openFileDialog1.FileNames;
    foreach(string FileName in FileNames)
    {
     //取得文件大小
     FileInfo MyFileInfo=new FileInfo(FileName);
     float MyFileSize=(float)MyFileInfo.Length/(1024*1024);
     this.axMediaPlayer1.FileName=FileName;
     //取得作者信息
     string MyAuthor=this.axMediaPlayer1.GetMediaInfoString(MediaPlayer.MPMediaInfoType.mpClipAuthor);
     //取得不含路径的文件名
     string MyShortFileName=FileName.Substring(FileName.LastIndexOf("\\")+1);    
     MyShortFileName=MyShortFileName.Substring(0,MyShortFileName.Length-4);
     //填充歌曲列表 
     string[] SubItem={MyShortFileName,MyAuthor,MyFileSize.ToString().Substring(0,4)+"M",FileName};
     ListViewItem Item=new ListViewItem(SubItem);
     this.listView1.Items.Add(Item);
     this.listView1.Items[0].Selected=true;     
    }        
    
   }
  }

  private void button2_Click(object sender, System.EventArgs e)
  {//播放MP3文件
   if(this.listView1.Items.Count>0)
   {
    if(this.listView1.SelectedItems.Count>0)
    {
     int iPos=this.listView1.SelectedItems[0].Index;
     string FileName=this.listView1.Items[iPos].SubItems[3].Text;
     this.axMediaPlayer1.FileName=FileName;
     this.axMediaPlayer1.Play();
    }
   }
   else
   {
    MessageBox.Show("请选择歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
   }
  }


  private void button4_Click(object sender, System.EventArgs e)
  {//暂停播放
   if(this.axMediaPlayer1.FileName.Length>0)
    this.axMediaPlayer1.Pause();  
   else
   {
    MessageBox.Show("请选择歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
   }
  }

  private void button5_Click(object sender, System.EventArgs e)
  {//停止播放
   if(this.axMediaPlayer1.FileName.Length>0)
    this.axMediaPlayer1.Stop();  
   else
   {
    MessageBox.Show("请选择歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
   }
  }

  private void button6_Click(object sender, System.EventArgs e)
  {//上一首歌曲
   if(this.listView1.Items.Count>0)
   {
    if(this.listView1.SelectedItems.Count>0)
    {
     int iPos=this.listView1.SelectedItems[0].Index;
     if(iPos>0)
     {
      this.listView1.Items[iPos-1].Selected=true;
      string FileName=this.listView1.Items[iPos-1].SubItems[3].Text;
      this.axMediaPlayer1.FileName=FileName;
      this.axMediaPlayer1.Play();     
     }
     else
     {
      MessageBox.Show("已经是第一首歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
     }
    }
   }
   else
   {
    MessageBox.Show("请选择歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
   } 
  }

  private void button7_Click(object sender, System.EventArgs e)
  {//下一首歌曲
   if(this.listView1.Items.Count>0)
   {
    if(this.listView1.SelectedItems.Count>0)
    {
     int iPos=this.listView1.SelectedItems[0].Index;
     if(iPos<this.listView1.Items.Count-1)
     {
      this.listView1.Items[iPos+1].Selected=true;
      string FileName=this.listView1.Items[iPos+1].SubItems[3].Text;
      this.axMediaPlayer1.FileName=FileName;
      this.axMediaPlayer1.Play();     
     }
     else
     {
      MessageBox.Show("已经是最后一首歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
     }
    }
   }
   else
   {
    MessageBox.Show("请选择歌曲!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
   }
  
  }

二:C#中播放AVI短片并使背景透明

我发现他所用的MediaPlayer控件是可以播放许多的媒体文件.但是并不能满足我所需要的情况。我的情况是用C#播放一小段AVI文件,并且背景要透明,而不是黑色,或其他颜色,我找了半天也不能找到什么方法可以使MediaPlayer播放的文件为透明的状态,十分郁闷!

但是随后我发现其他用VB的程序中可以实现这个功能,让我十分的高兴,于是我又打开了VB的代码进行了详细的查看,发现在VB中有一个控件为Animation可以播放AVI文件,而且其有一个属性选项是使背景透明。正好能够满足我的需要.可是这也是一个OCX的控件,于是,我先把该控件找到,位置是:C:\WINDOWS\SYSTEM32\COMCT32.ocx,然后我运行regsvr32进行手动注册,注册完毕以后我就在环境中进行引用,如下图:

(但是,后来本人发现原来这个根本不用regsvr32注册,因为.NET早已经注册了,郁闷中~~~~~~~~~~~~~~)

然后把控件拖入到窗体中,布好了局,将其属性AutoPlay设置为true,然后在代码中写入下边一句话即可:

private void Form1_Load(object sender, System.EventArgs e)

{

  this.axAnimation1.Open(Application.StartupPath +@"\FILECOPY.AVI");

}

这样当窗体加载的时候就会自动的播放这一小段AVI文件了.其实,这个问题并不是很难,主要是本人对COM组件并不是特别熟悉的过.只要熟悉这些组件的功能,以及运行的原理,势必会大大的提高我们的编程的效率。

原文地址:https://www.cnblogs.com/tuyile006/p/561351.html