zoukankan      html  css  js  c++  java
  • C#调用Windows Api播放midi音频

    导入winmm.dll中的函数mciSendString

    [DllImport("winmm.dll")]
    static extern Int32 mciSendString(String command, StringBuilder buffer, Int32 bufferSize, IntPtr hwndCallback);

    需要注意的是,所有命令需要在同一个线程中,才可以对同一个midi实例进行控制。该函数通过字符串向声卡发送指令,常见的用于播放midi文件的指令有以下:


    1.打开文件

    mciSendString($"open {file} type {device} alias {alias}", null, 0, new IntPtr());
    

    {file}: 文件名,需要打开的midi文件路径;

    {device}: 设备名称,一般填写”sequencer”,不过笔者发现填写”mpegvideo”会显著提高加载midi的速度;

    {alias}: 别名,用于后续对该midi的控制。


    2.播放文件

    mciSendString($"play {alias} [repeat]", null, 0, new IntPtr());

    {alias}: 前文提到的别名;

    [repeat]: 可选指令,可以循环播放。


    3.获取文件时长

    mciSendString("set {alias} time format milliseconds", null, 0, new IntPtr());
    mciSendString("status {alias} length", result, 100, new IntPtr());

    首先将时间格式设置为毫秒,然后将时间读取到result变量中。


    4.停止播放

    mciSendString($"stop {alias}", null, 0, new IntPtr());


    5.关闭文件

    mciSendString($"close {alias}", null, 0, new IntPtr());
  • 相关阅读:
    批处理
    命名规则
    注释
    HTML DOM属性
    OLTP
    修改HTML元素
    HTML
    工具资源系列之给虚拟机装个centos
    工具资源系列之给虚拟机装个windows
    工具资源系列之给mac装个虚拟机
  • 原文地址:https://www.cnblogs.com/optimo/p/13716322.html
Copyright © 2011-2022 走看看