1. 程式人生 > >使用Window 自帶的控件 axWindowsMediaPlayer 制作播放器

使用Window 自帶的控件 axWindowsMediaPlayer 制作播放器

表屬性 xwindow orm data ren vol tasks ner tel

首先,需要把Medio Player 控件添加到工具箱中,具體操作如下:

在VS編程環境的“工具箱”中單擊右鍵,選擇“選擇項”菜單,打開“選擇工具箱項”窗口,選擇“COM組件”標簽,在列表中找到並勾選“Windows Media Player”組件,單擊“確定”按鈕。將該組件添加到指定的工具箱選項卡中
然後在工具箱裏面找 Windows Media Player 控件,拉到form裏面,拉出來的控件就是AxWindowsMediaPlayer了,打開屬性窗口,在點擊拉過來的控件,可以清楚的看到控件名稱為AxWindowsMediaPlayer1

再次,進行player 的編程操作,默認的控件屬性介紹如下:

屬性/方法名: 說明:
[基本屬性]  
URL:String; 指定媒體位置,本機或網絡地址
uiMode:String; 播放器界面模式,可為Full, Mini, None, Invisible
playState:integer; 播放狀態,1=停止,2=暫停,3=播放,6=正在緩沖,9=正在連接,10=準備就緒
enableContextMenu:Boolean; 啟用/禁用右鍵菜單
fullScreen:boolean; 是否全屏顯示
[controls] wmp.controls //播放器基本控制
controls.play; 播放
controls.pause; 暫停
controls.stop; 停止
controls.currentPosition:double; 當前進度
controls.currentPositionString:string; 當前進度,字符串格式。如“00:23”
controls.fastForward; 快進
controls.fastReverse; 快退
controls.next; 下一曲
controls.previous; 上一曲
[settings] wmp.settings //播放器基本設置
settings.volume:integer; 音量,0-100
settings.autoStart:Boolean; 是否自動播放
settings.mute:Boolean; 是否靜音
settings.playCount:integer; 播放次數
[currentMedia] wmp.currentMedia //當前媒體屬性
currentMedia.duration:double; 媒體總長度
currentMedia.durationString:string; 媒體總長度,字符串格式。如“03:24”
currentMedia.getItemInfo(const string); 獲取當前媒體信息"Title"=媒體標題,"Author"=藝術家,"Copyright"=版權信息,"Description"=媒體內容描述,"Duration"=持續時間(秒),"FileSize"=文件大小,"FileType"=文件類型,"sourceURL"=原始地址
currentMedia.setItemInfo(const string); 通過屬性名設置媒體信息
currentMedia.name:string; 同 currentMedia.getItemInfo("Title")
[currentPlaylist] wmp.currentPlaylist //當前播放列表屬性
currentPlaylist.count:integer; 當前播放列表所包含媒體數
currentPlaylist.Item[integer]; 獲取或設置指定項目媒體信息,其子屬性同wmp.currentMedia


// 創見打開對話框對象實例
OpenFileDialog openFileDialog = new OpenFileDialog();

//設置為可以打開多個文件
openFileDialog.Multiselect = true;

//設置打開文件格式
openFileDialog.Filter = "Mp3文件|*.mp3|Wav文件|*.wav|Wma文件|*.wma|Wmv文件|*.wmv|所有格式|*.*";

//判斷是否單擊確定按鈕
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//建立播放列表,名字為aa
axWindowsMediaPlayer1.currentPlaylist = axWindowsMediaPlayer1.newPlaylist("aa", "");

//遍歷打開的集合
foreach (string fn in openFileDialog.FileNames)
{
//添加播放列表
axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(fn));
}
}
//播放
axWindowsMediaPlayer1.Ctlcontrols.play();
}

using System.IO;
using WMPLib;

public videoPlay()
{
InitializeComponent();

//全屏設置及隱藏鼠標
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = FormBorderStyle.None;
//Cursor.Hide();
//播放器全屏
Rectangle screenSize = System.Windows.Forms.SystemInformation.VirtualScreen;//獲取屏幕的寬和高
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Size = new System.Drawing.Size(screenSize.Width,screenSize.Height);
this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(0, 0);
this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(screenSize.Width, screenSize.Height);
//播放器設置
axWindowsMediaPlayer1.uiMode = "None";//播放器樣式
axWindowsMediaPlayer1.stretchToFit = true;//非全屏狀態時是否伸展到最佳大小
axWindowsMediaPlayer1.enableContextMenu = false;//禁用播放器右鍵菜單

}

private IWMPPlaylist videoList;//創建播放列表
private bool ifLoop = true;//視頻是否循環

//設置是否循環播放
public bool IfLoop
{
get { return ifLoop; }
set { ifLoop = value; }
}

