0

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!

2 Answers 2

0

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
  • This worked! Could you tell me specifically what the handle does?
    – Zerocchi
    Jun 8, 2014 at 8:27
  • this is your form and the handle is the (old style) reference to it. If you include it as the last parameter the MCI object will know just where to send the notification. Setting it to 0 makes the notification call go nowhere instead of to your form.
    – TaW
    Jun 8, 2014 at 8:38
0

Append

notify

to

play MediaFile

command:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.