我正在尝试制作一个程序,将显示像幻灯片一样的图像,每次显示一张图片时,声音将为当前图像播放,每个图像不同。如何使用带有声音的数组并播放来自该数组的声音?
string[] sounds = new string[] { "nero", "fai", "mpanio", "tv1", "volta",
"sleep1" };
private int currentAudioIndex = 0;
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
try
{
timer1.Interval = 5000; // Next time, wait 5 secs
button1.BackgroundImage = (Image)Properties.Resources.ResourceManager.GetObject(arr1[currentImageIndex]);
new SoundPlayer(Properties.Resources.nero).Play();// plays only one sound "nero"
currentImageIndex++;
}
finally
{
if (currentImageIndex < arr1.Length)
timer1.Start();
}
}
我假设你有wav文件资源名为"nero.wav", " fae .wav"等…
从那里,您可以将资源加载为Stream
,然后将流传递给SoundPlayer
构造函数:
Stream stream = Properties.Resources.ResourceManager.GetStream(arr1[currentImageIndex] + ".wav");
new SoundPlayer(stream).Play();