使用winmm.dll的C#MP3播放器

我正在尝试在午餐时间将一个(非常)粗糙的MP3播放器捆绑在一起,到目前为止,我已经有了它来播放文件,我正在建立一个文件列表来启用随机歌曲,但我想我只是遇到了障碍。

有没有办法知道当前播放的MP3何时播出? 一个事件或一些这样的? 目前我不认为我能够有播放列表等,除非这是因为它在每次播放后都停止了。

我已经把下面的整个来源都搞定了,随意挑选它并给我任何反馈,欢呼。

using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace X { public partial class Form1 : Form { List Names = new List(); StreamReader reader = File.OpenText(@"C:\X.txt"); string line; OpenFileDialog ofd = new OpenFileDialog(); StringBuilder buffer = new StringBuilder(128); string CommandString; [DllImport("winmm.dll")] private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback); public Form1() { InitializeComponent(); while ((line = reader.ReadLine()) != null) { if (line.Trim() != "") { Names.Add(line.Trim()); } } } private void btnplay_Click(object sender, EventArgs e) { if (ofd.FileName == "") { if (ofd.ShowDialog() == DialogResult.OK) { ofd.Filter = "MP3 Files|*.mp3"; CommandString = "open " + "\"" + ofd.FileName + "\"" + " type MPEGVideo alias Mp3File"; mciSendString(CommandString, null, 0, 0); CommandString = "play Mp3File"; mciSendString(CommandString, null, 0, 0); } } else { CommandString = "play Mp3File"; mciSendString(CommandString, null, 0, 0); } } private void btnpause_Click(object sender, EventArgs e) { CommandString = "pause mp3file"; mciSendString(CommandString, null, 0, 0); } private void btnbrowse_Click(object sender, EventArgs e) { ofd.Filter = "Mp3 files |*.mp3"; if (ofd.ShowDialog() == DialogResult.OK) { txtpath.Text = ofd.FileName; CommandString = "close Mp3File"; mciSendString(CommandString, null, 0, 0); CommandString = "open " + "\"" + ofd.FileName + "\"" + " type MPEGVideo alias Mp3File"; mciSendString(CommandString, null, 0, 0); } } } } 

当您调用mcisendstring打开文件时,您可以从mcisendstring命令获取通知,只需发送表单的句柄并覆盖表单的wndproc方法,然后您可以从MCI示例代码获取通知,如下所示。

  private void btnplay_Click(object sender, EventArgs e) { if (ofd.FileName == "") { if (ofd.ShowDialog() == DialogResult.OK) { ofd.Filter = "MP3 Files|*.mp3"; CommandString = "open " + "\"" + ofd.FileName + "\"" + " type MPEGVideo alias Mp3File"; mciSendString(CommandString, null, 0, this.Handle.ToInt64()); CommandString = "play Mp3File"; mciSendString(CommandString, null, 0, this.Handle.ToInt64()); } } else { CommandString = "play Mp3File"; mciSendString(CommandString, null, 0, this.Handle.ToInt64()); } } // Declare the nofify constant public const int MM_MCINOTIFY = 953; // Override the WndProc function in the form protected override void WndProc(ref Message m) { if (m.Msg == MM_MCINOTIFY) { // The file is done playing, do whatever } base.WndProc(ref m); }