C# 后台播放MP3、wav文件
播放MP3
public class Mp3Player
{
/// <summary>
/// 文件地址
/// </summary>
public string FilePath;
{
/// <summary>
/// 文件地址
/// </summary>
public string FilePath;
/// <summary>
/// 播放
/// </summary>
public void Play()
{
mciSendString("close all", "", 0, 0);
mciSendString("open " + FilePath + " alias media", "", 0, 0);
mciSendString("play media", "", 0, 0);
}
/// 播放
/// </summary>
public void Play()
{
mciSendString("close all", "", 0, 0);
mciSendString("open " + FilePath + " alias media", "", 0, 0);
mciSendString("play media", "", 0, 0);
}
/// <summary>
/// 暂停
/// </summary>
public void Pause()
{
mciSendString("pause media", "", 0, 0);
}
/// 暂停
/// </summary>
public void Pause()
{
mciSendString("pause media", "", 0, 0);
}
/// <summary>
/// 停止
/// </summary>
public void Stop()
{
mciSendString("close media", "", 0, 0);
}
/// 停止
/// </summary>
public void Stop()
{
mciSendString("close media", "", 0, 0);
}
/// <summary>
/// API函数
/// </summary>
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
private static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
/// API函数
/// </summary>
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
private static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
播放WAV文件
使用C#自带的SoundPlayer
1 using System.Media; 2 3 4 SoundPlayer sound = new SoundPlayer("声音.wav"); 5 sound.Play();

浙公网安备 33010602011771号