2010-03-02 6 views
1

Je suis en train d'enchaîner un lecteur MP3 (très) rugueux pendant mon heure de déjeuner, et jusqu'ici je l'ai pour jouer les fichiers, et je travaille d'une manière de construire une liste de noms de fichiers pour activer les chansons aléatoires, mais je pense que je viens de frapper un hic.C# Lecteur MP3 en utilisant winmm.dll

Y at-il un moyen de savoir quand le MP3 en cours de lecture est terminé? Un événement ou un tel? En l'état, je ne pense pas que je serais en mesure d'avoir des listes de lecture, etc., à moins que cela ne soit possible parce qu'il s'est arrêté après chaque lecture.

J'ai attatched l'ensemble de la source ci-dessous, n'hésitez pas à le démonter et me donner tous les commentaires que vous pourriez avoir, acclamations.

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); 
     } 
    } 
} 
} 

Répondre

2

Vous pouvez obtenir une notification de commande mciSendString lorsque vous appelant mciSendString pour ouvrir le fichier il suffit d'envoyer la poignée de votre formulaire et remplacer la méthode WndProc de votre formulaire alors u peut obtenir le notifier à partir du code échantillon MCI comme suit. `

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); 
} 
Questions connexes