如何从 ListBox 播放 wav 文件?
How to play a wav file from a ListBox?
下午好。我正在 wpf 上制作一个小程序。有一个列表框,其中加载了文件夹中的 wav 文件。有一个 RadioButton(有几个),当您单击它时,某些音频文件会添加到列表中。但是我不知道如何在点击ListBox中的wav元素时播放选定的音轨
SoundPlayer player;
string[] playList;
private void сmClassic(object sender, RoutedEventArgs e)
{
lbList.Items.Clear();
DirectoryInfo dir = new DirectoryInfo(@"C:\Users95355\Desktop\Студент\C#_FourthProject\FourthProject\bin\Debug\music\classic");
FileInfo[] files = dir.GetFiles("*.wav");
foreach (FileInfo fi in files)
{
lbList.Items.Add(fi.ToString());
}
string d = playList[lbList.SelectedIndex];
// player.Open(d); Yes, there is a mistake, but I was looking for an alternative
player.Play();
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
代码来自答案in original source。
private void сmClassic(object sender, RoutedEventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(@"C:\Users95355\Desktop\Студент\C#_FourthProject\FourthProject\bin\Debug\music\classic");
FileInfo[] files = dir.GetFiles("*.wav");
lbList.ItemsSource = files;
}
private readonly SoundPlayer player = new SoundPlayer();
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
player.Stop();
if(lbList.SelectedItem is FileInfo file)
{
player.SoundLocation=file.FullName;
player.PlaySync();
}
else
{
player.SoundLocation=null;
}
}
下午好。我正在 wpf 上制作一个小程序。有一个列表框,其中加载了文件夹中的 wav 文件。有一个 RadioButton(有几个),当您单击它时,某些音频文件会添加到列表中。但是我不知道如何在点击ListBox中的wav元素时播放选定的音轨
SoundPlayer player;
string[] playList;
private void сmClassic(object sender, RoutedEventArgs e)
{
lbList.Items.Clear();
DirectoryInfo dir = new DirectoryInfo(@"C:\Users95355\Desktop\Студент\C#_FourthProject\FourthProject\bin\Debug\music\classic");
FileInfo[] files = dir.GetFiles("*.wav");
foreach (FileInfo fi in files)
{
lbList.Items.Add(fi.ToString());
}
string d = playList[lbList.SelectedIndex];
// player.Open(d); Yes, there is a mistake, but I was looking for an alternative
player.Play();
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
代码来自答案in original source。
private void сmClassic(object sender, RoutedEventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(@"C:\Users95355\Desktop\Студент\C#_FourthProject\FourthProject\bin\Debug\music\classic");
FileInfo[] files = dir.GetFiles("*.wav");
lbList.ItemsSource = files;
}
private readonly SoundPlayer player = new SoundPlayer();
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
player.Stop();
if(lbList.SelectedItem is FileInfo file)
{
player.SoundLocation=file.FullName;
player.PlaySync();
}
else
{
player.SoundLocation=null;
}
}