//播放狀態改變時發生
private void axWindowsMediaPlayer1_StatusChange(object sender, EventArgs e)
{
//判斷視頻是否已停止播放
if ((int)axWindowsMediaPlayer1.playState == 1)
{
//停頓2秒鐘再重新播放
//System.Threading.Thread.Sleep(2000);
//重新播放
//axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
//播放
public void videoStart()
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
//列表播放
public void videoListStart()
{
videoPlayList();//重新獲取播放列表
axWindowsMediaPlayer1.Ctlcontrols.play();
}
//暫停
public void videoPause()
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
//重播
public void videoReplay()
{
videoStop();
videoStart();
}
//列表重播
public void videoListReplay()
{
axWindowsMediaPlayer1.currentPlaylist = videoList;//重新載入播放列表
videoStart();
}
//停止播放
public void videoStop()
{
//axWindowsMediaPlayer1.currentPlaylist.clear();//清除列表
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
//視頻靜音
public void videoMute(bool t)
{
axWindowsMediaPlayer1.settings.mute = t;
}
//播放下一個視頻
public void videoNext()
{
//判斷當前所播放的視頻是否是列表的最後一個
if (axWindowsMediaPlayer1.currentMedia.name == axWindowsMediaPlayer1.currentPlaylist.Item[axWindowsMediaPlayer1.currentPlaylist.count - 1].name)
{
}
else
{
axWindowsMediaPlayer1.Ctlcontrols.next();//播放下一個
}
}
//播放上一個媒體
public void videoPrevious()
{ //判斷當前所播放的視頻是否是列表的第一個
if (axWindowsMediaPlayer1.currentMedia.name == axWindowsMediaPlayer1.currentPlaylist.Item[0].name)
{
}
else
{
axWindowsMediaPlayer1.Ctlcontrols.previous();//播放上一個
}
}

//獲取播放類表及初始化
public void videoPlayList()
{
videoList = axWindowsMediaPlayer1.playlistCollection.newPlaylist("one");//創建播放列表
string path = @".\data\video";//媒體路徑
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
{
if (fsi is FileInfo)
{
FileInfo fi = (FileInfo)fsi;
videoList.appendItem(axWindowsMediaPlayer1.newMedia(fi.FullName));
}
}
axWindowsMediaPlayer1.currentPlaylist = videoList;//查找到視頻、播放類表
axWindowsMediaPlayer1.settings.setMode("loop", ifLoop);//設置類表循環播放
}

第三,簡單應用實例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WMPLib;

namespace Audio_Player
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            lstFiles.View = View.List;
           AddFilesToolStripMenuItem.Click += AddFilesToolStripMenuItem_Click;
            ExitToolStripMenuItem.Click += ExitToolStripMenuItem_Click;
            HelpToolStripMenuItem.Click += HelpToolStripMenuItem_Click;
            lstFiles.DoubleClick += LstFiles_DoubleClick;
            
        }

        private void HelpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Audio Player  By  Kevin Lu \r\r" +
                            "Tel: 1234567898");
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

        private void AddFilesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //List<string> filePath=new List<string>();
            OpenFileDialog fileDialog=new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Filter = "Mp3文件|*.mp3|Wav文件|*.wav|Wma文件|*.wma|Wmv文件|*.wmv|所有格式|*.*";
            if (fileDialog.ShowDialog()==DialogResult.OK)
            {
                axWindowsMediaPlayer1.currentPlaylist = axWindowsMediaPlayer1.newPlaylist("新建列表", "");
                foreach (string filePath in fileDialog.FileNames )
                {
                    ListViewItem lstViewItem=new ListViewItem();
                    lstViewItem.Text = filePath.Substring(filePath.LastIndexOf(\\) + 1, filePath.Length - filePath.LastIndexOf(\\) - 1);
                    lstViewItem.SubItems.Add(filePath);
                    lstFiles.Items.Add(lstViewItem);
                   // lstFiles.Items.Add(filePath.Substring(filePath.LastIndexOf(‘\\‘) + 1, filePath.Length - filePath.LastIndexOf(‘\\‘) - 1), filePath);
                    //
                }
            }
           // axWindowsMediaPlayer1.Ctlcontrols.play();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.settings.autoStart = true;
        }

        private void LstFiles_DoubleClick(object sender, EventArgs e)
        {foreach (ListViewItem item in lstFiles.Items)
            {
                axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(item.SubItems[1].Text));
            }
            axWindowsMediaPlayer1.Ctlcontrols.play();
            axWindowsMediaPlayer1.currentPlaylist.clear();
            axWindowsMediaPlayer1.Ctlcontrols.stop();
            
            if (lstFiles.SelectedItems!=null)
            {
                ListViewItem item = lstFiles.SelectedItems[0];
                axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(item.SubItems[1].Text));
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }
        }

        private void 清空列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lstFiles.Items.Clear();
        }

        private void 單曲循環ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.URL = lstFiles.Items[0].SubItems[1].Text;
            axWindowsMediaPlayer1.settings.playCount = 100000;
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }

        private void 列表循環播放ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lstFiles.Items)
            {
                axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(item.SubItems[1].Text));
            }
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }

        private void 列表播放ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.currentPlaylist.clear();
            foreach (ListViewItem item in lstFiles.Items)
            {
                axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(item.SubItems[1].Text));
            }
            axWindowsMediaPlayer1.Ctlcontrols.play();

        }

        private void 單曲播放ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.currentPlaylist.clear();
            if (lstFiles.SelectedItems.Count==0)
            {
                axWindowsMediaPlayer1.URL = lstFiles.Items[0].SubItems[1].Text;
            }
            else
            {
                axWindowsMediaPlayer1.URL = lstFiles.SelectedItems[0].SubItems[1].Text;
            }

            axWindowsMediaPlayer1.Ctlcontrols.play();
        }

        
    }
}

技術分享圖片技術分享圖片

使用Window 自帶的控件 axWindowsMediaPlayer 制作播放器