C#學習11
使用Windows表單應用程式,基於Windows Media Player組件寫一個簡單的播放器,功能比較簡陋,通過開啟檔案按鈕來選取音樂所在的地址,還有4個按鈕,分別是:播放,結束,暫停/繼續,歌曲資訊
添加AxWindowsMediaPlayer控制項的方法:在”工具箱”中單擊右鍵,選擇”選擇項”菜單,開啟”選擇工具箱項”視窗,選擇”COM”組件”標籤,在列表中找到並勾選”Windows Media Player”組件,單擊”確定”按鈕。將該組件添加到指定的工具箱選項卡中,然後在工具箱裡面找 Windows Media Player 控制項,拉到form裡面即可
OpenFileDialog控制項也許注意
賦個,按暫停按鈕時停止播放,再按一次就繼續播放
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;namespace cam{ public partial class Cambridge : Form { public Cambridge() { InitializeComponent(); } private void ButPlay_Click(object sender, EventArgs e) //載入多媒體檔案 { this.axWindowsMediaPlayer1.URL = this.optFile.FileName; } private void ButOpen_Click(object sender, EventArgs e) { this.optFile.ShowDialog(); this.axWindowsMediaPlayer1.newMedia(this.optFile.FileName); } private void ButStop_Click(object sender, EventArgs e) //停止 { this.axWindowsMediaPlayer1.Ctlcontrols.stop(); } private void ButPause_Click(object sender, EventArgs e) { if (this.ButPause.Text == "暫停") //暫停播放 { this.axWindowsMediaPlayer1.Ctlcontrols.pause(); this.ButPause.Text = "繼續"; } else { this.axWindowsMediaPlayer1.Ctlcontrols.play();//繼續播放 this.ButPause.Text = "暫停"; } } private WMPLib.WindowsMediaPlayerClass c;//媒體對象 private WMPLib.IWMPMedia m; //建立媒體播放清單對象 private void ButInfo_Click(object sender, EventArgs e) { if (this.optFile.FileName != "optFile") //執行個體化WindowsMediaPlayerClass類的對象 { c = new WMPLib.WindowsMediaPlayerClass(); m = c.newMedia(this.optFile.FileName); MessageBox.Show("歌手名:" + m.getItemInfo("Author") + "\r\n" + "歌名:" + m.getItemInfo("Title")); } } }}