问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

C#中怎样实现音乐播放?

发布网友 发布时间:2022-05-29 03:13

我来回答

4个回答

热心网友 时间:2024-04-29 13:31

播放其它格式可以调用API来播放,最简单的就是直接调用windows media player控件
API调用播放类
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace MediaPlayer
{
class MediaPlayer
{
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
private static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);

[DllImport("winmm.dll", EntryPoint = "mciGetDeviceID", CharSet = CharSet.Auto)]
private static extern int mciGetDeviceID(string lpstrName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer
);

public enum PlayTypeName : byte
{
File = 1,
CDAudio = 2,
VCD = 3,
RealPlay = 4
}

public enum AudioSource : byte
{
H = 0,
L = 1,
R = 2
}

public enum Playstate : byte
{
Stopped = 1,
Playing = 2,
Pause = 3
}

public enum PlayStyle : byte
{
顺序 = 1,
随机 = 2,
循环 = 3
}

public PlayTypeName PlayType;
public int Temp; //零时变量 工程中将使用
public String SongName;//储存当前真正播放的歌曲名称
public string PreSongName;
public string NextSongName;
public int SongIndex; //储存当前播放的歌曲列表的位置
public int totalSong;
public PlayStyle PlayerStyle;//播放模式
public int Valume;//音量大小
public AudioSource audiosource;
public bool IsSlowly;//播放速度

/// <summary>
/// 获取DeviceID
/// </summary>
/// <returns>返回设备类型</returns>
public int GetDeviceID()
{
return mciGetDeviceID("NOWMUSIC");
}

/// <summary>
/// 根据文件名,确定设备
/// </summary>
/// <param name="ff">文件名</param>
/// <returns></returns>
public string GetDriverID(string ff)
{
string result = "";
ff = ff.ToUpper().Trim();
switch (ff.Substring(ff.Length - 3))
{
case "MID":
result = "Sequencer";
break;
case "RMI":
result = "Sequencer";
break;
case "IDI":
result = "Sequencer";
break;
case "WAV":
result = "Waveaudio";
break;
case "ASX":
result = "MPEGVideo2";
break;
case "IVF":
result = "MPEGVideo2";
break;
case "LSF":
result = "MPEGVideo2";
break;
case "LSX":
result = "MPEGVideo2";
break;
case "P2V":
result = "MPEGVideo2";
break;
case "WAX":
result = "MPEGVideo2";
break;
case "WVX":
result = "MPEGVideo2";
break;
case ".WM":
result = "MPEGVideo2";
break;
case "WMX":
result = "MPEGVideo2";
break;
case "WMP":
result = "MPEGVideo2";
break;
case ".RM":
result = "RealPlay";
break;
case "RAM":
result = "RealPlay";
break;
case ".RA":
result = "RealPlay";
break;
case "MVB":
result = "RealPlay";
break;
default:
result = "MPEGVideo";
break;
}
return result;
}

/// <summary>
/// 打开MCI设备,
/// </summary>
/// <param name="FileName">要打开的文件名</param>
/// <param name="Handle">mci设备的窗口句柄</param>
/// <returns>传值代表成功与否</returns>
public bool OpenMusic(string FileName, IntPtr Handle)
{
bool result = false;
string MciCommand;
int RefInt;

CloseMusic();

ShortPathName = "";
ShortPathName = ShortPathName.PadLeft(260, Convert.ToChar(" "));
RefInt = GetShortPathName(FileName, ShortPathName, ShortPathName.Length);
ShortPathName = GetCurrPath(ShortPathName);
string DriverID = GetDriverID(ShortPathName);
if (DriverID == "RealPlay")
return false;

MciCommand = string.Format("open {0} type {1} alias NOWMUSIC ", ShortPathName, DriverID);//"open " & RefShortName & " type " & DriverID & " alias NOWMUSIC"

if (DriverID == "AVIVideo" || DriverID == "MPEGVideo" || DriverID == "MPEGVideo2")
{
if (Handle != IntPtr.Zero)
{
MciCommand = MciCommand + string.Format(" parent {0} style child ", Handle);// " parent " & Hwnd & " style child"
}
else
{
MciCommand = MciCommand + " style overlapped ";
}
}

TemStr = "";
TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
RefInt = mciSendString(MciCommand, null, 0, 0);
mciSendString("set NOWMUSIC time format milliseconds", null, 0, 0);

if (RefInt == 0)
{
result = true;
SongName = Path.GetFileNameWithoutExtension(FileName);
}
return result;
}

/// <summary>
/// 播放音乐
/// </summary>
/// <returns></returns>
public bool PlayMusic()
{
bool result = false;
int RefInt = mciSendString("play NOWMUSIC", null, 0, 0);
if (RefInt == 0)
{
result = true;
SetValume(Valume);//当前音量大小
//检测播放速度
if (IsSlowly)
SetSpeed(800);
else
SetSpeed(1200);
//检测声道
switch ((int)audiosource)
{
case 0:
SetAudioSource(AudioSource.H);
break;

case 1:
SetAudioSource(AudioSource.L);
break;

case 2:
SetAudioSource(AudioSource.R);
break;
}
}
return result;
}

/// <summary>
/// 设置声音大小
/// </summary>
/// <param name="Valume">音量大小</param>
/// <returns></returns>
public bool SetValume(int Valume)
{
bool result = false;
string MciCommand = string.Format("setaudio NOWMUSIC volume to {0}", Valume);
int RefInt = mciSendString(MciCommand, null, 0, 0);
if (RefInt == 0)
{
result = true;
}
return result;
}

/// <summary>
/// 设置播放速度
/// </summary>
/// <param name="Speed"></param>
/// <returns></returns>
public bool SetSpeed(int Speed)
{
bool result = false;
string MciCommand = string.Format("set NOWMUSIC speed to {0}", Speed);
int RefInt = mciSendString(MciCommand, null, 0, 0);
if (RefInt == 0)
result = true;
return result;
}

/// <summary>
/// 设置声道
/// </summary>
/// <param name="audioSource"></param>
/// <returns></returns>
public bool SetAudioSource(AudioSource audioSource)
{
bool result = false;
string strSource = "";
switch ((int)audioSource)
{
case 1: strSource = "left"; break;
case 2: strSource = "right"; break;
case 0: strSource = "stereo"; break;
}
int RefInt = mciSendString("setaudioNOWMUSIC source to " + strSource, null, 0, 0);
if (RefInt == 0)
result = true;
return result;
}

/// <summary>
/// 设置静音 True为静音,FALSE为取消静音
/// </summary>
/// <param name="AudioOff"></param>
/// <returns></returns>
public bool SetAudioOnOff(bool AudioOff)
{
bool resut = false;
string OnOff;
if (AudioOff)
OnOff = "off";
else
OnOff = "on";
int RefInt = mciSendString("setaudio NOWMUSIC " + OnOff, null, 0, 0);
if (RefInt == 0)
resut = true;
return resut;
}

/// <summary>
/// 关闭媒体
/// </summary>
/// <returns></returns>
public bool CloseMusic()
{
int RefInt = mciSendString("close NOWMUSIC", null, 0, 0);
if (RefInt == 0)
return true;
return false;
}

/// <summary>
/// 暂停播放
/// </summary>
/// <returns></returns>
public bool PauseMusic()
{
int RefInt = mciSendString("pause NOWMUSIC", null, 0, 0);
if (RefInt == 0)
return true;
return false;
}

/// <summary>
/// 获得当前媒体的状态是不是在播放
/// </summary>
/// <returns></returns>
public Playstate IsPlaying()
{
Playstate isPlaying = Playstate.Stopped;
try
{
rLength = "";
rLength = rLength.PadLeft(128, Convert.ToChar(" "));
int RefInt = mciSendString("status NOWMUSIC mode", rLength, rLength.Length, 0);
rLength = rLength.Trim();
if (rLength.Substring(0, 7) == "playing" || rLength.Substring(0, 2) == "播放")
isPlaying = Playstate.Playing;
else if (rLength.Substring(0, 7) == "stopped" || rLength.Substring(0, 2) == "停止")
isPlaying = Playstate.Stopped;
else isPlaying = Playstate.Pause;
}
catch
{ }
return isPlaying;
}

/// <summary>
/// 获取当前播放进度 毫秒
/// </summary>
/// <returns></returns>
public int GetMusicPos()
{
rLength = "";
rLength = rLength.PadLeft(128, Convert.ToChar(" "));
mciSendString("status NOWMUSIC position", rLength, rLength.Length, 0);
rLength = rLength.Trim();
if (string.IsNullOrEmpty(rLength))
return 0;
else
return (int)(Convert.ToDouble(rLength));
}

/// <summary>
/// 获取当前播放进度 格式 00:00:00
/// </summary>
/// <returns></returns>
public string GetMusicPosString()
{
rLength = "";
rLength = rLength.PadLeft(128, Convert.ToChar(" "));
mciSendString("status NOWMUSIC position", rLength, rLength.Length, 0);
rLength = rLength.Trim();
if (string.IsNullOrEmpty(rLength))
return "00:00:00";
else
{
int s = Convert.ToInt32(rLength) / 1000;
int h = s / 3600;
int m = (s - (h * 3600)) / 60;
s = s - (h * 3600 + m * 60);
return string.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);
}
}

