C# Windows Media Player操作

右擊工具箱->選擇項 -> 顯示"選擇工具箱項" -> COM組件 -> Windows Media Player wmp.dll 添加

MyMediaPlayer窗體

public static string path = "";//記錄文件完整路徑
string duration = "";//當前文件播放的時間
int width = 0;//播放文件的寬度
int height = 0;//播放文件的高度
//存放路徑key為文件名,value為完整路徑
private Dictionary<string, string> pathList = new Dictionary<string,string>();
private void 打開文件ToolStripMenuItem_Click(object sender, EventArgs e)
{

        this.OpenFile();
        //清除播放列表中所有的值
        this.listView1.Items.Clear();
        //清除集合
        pathList.Clear();
        //添加到Listview中
        listViewUpdate(Path.GetFileName(path), path);
        //播放
        axWmp.URL = path;
        //當打開播放文件,啟動timer控件,得到文件的時間,和寬度高度。
        //如果放在當前位置,得到的數(shù)值為0,
        //可能因為媒體文件的打開需要一定時間,這里等待媒體文件的打開
         timer1.Start();

    }
    private void OpenFile() {
        //打開一個文件
        OpenFileDialog ofd = new OpenFileDialog();
        DialogResult dr = ofd.ShowDialog();
        if (dr == DialogResult.Cancel)
        { //取消打開
            return;
        }
        //否則記錄打開路徑
        path = ofd.FileName;

    }
    private void 屬性ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //只記錄文件名
        //String s=Path.GetFileNameWithoutExtension(path);
        //記錄文件名和擴展名
        string ss = Path.GetFileName(path);
        //播放文件的信息
        MusicInfo mi = new MusicInfo(ss,duration,width,height);
        mi.ShowDialog();

    }
    private void 播放ToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        //播放文件
        this.axWmp.Ctlcontrols.play();

    }

    private void 暫停ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //暫停播放
        this.axWmp.Ctlcontrols.pause();
    }

    private void 停止ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.axWmp.Ctlcontrols.stop();
        //清除圖像,等待就須狀態(tài)
        this.axWmp.close();
    }

    private void 升高音量ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.axWmp.settings.volume += 5;

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
     //得到文件播放總時間和大小
        duration = this.axWmp.currentMedia.durationString;
        width = axWmp.currentMedia.imageSourceWidth;
        height = axWmp.currentMedia.imageSourceHeight;

    }

    private void 降低音量ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.axWmp.settings.volume -= 5;
    }

    private void 靜音ToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // this.axWmp.settings.volume = 0;
        if (this.axWmp.settings.mute == true) {//true代表靜音
            this.axWmp.settings.mute =false; 
        }
        else if (this.axWmp.settings.mute == false) {
            this.axWmp.settings.mute = true;
        }

    }

    private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        //選擇一個文件
        this.OpenFile();
        //打開路徑取出文件名及其擴展名
        string musicFileName = Path.GetFileName(path);
        listViewUpdate(musicFileName,path);

    }
    //添加到listview
    //文件名和文件完整路徑名
    private void listViewUpdate(string name,string path) {
        //判斷是否包含鍵值
        if (pathList.ContainsKey(name)) {//包含返回真
            return;
        }
        pathList.Add(name, path);

        ListViewItem lvi = new ListViewItem(name);
        listView1.Items.Add(lvi);

    }

    private void listView1_DoubleClick(object sender, EventArgs e)
    {
        //通過文件名,獲得完整路徑
        this.axWmp.URL =pathList[listView1.SelectedItems[0].Text];
    }

    private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        //刪除播放列表項
        //1首先刪除集合中的值,然后刪除Listview
        if (listView1.SelectedItems.Count == 0) {
            return;
        }
        pathList.Remove(listView1.SelectedItems[0].Text);
        //從listview1中刪除
        //刪除以后還能播放
        this.listView1.Items.Remove(listView1.SelectedItems[0]);

    }

    private void 關于ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        About a = new About();
        a.ShowDialog();
    }

MusicInfo窗體

