您好 请你给我一些允许我读取存储在数据库中的file.wav(“alarme.wav”)的代码,我试过以下代码:
private void button1_Click (object sender, EventArgs e)
{
OdbcConnection cn = new OdbcConnection ("DSN = cp1");
cn.Open ();
OdbcCommand cmd1 = new OdbcCommand ("select path from WHERE Code Alarm alarm = 32 ", cn);
cmd1.Connection = cn;
fileName = cmd1.ToString ();
WaveOut wave = new ();
wave.DeviceNumber = 0;
playSound (0);
但他给了我以下错误: “无法找到文件'C:\ Users \ wafa \ Documents \ Visual Studio 2010 \ Projects \ ALARMED \ ALARMED \ bin \ Debug \ System.Data.Odbc.OdbcCommand'。” 在线: “waveReader = new NAudio.Wave.WaveFileReader(fileName);”在我的“playSound”功能中:
public void PlaySound (int deviceNumber)
{
disposeWave ();// stop previous sounds Before Starting
waveReader = new NAudio.Wave.WaveFileReader (fileName);
var waveOut NAudio.Wave.WaveOut = new ();
waveOut.DeviceNumber = deviceNumber;
var output = waveOut;
output.Init (waveReader);
output.Play ();
}
提前谢谢你。 美好的一天:)
答案 0 :(得分:0)
我相信你通过
完成了这项工作System.Media.SoundPlayer Player = new System.Media.SoundPlayer(fileName)
Player.Play();
答案 1 :(得分:0)
虽然我不能给你一个“Ready To Go”解决方案,但我建议你在错误行上设置一个断点,并检查“fileName”的值。如果它是正确的路径,那么您使用该路径的方式是错误的。如果路径错误,则需要在尝试播放波形之前修复路径提取方式。
编辑1& 2合并:
public partial class MyForm : Form
{
#region Fields & Properties
Dictionary<int, string> _soundFilepaths;
#endregion
#region Constructor & Destructor
public MyForm()
{
InitializeComponent();
this.EnableSounds();
}
#endregion
#region Events
private void playSound1_Click(object sender, EventArgs e)
{
this.PlaySound(0, 1);
}
private void playSound2_Click(object sender, EventArgs e)
{
this.PlaySound(0, 2);
}
#endregion
#region Methods
private void EnableSounds()
{
Dictionary<int, string> soundPaths = new Dictionary<int, string>();
string commandString = "select code_alarme, chemin from alarme";
using (OdbcConnection conn = new OdbcConnection("DSN=cp1"))
{
using (OdbcCommand cmd = new OdbcCommand(commandString, conn))
{
cmd.CommandTimeout = 60;
conn.Open();
using (OdbcDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
soundPaths.Add(int.Parse(dr[0].ToString()), dr[1].ToString());
}
}
}
}
this._soundFilepaths = soundPaths;
}
private void PlaySound(int deviceID, int soundID)
{
// I don't have the NAudio library so I can't check if it works or no though.
disposeWave();
NAudio.Wave.WaveFileReader waveReader = new NAudio.Wave.WaveFileReader(this._soundFilepaths[soundID]);
var waveOut = new NAudio.Wave.WaveOut();
waveOut.DeviceNumber = deviceNumber;
var output = waveOut;
output.Init(waveReader);
output.Play();
}
#endregion
}
答案 2 :(得分:0)
cmd1.ToString ();
这看起来很错误。 IDBCommand公开了ExecuteRead()
或ExecuteScalar()
等方法。
ToString()
的默认实现是输出类型名称。除非你的工作目录中有一个名为“ System.Data.OdbcCommand ”的文件,否则你不会听到太多,我恐怕......