C# 使用winmm的mp3播放器,事件mp3结束

C# 使用winmm的mp3播放器,事件mp3结束,c#,mp3,C#,Mp3,我找到了一些代码,并出于自己的目的对其进行了修改。但我不知道当mp3播放到最后时该如何上钩。我想做的是,如果一首歌结束了,下一首是演奏。在当前结束时播放随机歌曲很简单,但如何知道何时结束?我是否应该尝试计算歌曲的长度,并在歌曲结束时使用计时器调用函数 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using

我找到了一些代码,并出于自己的目的对其进行了修改。但我不知道当mp3播放到最后时该如何上钩。我想做的是,如果一首歌结束了,下一首是演奏。在当前结束时播放随机歌曲很简单,但如何知道何时结束?我是否应该尝试计算歌曲的长度,并在歌曲结束时使用计时器调用函数

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

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用System.IO;
播放MP3歌曲的名称空间
{
公共部分类Form1:Form
{
公共常数int MM_MCINOTIFY=0x3B9;
[DllImport(“winmm.dll”)]
私有静态外部长mciSendString(字符串strCommand、StringBuilder strReturn、int-iReturnLength、IntPtr-hwndCallback);
清单a;
清单2;
字符串st;
公共表格1()
{
初始化组件();
}
私有无效集_列表()
{
字符串[]costam=Directory.GetFiles(folderBrowserDialog1.SelectedPath,“*.mp3”,SearchOption.AllDirectories);
string[]costam2=新字符串[Directory.GetFiles(folderBrowserDialog1.SelectedPath,“*.mp3”,SearchOption.AllDirectories).Count()];
int ilosc=costam.Count();
int x=0;

而(xAppend
notify
to
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);
}

通知
附加到
播放媒体文件
命令:

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