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

标签 c# mp3

<分区>

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

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

    }
}

最佳答案

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

关于c# - 使用 winmm 的 mp3 播放器,事件 mp3 结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9861048/

相关文章:

linux - 通过 wifi 流式传输音频 : feasible and how?

linux - 如何以编程方式将 RIFF header 添加到 MP3 文件?

c# - WPF 命令绑定(bind)到带有 MVVM 的 DataItemTemplate

audio - 保存音量更大的音频文件的最佳做法是什么?

C# Winforms。在矩形中心准确绘制文本

c# - Request.Content.IsMimeMultipartContent() 失败

javascript - 使用JavaScript播放/暂停MP3问题

android - 从扩展文件中读取 mp3

c# - 阅读服务总线主题订阅死信队列 (DLQ) C# 的最佳方式

c# - Reflection.Emit 中的 IEnumerable 属性创建