【问题标题】:Simple MP3 player, notify not working简单的 MP3 播放器,通知不工作
【发布时间】:2014-06-08 06:07:32
【问题描述】:

这里是 C# 初学者。我想让播放器在歌曲结束后立即停止,所以我尝试了解决方案stated here。问题是歌曲播放完后播放器没有停止,我需要手动点击停止按钮才能选择另一首歌曲。我是不是哪里做错了?

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

    }
}

非常感谢!

【问题讨论】:

    标签: c# winmm


    【解决方案1】:

    您已从 mciSendString 中省略了窗口句柄:

    mciSendString(command, null, 0, 0);
    

    应该是这样的:

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

    现在 Stop_Click 被调用。

    你可以通过添加一个简单的命令来测试它:

    label1.Text = "Stopped";
    

    【讨论】:

    • 这成功了!能具体说说手柄的作用吗?
    • this 是您的表单,句柄是对它的(旧样式)引用。如果您将它作为最后一个参数包含,MCI 对象将知道将通知发送到何处。将其设置为 0 会使通知调用无处可去,而不是发送到您的表单。
    【解决方案2】:

    追加

    通知

    播放媒体文件

    命令:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-23
      • 2014-02-25
      • 2013-05-18
      • 2014-11-11
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      相关资源
      最近更新 更多