积极答复者
关于调用winmm.dll播放wav的问题

问题
-
使用了toolbar,可以选择播放第几首歌曲。
请问该怎么使用?
我手头倒是有个例子,可是使用了之后提示 PInvoke堆栈不对称,然后我把
private static extern long mciSendString(string lpstrCommand,string lpstrReturnString,long length,long hwndcallback);
里的long都改成int,程序是可以运行了,但是点按钮之后不播放歌曲。- 已移动 Sheng Jiang 蒋晟Moderator 2009年5月10日 16:18 CLR问题 ([Loc]From:Visual C#)
答案
全部回复
-
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace 播放音乐
{
public enum PlaySoundFlags : int
{
SND_SYNC = 0x0, // play synchronously (default)
SND_ASYNC = 0x1, // play asynchronously
SND_NODEFAULT = 0x2, // silence (!default) if sound not found
SND_MEMORY = 0x4, // pszSound points to a memory file
SND_LOOP = 0x8, // loop the sound until next sndPlaySound
SND_NOSTOP = 0x10, // don't stop any currently playing sound
SND_NOWAIT = 0x2000, // don't wait if the driver is busy
SND_ALIAS = 0x10000, // name is a registry alias
SND_ALIAS_ID = 0x110000,// alias is a predefined ID
SND_FILENAME = 0x20000, // name is file name
SND_RESOURCE = 0x40004, // name is resource name or atom
};
class PlayMusic
{
public PlayMusic()
{
//
// TODO: 在此处添加构造函数逻辑
//
}// [DllImport("winmm.dll")]
// private static extern long PlaySound(String fileName,long a,long b);public static void Play(string p_FileName)
{
try
{
mciSendString(@"close " + p_FileName, " ", 0, 0);
mciSendString(@"open " + p_FileName, " ", 0, 0);
mciSendString(@"play " + p_FileName, " ", 0, 0);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}}
[DllImport("winmm.dll")]
private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int length, int hwndcallback);
/// <summary>
/// 停止当前音乐播放
/// </summary>
/// <param name="fileName">音乐文件名称</param>
public static void StopMusic(string fileName)
{
try
{
mciSendString(@"close " + fileName, " ", 0, 0);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}