C#winform 怎么播放amr音频文件
发布网友
发布时间:2022-04-26 00:27
我来回答
共1个回答
热心网友
时间:2023-10-24 15:51
使用MCI Command String多媒体设备程序接口播放mp3,avi等。
这种方法很好用。
下面的类传入音乐名(路径)可以完成音乐播放,循环播放和停止。
using System.Runtime.InteropServices;
class Music
{
[DllImport("winmm.dll")]
public static extern int mciSendString(string m_strCmd, string m_strReceive, int m_v1, int m_v2);
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);
private void playMusic(string name)
{
try
{
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(name, shortpath, shortpath.Capacity);
name = shortpath.ToString();
string buf = string.Empty;
mciSendString("play " + name, buf, buf.Length, 0); //播放
}
catch (Exception ex)
{
throw ex;
}
}
private void playMusicRepeatly(string name)
{
try
{
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(name, shortpath, shortpath.Capacity);
name = shortpath.ToString();
string buf = string.Empty;
mciSendString("play " + name+" repeat", buf, buf.Length, 0); //播放
}
catch (Exception ex)
{
throw ex;
}
}
public void stopMusic(string name)
{
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(name, shortpath, shortpath.Capacity);
name = shortpath.ToString();
string buf = string.Empty;
mciSendString(@"stop "+name, buf, buf.Length, 0);
mciSendString(@"close "+name, buf, buf.Length, 0); //停止播放
}
}
热心网友
时间:2023-10-24 15:51
使用MCI Command String多媒体设备程序接口播放mp3,avi等。
这种方法很好用。
下面的类传入音乐名(路径)可以完成音乐播放,循环播放和停止。
using System.Runtime.InteropServices;
class Music
{
[DllImport("winmm.dll")]
public static extern int mciSendString(string m_strCmd, string m_strReceive, int m_v1, int m_v2);
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);
private void playMusic(string name)
{
try
{
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(name, shortpath, shortpath.Capacity);
name = shortpath.ToString();
string buf = string.Empty;
mciSendString("play " + name, buf, buf.Length, 0); //播放
}
catch (Exception ex)
{
throw ex;
}
}
private void playMusicRepeatly(string name)
{
try
{
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(name, shortpath, shortpath.Capacity);
name = shortpath.ToString();
string buf = string.Empty;
mciSendString("play " + name+" repeat", buf, buf.Length, 0); //播放
}
catch (Exception ex)
{
throw ex;
}
}
public void stopMusic(string name)
{
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(name, shortpath, shortpath.Capacity);
name = shortpath.ToString();
string buf = string.Empty;
mciSendString(@"stop "+name, buf, buf.Length, 0);
mciSendString(@"close "+name, buf, buf.Length, 0); //停止播放
}
}