private string Info="";//獲取另一個窗口的信息
private string Duration;
private int Width=0;
private int Height=0;
public MusicInfo(string info,string duration,int width,int height) {
Info = info;
Duration = duration;
Width = width;
Height = height;
InitializeComponent();
}

    private void Form2_Load(object sender, EventArgs e)
    {
        //判斷是否有文件播放
        if (String.IsNullOrEmpty(Info))
        {
            this.lblFileName.Text = "無播放文件";
        }
        else {
            this.lblFileName.Text = Info;
        }

        //Graphics gh = this.CreateGraphics();
        //gh.DrawLine(Pens.Blue, new Point(100, 10), new Point(100, 300));
        //判斷文件類型
        string Extensiona = Path.GetExtension(Info);
        if (Extensiona == ".wmv")
        {
            this.lblFileType.Text = "視頻";
        }
        else {
            this.lblFileType.Text = "音頻";
        }
        //獲取文件大小
        if (string.IsNullOrEmpty(Info))
        {
            this.lblFileSize.Text = "0MB";

        }
        else {
            FileInfo fi = new FileInfo(Info);
            long fileSize = fi.Length / 1024 / 1024;
            this.lblFileSize.Text = fileSize + "MB";
        }

        //當前播放時間
        this.lblDuration.Text = Duration;

        //分辨率
        if (Width == 0 &&Height == 0) {
            lblResolution.Text = "無視頻";
        }
        this.lblResolution.Text = Width + "x" + Height;
        //詳細路徑
        //第二種方法獲得另一個窗體的路徑
        //當創(chuàng)建新的對象path路徑為空,做靜態(tài)成員
       // MyMediaPlayer MP = new MyMediaPlayer();
        this.lblFilePositon.Text =MyMediaPlayer.path;

    }

1、CD、VCD音頻播放

首先需要添加Wiindows Media Player組件,方法:

(1)“工具箱”右鍵“選擇項”。

(2)在彈出的“選擇工具箱項”對話框中選擇“COM組件”選項卡。

(3)在COM組件列表中選擇Windows Media Player,單擊確定,ok。

事例代碼:

private void 選取文件_Click(object sender, EventArgs e)
{
this.optFile.ShowDialog();
this.axWindowsMediaPlayer1.newMedia(this.optFile.FileName);
}

private void 播放文件_Click(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.URL = this.optFile.FileName;
}

private void 停止_Click(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.close();
}

暫停: this.axWindowsMediaPlayer1.Ctlcontrols.pause();

繼續(xù)播放:this.axWindowsMediaPlayer1.Ctlcontrols.play();

獲取歌曲信息:

private WMPLib.WindowsMediaPlayerClass c;
private WMPLib.IWMPMedia m;

private void ButInfo_Click(object sender, EventArgs e)
{
if (this.optFile.FileName != "optFile")
{
c = new WMPLib.WindowsMediaPlayerClass();
m = c.newMedia(this.optFile.FileName);
MessageBox.Show("歌手名:" + m.getItemInfo("Author") + "/r/n" + "歌 名:" + m.getItemInfo("Title"));
}
}

2、MP3 、WAV播放

1)帶記憶功能的MP3 播放器

具有帶記憶功能只需要在程序退出時,將當前用戶選擇的文件保存到ini文件中就可以了。

如:在打開文件的同時寫入到ini文件中:

string strpath;
public Form1() //構造函數(shù)
{
InitializeComponent();
strpath = System.Environment.CurrentDirectory;
}
private void button1_Click(object sender, EventArgs e)
{
this.openFileDialog1.FileName = "";
this.openFileDialog1.ShowDialog();
StreamWriter s = new StreamWriter(strpath + "http://HyList.ini", true);
s.WriteLine(openFileDialog1.FileName);
s.Flush();
s.Close();
ShowWindows(openFileDialog1.FileName); //調用函數(shù)
}

public void ShowWindows(string fileName)
{
this.listBox1.Items.Add(fileName);
}

在打開窗體的時候加載ini文件中的數(shù)據(jù):

private void Form1_Load(object sender, EventArgs e)
{
string str = Application.StartupPath;
StreamReader sr = new StreamReader(str + "http://HyList.ini");
while (sr.Peek() >= 0)
{
string strk=sr.ReadLine();
if (strk!="")
listBox1.Items.Add(strk);
}
sr.Close();
}

播放選中文件:

private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count > 0)
{
string strPath = listBox1.SelectedItem.ToString();

ShowPlay(strPath);
}
}

2)、自動播放的MP3播放器

(1)自動添加播放列表:

private void button1_Click(object sender, EventArgs e)
{
this.listBox1.Items.Clear();
if (this.folderBrowserDialog1.ShowDialog()==DialogResult.OK)
{
DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
GetAllFiles(dir); //調用函數(shù)
}
}

