我要做的是播放列表框中所选项目的声音文件。例如:项目1播放“1.mp3”,项目2播放“2.mp3” 但事实是,它需要停止其他音频然后播放。它在播放wavs时执行此操作:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == 0)
{
SoundPlayer player = new SoundPlayer();
player.SoundLocation = "S1.mp3";
player.Play();
}
if (listBox1.SelectedIndex == 1)
{
SoundPlayer player = new SoundPlayer();
player.SoundLocation = "S2.mp3";
player.Play();
}
if (listBox1.SelectedIndex == 2)
{
SoundPlayer player = new SoundPlayer();
player.SoundLocation = "S3.mp3";
player.Play();
}
}
但是wavs太大了所以我需要一个替代方案。
我已经全神贯注地寻找解决方案,但没有任何作用:( NAudio播放声音在彼此之上,我无法找到如何使用NVorbis和oggsharp等。
使用任何格式的音频都很好,我不在乎。我就是不能用wav。
答案 0 :(得分:2)
有一个SoundPlayer
实例,在播放下一个音频之前,请停止当前音频。
public class Sounds
{
SoundPlayer player = new SoundPlayer();
public void Play(string file)
{
player.Stop();
player.SoundLocation = file;
player.Play();
}
}
对于MP3,您可以使用WMPLib.WindowsMediaPlayer
要以编程方式创建Windows Media Player控件,必须先添加对wmp.dll的引用,该引用位于\ Windows \ system32文件夹中。
public class Sounds
{
WMPLib.WindowsMediaPlayer player = new WMPLib.WindowsMediaPlayer();
private void PlayFile(String url)
{
player.URL = url;
player.controls.play();
}
}
用法:
public class MyThing
{
Sounds sounds = new Sounds();
string SelectedFile;
public void OnPlayClick()
{
sounds.PlayFile(SelectedFile);
}
}