2010-03-02 210 views
1

我正在嘗試在午餐時間將(非常)粗糙的MP3播放器捆綁在一起,到目前爲止我已經準備好播放這些文件了,而且我正在開發一種方法建立一個文件名列表來啓用隨機歌曲,但我認爲我剛剛遇到了一個問題。使用winmm.dll的C#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<string> Names = new List<string>(); 
    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); 
     } 
    } 
} 
} 

回答

2

您可以從mcisendstring命令,當你調用mcisendstring用於打開該文件只發送表單的手柄並覆蓋窗體的WndProc方法則U可以從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); 
}