如何从列表框播放wav文件?



下午好。我在wpf上编写一个小程序。有一个ListBox,其中加载文件夹中的wav文件。有一个RadioButton(有几个),当你点击它时,某些音频文件被添加到列表中。但是我不知道如何播放选定的音轨时,点击的wav元素在ListBox

SoundPlayer player;
string[] playList;
private void сmClassic(object sender, RoutedEventArgs e)
{
lbList.Items.Clear();
DirectoryInfo dir = new DirectoryInfo(@"C:Users1395355DesktopСтудентC#4_FourthProjectFourthProjectbinDebugmusicclassic");
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)
{

}

}

从原始源代码的答案中摘取代码。

private void сmClassic(object sender, RoutedEventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(@"C:Users1395355DesktopСтудентC#4_FourthProjectFourthProjectbinDebugmusicclassic");
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;
}
}

相关内容

  • 没有找到相关文章

最新更新