创建迷你媒体播放器






3.88/5 (6投票s)
使用 Windows Media Player COM 创建迷你媒体播放器

引言
本文将向您介绍 Windows Media Player 的主要类,如何使用 C# 将歌曲加载到您的 Windows 窗体中,自定义它,制作歌曲列表等等...本教程使用 Windows Media Player COM 引用。
背景
在阅读本文之前,您可能想稍微了解一下 Windows Media Player 类或使用 COM 进行引用,但这不是必要的。
使用代码
一开始,我编写的媒体系统使用 Form1 进行初始化,使用
private void Form1_Load(object sender, EventArgs e) { this.player = new Media_Player_Registry.MediaPlayer(); // Set media player vars this.player.Volume = this.Volume.Value; }
我调用了 Media_Player_Registry.MediaPlayer
,这是我的媒体播放器类,用于管理我对 Windows Media Player 的操作。
在 MediaPlayer
类中,您可以看到一些有用的变量
player
- 这是与 Windows Media Player COM 建立“连接”的变量。
在构造函数中初始化它使我能够控制 Windows Media Player 使用的一些方法(例如加载歌曲、评分等...)-
Active_Song - active song 是一个 Song 类型的变量(我也创建了 song 类,稍后会讨论),这是活动歌曲的指针(我正在收听/停止但已选择的歌曲)
-
volume
- 这是一个整数,表示歌曲的音量(就像您可以在媒体播放器中提高和降低它一样) -
media_loaded_songs
- 整数 - 加载的歌曲数量
song
类
在 song
类中,您有一个“虚拟”类,仅存储歌曲的值
-
song_name
- 歌曲名称 -
active_song
- 歌曲是否在媒体播放器上处于活动状态 -
mute
- 歌曲是否静音
方法
我不会在这里描述我制作的所有方法,但会描述其中的一些方法
Media_Player_Registry.Song.BaseName()
方法
这是一种获取文件基本名称的方法(我真的不知道 C# 中是否有...所以我制作了一个),该方法获取类似于 (C:\uses\yahav\Music\song name.mp3) 的 Song_Name
属性,并从中释放 “song name”。
public string BaseName() { string basename = ""; string tmp_basename = ""; for (int i = 0; i < this.song_name.Length; i++) { // Override tmp tmp_basename = this.song_name.Substring(i); //System.Windows.Forms.MessageBox.Show("letter: " + this.song_name[i] + " indexof: " + tmp_basename.IndexOf(@"\").ToString()); if (tmp_basename.IndexOf(@"\") == -1) { basename += this.song_name[i]; } } return basename; }
在这里,我得到了名称,运行并随时进行子字符串操作,如果我再也看不到任何斜杠,就开始保存字符串。
Media_Player_Registry.MediaPlayer.Volume
属性
public int Volume { get { return this.volume; } set { if (value < 0) { this.volume = 0; } else if (value > 10) { this.volume = 10; } else { this.volume = value; } // Set it on Windows Media Player // Fix up switch ( this.volume ) { case 0: this.volume = 10; break; case 1: this.volume = 9; break; case 2: this.volume = 8; break; case 3: this.volume = 7; break; case 4: this.volume = 6; break; case 6: this.volume = 4; break; case 7: this.volume = 3; break; case 8: this.volume = 2; break; case 9: this.volume = 1; break; case 10: this.volume = 0; break; } this.volume = this.volume * -100; this.player.Volume = this.volume; //System.Windows.Forms.MessageBox.Show(this.player.Volume.ToString()); } }
这有点棘手:我意识到 Windows Media Player 类获取的参数是从 -100 到 -1000,所以在我的滚动条中我放置了 1 - 10,然后我在这里使用了 ( volume * -100 ) 来获得它。
我为什么要进行这种切换?因为我发现它是相反的 - 如果我放置 1,则意味着 9...所以我想不出更好的方法...
我想讨论的最后一个方法是 Media_Player_Registry.Form1.LoadSong
方法
public void LoadSong()
{
// Some vars
LoadDialog.Reset();
LoadDialog.CheckFileExists = true;
LoadDialog.CheckPathExists = true;
LoadDialog.AddExtension = true;
LoadDialog.DefaultExt = ".mp3";
LoadDialog.Title = "Select mp3 file";
LoadDialog.Filter = "MP3 files (*.mp3)|*.*";
LoadDialog.ShowDialog();
if (this.LoadDialog.FileName == "" || this.LoadDialog.FileName == null)
{
return;
}
this.LoadSong(LoadDialog.FileName);
// Set on list
TreeNode t = new TreeNode();
t.Name = LoadDialog.FileName;
t.Checked = true;
t.NodeFont = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
t.Text = this.player.Active_BaseSongName();
this.SongList.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
t});
}
private void LoadSong(string song_filename)
{
this.player.Load_Song(song_filename);
// Set name
this.SongNameVar.Text = this.player.Active_BaseSongName();
// Botton name is stop
this.SongState.Text = "Stop";
}
这些是两个方法,一个不获取任何参数,另一个获取字符串文件名,不同之处在于:不获取任何参数的方法会打开一个 .NET 打开文件对话框,以从用户那里获取所需的歌曲,获取后,它会使用 LoadDialog.FileName (LoadDialog = 对话框的名称) 调用具有文件名的 LoadSong。不获取任何参数的 LoadSong 也会将歌曲插入到树视图中
关注点
当我制作音量选择时,猜测它们的索引真的很烦人,所以我使用了代码
System.Windows.Forms.MessageBox.Show( this.player.Volume );
来获取音量。