本文介绍了听不到声音,当演奏在C#.NET中的MIDI文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code播放MIDI文件为我的比赛,但我不能听到我的发言任何声音。你能帮我吗?这是一种紧急情况,请...
我已开启扬声器;)

 函数[DllImport(WINMM.DLL,入口点=mciSendStringA)]
私人静态外部长mciSendString(字符串lpstrCommand,串lpstrReturnString,长uReturnLength,长hwndCallback);
公共静态长PlayMidiFile(字符串MidiFile)
{
 长LRET = -1; 如果(File.Exists(MidiFile))
 {
  LRET = mciSendString(停止MIDI,,0,0);
  LRET = mciSendString(关闭MIDI,,0,0);
  LRET = mciSendString((开放式序!
   +(MidiFile +的别名的MIDI)),,0,0);
  LRET = mciSendString(播放midi,,0,0);
  返回LRET;
 } 其他
 {
  //错误信息
  返回LRET;
 }
}


解决方案

我真的不知道你实现WINMM.DLL,但我有一个测试和工作code吧。

我得到的源$ C ​​$ C从这个开源项目:。

在code的实现是pretty直线前进如下。希望它帮助。

 使用系统;
使用System.Text;
使用System.Runtime.InteropServices;
使用System.IO;命名空间TeaTimer
{
    ///<总结>
    /// MCIPlayer是被杀害的基于关闭code。
    ///在这里找到:http://www.sadeveloper.net/Articles_View.aspx?articleID=212
    ///< /总结>
    公共类MCIPlayer
    {
        私人静态只读字符串sAlias​​ =TeaTimerAudio;        函数[DllImport(WINMM.DLL)]
        私人静态外部长mciSendString(字符串strCommand,StringBuilder的strReturn,诠释iReturnLength,IntPtr的hwndCallback);
        函数[DllImport(WINMM.DLL)]
        私人静态外部长PlaySound(字节[]数据,IntPtr的HMOD,UInt32的dwFlags中);        公共静态无效播放(字符串sFile)
        {
            _open(sFile);
            _玩();
        }
        公共静态无效停止()
        {
            _关();
        }        私有静态无效_open(字符串sFileName)
        {
            如果(_Status()!=)
                _关();            字符串sCommand =打开\\+ sFileName +\\别名+ sAlias​​;
            mciSendString(sCommand,NULL,0,IntPtr.Zero);
        }        私有静态无效_close()
        {
            字符串sCommand =接近+ sAlias​​;
            mciSendString(sCommand,NULL,0,IntPtr.Zero);
        }        私有静态无效_play()
        {
            字符串sCommand =玩+ sAlias​​;
            mciSendString(sCommand,NULL,0,IntPtr.Zero);
        }        私人静态字符串_Status()
        {
            StringBuilder的sBuffer =新的StringBuilder(128);
            mciSendString(身份+ sAlias​​ +模式,sBuffer,sBuffer.Capacity,IntPtr.Zero);
            返回sBuffer.ToString();
        }
    }
}

编辑:这是你如何播放和停止音乐文件:

 公共静态无效playSound(字符串sFile)
{
    //WavPlay.WavPlayer.Play(sFile);
    MCIPlayer.Play(sFile);
}
公共静态无效stopSound()
{
    //WavPlay.WavPlayer.StopPlay();
    MCIPlayer.Stop();
}

I use this code to play a MIDI file for my game, but I can not hear any sound from my speakers. Would you help me? It's kind of an emergency, please...My speakers are turned on ;)

[DllImport("winmm.dll", EntryPoint="mciSendStringA")]
private static extern long mciSendString(string lpstrCommand, string lpstrReturnString, long uReturnLength, long hwndCallback);
public static long PlayMidiFile(string MidiFile)
{
 long lRet = -1;

 if (File.Exists(MidiFile))
 {
  lRet = mciSendString("stop midi", "", 0, 0);
  lRet = mciSendString("close midi", "", 0, 0);
  lRet = mciSendString(("open sequencer!"
   + (MidiFile + " alias midi")), "", 0, 0);
  lRet = mciSendString("play midi", "", 0, 0);
  return lRet;
 }

 else
 {
  //Error Message
  return lRet;
 }
}
解决方案

I am not really sure about your implementation of winmm.dll but I have a tested and working code for it.

I got the source code from this open source project: Tea Timer.

The implementation of the code is pretty straight forward as below. Hope it helps.

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

EDIT: This is how you play and stop a music file:

public static void playSound(string sFile)
{
    //WavPlay.WavPlayer.Play(sFile);
    MCIPlayer.Play(sFile);
}
public static void stopSound()
{
    //WavPlay.WavPlayer.StopPlay();
    MCIPlayer.Stop();
}

这篇关于听不到声音,当演奏在C#.NET中的MIDI文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:57