C#學習11

來源:互聯網
上載者:User

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"));            }        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.