/// <summary>
/// 获取媒体的长度
/// </summary>
/// <returns></returns>
public int GetMusicLength()
{
rLength = "";
rLength = rLength.PadLeft(128, Convert.ToChar(" "));
mciSendString("status NOWMUSIC length", rLength, rLength.Length, 0);
rLength = rLength.Trim();
if (string.IsNullOrEmpty(rLength))
return 0;
else
return Convert.ToInt32(rLength);
}

/// <summary>
/// 获取媒体的长度 00:00:00
/// </summary>
/// <returns></returns>
public string GetMusicLengthString()
{
rLength = "";
rLength = rLength.PadLeft(128, Convert.ToChar(" "));
mciSendString("status NOWMUSIC length", rLength, rLength.Length, 0);
rLength = rLength.Trim();
if (string.IsNullOrEmpty(rLength))
return "00:00:00";
else
{
int s = Convert.ToInt32(rLength) / 1000;
int h = s / 3600;
int m = (s - (h * 3600)) / 60;
s = s - (h * 3600 + m * 60);
return string.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);
}
}

public bool SetMusicPos(int Position)
{
string MciCommand = string.Format("seek NOWMUSIC to {0}", Position);
int RefInt = mciSendString(MciCommand, null, 0, 0);
if (RefInt == 0)
return true;
else
return false;
}