public void GetAllFiles(DirectoryInfo dir)
{
this.listBox1.Items.Clear();
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回表示某個目錄中所有文件和子目錄的強類型System.IO.FileSystemInfo項的數(shù)組
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo)
{
GetAllFiles((DirectoryInfo)i);
}
else
{
string str = i.FullName;
int b = str.LastIndexOf("http://");
string strbbb = str.Substring(b + 1);
if (strbbb.Substring(strbbb.Length - 3) == "mp3")
{
this.listBox1.Items.Add(str.Substring(b + 1));
//添加列表
WC = new WMPLib.WindowsMediaPlayerClass();
MC = WC.newMedia(str);
this.axWindowsMediaPlayer1.currentPlaylist.appendItem(MC);
}
}
}
}

播放所有文件:

private void button2_Click(object sender, EventArgs e)
{

if (MC != null)
this.axWindowsMediaPlayer1.Ctlcontrols.play();
else
MessageBox.Show("請?zhí)砑游募斜?);
}

3、播放動畫

1)播放Flash動畫

首先需要計算機中有Flash插件,添加過程如下:

(1)選擇“工具箱”,單擊鼠標右鍵,在彈出的快捷菜單中選擇“選擇項”。

(2)彈出“選擇工具箱項”對話框,選擇“COM組件”選擇卡。

(3)在COM組件列表中,單擊[瀏覽]按鈕,在對話框中選擇“//SYSTEM32/Macromed/Flash/SWFLASH.OCX”.

打開文件事件:

try
{
openFileDialog1.Filter = "Flash文件(.swf)|.swf|所有文件(.)|.";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string MyFileName = openFileDialog1.FileName;
this.axShockwaveFlash1.Movie = MyFileName;
}
}
catch (Exception ey)
{
MessageBox.Show(ey.Message);
}

//播放
private void button6_Click(object sender, EventArgs e)
{
this.axShockwaveFlash1.Rewind();
this.axShockwaveFlash1.Play();
}

//第一幀
private void button3_Click(object sender, EventArgs e)
{
this.axShockwaveFlash1.Rewind();
}

//上一幀
private void button4_Click(object sender, EventArgs e)
{
this.axShockwaveFlash1.Back();
}

//下一幀
private void button5_Click(object sender, EventArgs e)
{
this.axShockwaveFlash1.Forward();
}

//停止
private void button2_Click(object sender, EventArgs e)
{
this.axShockwaveFlash1.Stop();
}

2)播放制作AVI播放器

需添加“COM組件”中“Microsoft Animation Control,Version 6.0”

主要方法: open()方法,eg. this.axAnimation1.Open(Application.StartupPath + "http://clock.avi");

stop()方法,

Play()方法,播放文件。參數(shù)this.axAnimation1.Play(播放次數(shù), 播放開始幀, 播放結束幀);

4、播放Gif動畫

Bitmap bitmap = new Bitmap(Application.StartupPath+"http://1.gif");
bool current = false;

播放事件:

private void button1_Click(object sender, EventArgs e)
{
PlayImage();
ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));//播放
}

public void PlayImage()
{
if (!current)
{
ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));
current = true;
}
}

停止播放:

private void button2_Click(object sender, EventArgs e)
{
ImageAnimator.StopAnimate(bitmap, new EventHandler(this.OnFrameChanged));//停止
}

轉自:https://blog.csdn.net/suxuelian/article/details/49760475

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一、 1、請用Java寫一個冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨云閱讀 1,412評論 0 6
  • AI系列網址:AI 系列 總目錄 目標需求 使用錄音形式,模擬微信語音聊天。按住錄音,松開發(fā)送語音,并完成語音識別...
    Lambert_lin閱讀 1,616評論 0 0
  • 你最近一次立了flag卻沒有做到,是什么時候?當你發(fā)現(xiàn)自己沒有做到后,會采取什么措施? (話題來自小灶一班) 年前...
    撿到蜜罐的熊閱讀 141評論 0 0
  • 我去年七月份畢業(yè),是一位高齡應屆畢業(yè)生(我1991年出生的,好尷尬呀!)。 在這里簡單說一說自己在畢業(yè)后才懂得的一...
    啰嘹玩家閱讀 245評論 0 1