gpt4 book ai didi

c# - 如何在C#中重复MIDI文件?

转载 作者:行者123 更新时间:2023-12-02 23:00:17 24 4
gpt4 key购买 nike

根据我在here中的最后一篇文章,一个 friend 建议使用此代码:

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

它工作正常,但现在的问题是,我无法重复我的MIDI文件。
我看到了一些代码,但我不知道为什么它不起作用。
我试过了:
Scommand = "play "+sAlias+" repeat "; 
mciSendString(Scommand, null, 0, IntPtr.Zero);

最佳答案

为什么您认为“repeat”是受支持的命令?
根据MSDN,我看不到它受支持。

我看到的解决方案是使用notify flag

这是为我工作的示例:

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

关于c# - 如何在C#中重复MIDI文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3905732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com