2012-03-25 10 views
2

私はいくつかのコードを見つけ、それを私の目的のために修正します。しかし、私はmp3が最後に到達するときにどのようにフックするか分からない。私は1つの曲が終了したら、次は再生しています。現在の終了時にランダムな曲を再生するのは簡単ですが、終了時を知る方法は?曲の長さを計算して、曲が終了したら関数を呼び出すためにタイマーを使うべきですか?win3を使用しているmp3プレーヤー、イベントmp3 end

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; 
using System.Runtime.InteropServices; 
using System.IO; 

namespace Playing_MP3_songs 
{ 
    public partial class Form1 : Form 
    { 
     public const int MM_MCINOTIFY = 0x3B9; 
     [DllImport("winmm.dll")] 
     private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback); 

     List<string> lista; 
     List<string> lista2; 
     string st; 
     public Form1() 
     { 
      InitializeComponent(); 


     } 

     private void set_list() 
     { 
      string[] costam = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.mp3", SearchOption.AllDirectories); 
      string[] costam2 = new string[Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.mp3", SearchOption.AllDirectories).Count()]; 
      int ilosc = costam.Count(); 
      int x = 0; 
      while (x <= ilosc - 1) 
      { 
       costam2[x] = Path.GetFileName(costam[x]); 
       x++; 

      } 
      lista = new List<string>(costam); 
      lista2 = new List<string>(costam2); 
      //listBox1.Sorted = true; 
      listBox2.DataSource = lista; 
      listBox1.DataSource = lista2; 
      if (listBox1.Items.Count == 0) 
      { 
       button2.Enabled = false; 
       button3.Enabled = false; 
      } 
      else 
      { 
       button2.Enabled = false; 
       button3.Enabled = true; 
      } 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) 
      { 
       set_list(); 
       button3.Enabled = true; //So you dont play no file. lol 
      } 


     } 


     protected override void OnLoad(EventArgs e) 
     { 

      base.OnLoad(e); 
      TextReader sr = new StreamReader("path.txt"); 
      st = sr.ReadLine(); 
      sr.Close(); 
      folderBrowserDialog1.SelectedPath = st; 
      set_list(); 
     } 
     protected override void OnClosing(CancelEventArgs e) 
     { 
      base.OnClosing(e); 
      TextWriter writer = new StreamWriter("path.txt"); 
      writer.WriteLine(folderBrowserDialog1.SelectedPath); 
      writer.Close(); 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      stop(); 
      listBox2.SetSelected(listBox1.SelectedIndex, true); 
      mciSendString("open \"" + listBox2.SelectedItem.ToString() + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero); 
      mciSendString("play MediaFile", null, 0, IntPtr.Zero); 

      //button3.Enabled = false; 
      button2.Enabled = true; 
     } 

     private void play() 
     { 
      stop(); 
      listBox2.SetSelected(listBox1.SelectedIndex, true); 
      mciSendString("open \"" + listBox2.SelectedItem.ToString() + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero); 
      mciSendString("play MediaFile", null, 0, IntPtr.Zero); 
      //button3.Enabled = false; 
      button2.Enabled = true; 
     } 

     private void stop() 
     { 
      mciSendString("close MediaFile", null, 0, IntPtr.Zero); 
      listBox2.ClearSelected(); 
      //button2.Enabled = false; 
      button3.Enabled = true; 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      mciSendString("close MediaFile", null, 0, IntPtr.Zero); 
      listBox2.ClearSelected(); 
      button2.Enabled = false; 
      button3.Enabled = true; 
     } 

     private void listBox1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if (e.KeyChar.ToString() == "B") 
      { 
       listBox1.ClearSelected(); 
       int ilosc = listBox1.Items.Count; 
       Random random = new Random(); 
       int number = random.Next(0, ilosc - 1); 
       listBox1.SetSelected(number, true); 
       stop(); 
       play(); 
       e.Handled = true; 
      } 

     } 

     private void listBox1_DoubleClick(object sender, EventArgs e) 
     { 
      play(); 
     } 

    } 
} 

答えて

0

追加notifyコマンドplay MediaFileに:

mciSendString("play MediaFile notify", null, 0, IntPtr.Zero); 

上書き形態でのWndProc関数は、メッセージを通知受信する際に完全な再生

protected override void WndProc(ref Message m) 
{ 
    if (m.Msg == Media.MM_MCINOTIFY) 
    { 
     do smthing.. 
    } 
    base.WndProc(ref m); 
} 
関連する問題