简体   繁体   English

简单的MP3播放器,通知无法正常工作

[英]Simple MP3 player, notify not working

C# beginner here. C#初学者在这里。 I want to make the player stop immediately after the song ended, so I tried the solution stated here . 我想让播放器在歌曲结束后立即停止播放,因此我尝试了此处所述的解决方案。 The problem is player is not stopped after the song finished and I need to manually hit stop button in order to choose another song. 问题是歌曲播放完后播放器没有停止,我需要手动点击停止按钮以选择另一首歌曲。 Did I do wrong somewhere? 我在某处做错了吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MusicP
{
    public partial class Form1 : Form
    {
        string command = "";

        [DllImport("winmm.dll")]
        private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback);

        public Form1()
        {
            InitializeComponent();
        }

        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)
            {
                Stop_Click(this, EventArgs.Empty);
            }
            base.WndProc(ref m);
        }

        private void Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "MP3 Files|*.mp3";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string file = ofd.FileName;
                label1.Text = ofd.SafeFileName;
                command = "open \"" + file + "\" type MPEGVideo alias MP3";
                mciSendString(command, null, 0, 0);
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void Play_Click(object sender, EventArgs e)
        {
            if (label1.Text == "")
            {
                Open_Click(this, EventArgs.Empty);
            }
            command = "play MP3 notify";
            mciSendString(command, null, 0, 0);
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            command = "stop MP3";
            mciSendString(command, null, 0, 0);

            command = "close MP3";
            mciSendString(command, null, 0, 0);
        }

        private void Pause_Click(object sender, EventArgs e)
        {
            command = "pause MP3";
            mciSendString(command, null, 0, 0);
        }

        private void Resume_Click(object sender, EventArgs e)
        {
            command = "resume MP3";
            mciSendString(command, null, 0, 0);
        }

    }
}

Thanks a lot! 非常感谢!

Append 附加

notify 通知

to

play MediaFile 播放MediaFile

command: 命令:

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

You have left out the handle to your window from the mciSendString: 您已从mciSendString中省略了窗口的句柄:

mciSendString(command, null, 0, 0);

should be something like this: 应该是这样的:

mciSendString(command, null, 0, (int)this.Handle);

now the Stop_Click gets called. 现在,调用Stop_Click。

You can test it by addinga somple command to it: 您可以通过向其添加一个somple命令来对其进行测试:

label1.Text = "Stopped";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM