WinForm中实现播放mp3 、mp4文件

1、在Vs2012 工具箱右键-->"选择项"-->"Com组件"-->"Windows Media Player" 勾上,然后点击确定。

2、在WinForm 窗体中添加两个 button按钮,一个Windows Media Player,相应

按钮代码如下:

//播放音乐 
private void button1_Click(object sender, EventArgs e)
        {
            FileDialog open = new OpenFileDialog();
            //wxWindowsMediaplayer1是控件名
            this.axWindowsMediaPlayer1.settings.playCount = 2;//播放次数;
            open.Filter = "*.mp3|*.mp3";
            open.Title = "打开文件";
            if (open.ShowDialog() == DialogResult.OK)
            {
                axWindowsMediaPlayer1.URL = open.FileName;
                axWindowsMediaPlayer1.Ctlcontrols.play();//播放文件
            }
        }


 //播放视频
 private void button2_Click(object sender, EventArgs e)
        {
            FileDialog open = new OpenFileDialog();
            //wxWindowsMediaplayer1是控件名
            this.axWindowsMediaPlayer1.settings.playCount = 2;//播放次数;
            open.Filter = "*.mp4|*.mp4"; //文件格式
            open.Title = "打开文件";
            if (open.ShowDialog() == DialogResult.OK)
            {
                axWindowsMediaPlayer1.URL = open.FileName;
                axWindowsMediaPlayer1.Ctlcontrols.play();//播放文件
            }
        }

 

效果图:

 

如果有不对的地方,望园子中的朋友加以指出!

posted @ 2015-04-21 20:59  段晨  阅读(11507)  评论(2编辑  收藏