1

According to my last post in here one of friends suggested this code:

using System;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace TeaTimer
{
    /// <summary>
    /// MCIPlayer is based off code by Slain.
    /// Found here: http://www.sadeveloper.net/Articles_View.aspx?articleID=212
    /// </summary>
    public class MCIPlayer
    {
        private static readonly string sAlias="TeaTimerAudio";

        [DllImport("winmm.dll")]
        private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
        [DllImport("Winmm.dll")]
        private static extern long PlaySound(byte[] data, IntPtr hMod, UInt32 dwFlags);

        public static void Play(string sFile)
        {
            _Open(sFile);
            _Play();
        }
        public static void Stop() 
        {
            _Close();
        }

        private static void _Open(string sFileName)
        {
            if(_Status()!="")
                _Close();

            string sCommand = "open \"" + sFileName + "\" alias "+sAlias;
            mciSendString(sCommand, null, 0, IntPtr.Zero);
        }

        private static void _Close()
        {
            string sCommand = "close "+sAlias;
            mciSendString(sCommand, null, 0, IntPtr.Zero);
        }

        private static void _Play()
        {
            string sCommand = "play "+sAlias;
            mciSendString(sCommand, null, 0, IntPtr.Zero);
        }

        private static string _Status()
        {
            StringBuilder sBuffer = new StringBuilder(128);
            mciSendString("status "+sAlias+" mode", sBuffer, sBuffer.Capacity, IntPtr.Zero);
            return sBuffer.ToString();
        }
    }
}

It works fine, but now the problem is, I can not repeat my MIDI file. I saw some code but I don't know why it doesn't work. I tried:

Scommand = "play "+sAlias+" repeat "; 
mciSendString(Scommand, null, 0, IntPtr.Zero);
Community
  • 1
  • 1
Amir
  • 1,919
  • 8
  • 53
  • 105

1 Answers1

2

Why do you think "repeat" is a supported command? According to MSDN I cannot see that it is supported.

The solution that I see is to use notify flag.

Here is sample working for me:

public class MCIPlayer
{
    private class Form2: Form
    {
        public Form2()
        {
            if (!IsHandleCreated) CreateHandle();
        }

        private const int MM_MCINOTIFY = 953;

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == MM_MCINOTIFY)
                MCIPlayer.Play(file);
        }

        public string file;
    }

    private static Form2 f = new Form2();

    private static readonly string sAlias = "TeaTimerAudio";

    [DllImport("winmm.dll", SetLastError = true)]
    private static extern int mciSendString(string strCommand, 
                    StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
    [DllImport("Winmm.dll")]
    private static extern long PlaySound(byte[] data, IntPtr hMod, UInt32 dwFlags);

    public static void Play(string sFile)
    {
        _Open(sFile);
        _Play();
    }

    public static void PlayTwice(string sFile)
    {
        _Open(sFile);
        f.file = sFile;
        _PlayTwice();
    }

    public static void Stop()
    {
        _Close();
    }

    private static void _Open(string sFileName)
    {
        if (_Status() != "")
            _Close();

        string sCommand = "open \"" + sFileName + "\" alias " + sAlias;
        mciSendString(sCommand, null, 0, IntPtr.Zero);
    }

    private static void _Close()
    {
        string sCommand = "close " + sAlias;
        mciSendString(sCommand, null, 0, IntPtr.Zero);
    }

    private static void _Play()
    {
        string sCommand = "play " + sAlias;
        mciSendString(sCommand, null, 0, IntPtr.Zero);
    }

    private static void _PlayTwice()
    {
        string sCommand = "play " + sAlias + " notify";
        mciSendString(sCommand, null, 0, f.Handle);
    }

    private static string _Status()
    {
        StringBuilder sBuffer = new StringBuilder(128);
        mciSendString("status " + sAlias + " mode", sBuffer, 
                              sBuffer.Capacity, IntPtr.Zero);
        return sBuffer.ToString();
    }
}
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
ironic
  • 8,368
  • 7
  • 35
  • 44
  • What should I do for repeating more than 2 times?plz help me :(( – Amir Nov 13 '10 at 21:58
  • I have one more problem,I use it in my frame,I mean I have an other frame, but after I close frame again my sound staring after a while – Amir Nov 13 '10 at 22:01
  • @Amir You can put a variable somewhere to store the times the song repeats and check that variable within WndProc() to determine if need to call MCIPlayer.Play(file) again – Silent Sojourner Jun 20 '17 at 16:20