private string GetCurrPath(string name)
{
if (name.Length < 1) return "";
name = name.Trim();
name = name.Substring(0, name.Length - 1);
return name;
}

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
private string ShortPathName = "";
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
private string rLength = "";
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
private string TemStr = "";
}
}

热心网友 时间:2024-04-29 13:31

添加using System.Media

soundPlayer player =new soundPlayer("歌曲名字.wav")
player.play();

热心网友 时间:2024-04-29 13:32

http://www.zu14.cn/2009/02/13/dotnet-system-media-soundplayer/

http://www.zu14.cn/2009/01/24/dotnet-mediaplayer/

热心网友 时间:2024-04-29 13:32

调用COM组件
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
个人账户养老金预测公式:现有5万元,缴费20年,能领多少钱? 临沂比较有名的男装品牌 呼伦贝尔市悦动网络科技有限公司怎么样? 呼伦贝尔中汇实业有限公司怎么样? 呼伦贝尔油玉不绝电子商务有限公司怎么样? 如何避免wps卡顿? 属鼠的男人找对象是属什么,属鼠的人和什么属相合 96年鼠的姻缘在哪年 属相相合年份运势提升 2024属鼠找对象属什么最佳 黑客攻击网站能报案吗 请问身份证在乡镇办里的定制是在省里还是县里定制 我想找一款内嵌字幕的软件 ...是重要政治工作做好政治工作必须有过硬的政治能力做纸张特别是_百度... &lt;?php $s=explode(&quot;\n&quot;,trim(`dir/b e:\\video`)); print_r($s... 怎么用avs 合并两个文件,并添加图片logo,求avs语句!!! 我在linux下安装好opencv2.1后,并且配置好了。但是在编译时找文件或目录... Frankie Paul的《If You》 歌词 ...份linux为服务端从摄像头获取图像,android为客户端 传递opencv... 儿子想要个英文名字中文名叫刘佳鑫 美国版的极速前进有什么大牌嘉宾参加过? 平井坚的《Paul》 歌词 北京航天中心医院附近的旅馆有哪些 paulkei啥牌 明年结婚,请问一下泰州海陵亚细亚酒店怎么样! 泰州海陵区宾馆隔离点哪个部门负责 某载重汽车的功率是90kw,当它以54km/h的速度匀速前进时,汽车受到的阻力... myEclipse下调用opencv_java249.dll到linux下部署opencv-249.jar下n... 北京王秀俊口腔诊所怎么样? 三本(二本2)就真的这么贵?!知道前进谢谢 从首都机场到玉泉路海凌宾馆怎么走 乡镇办理的身份证,定制是在县城还是省里 办理身份证都需要什么 没身份证用其他的证件可以定制吗? 计算机配置信息 急找定做身份证扫描件的专业机构,事成后重谢!!! 办理定制机要不要身份证原件 Microsoft VBScript 运行时错误 (0x800A0009)下标越界: &#39;[number... 如果让你设计身份证号码,你会怎么做? dr钻戒定制需要男女双方身份证吗 帮忙制作粉丝身份证!! 定制darry ring需要提供女方身份证吗 刻自己的身份证和自己名字的项链 什么地方有的买啊? 凭身份证定制的戒指 需要身份证号定制的礼物是什么 戴瑞的绑定身份证定制钻戒 每位男士凭身份证定制一生仅有一枚的戒指 求扇形的圆心角,怎么解 晚上用热水泡脚有好处吗? 低碳环保小诗歌六句有哪些? 扇形圆心角怎么求