Simple MP3 player, notify not working

℡╲_俬逩灬. 提交于 2019-12-25 02:51:25

问题


C# beginner here. 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!


回答1:


You have left out the handle to your window from the mciSendString:

mciSendString(command, null, 0, 0);

should be something like this:

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

now the Stop_Click gets called.

You can test it by addinga somple command to it:

label1.Text = "Stopped";



回答2:


Append

notify

to

play MediaFile

command:

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


来源:https://stackoverflow.com/questions/24103476/simple-mp3-player-notify-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!