C# – WPF – Dropdown menus & audio combination
In this article I present the combination of a drop-down menu and audio in a WPF application. Simply, the application’s logic is the following – you select one media file from a drop-down list and then you play it. The playing of the file is helped by the class SoundPlayer, which has the following interesting methods and properties:
> Play(): Plays a WAV file in a way that other actions can be carried out in the same time;
> PlaySync(): This action does not allow other actions in the same time & it cannot be paused;
> PlayLooping(): This is just a looping action, which may be paused;
> Stop(): A good way to stop PlayLooping() or Play();
> Load(): A good way to load a WAV file, which will be played with PlaySync();
> LoadAsync(): Loads a file to be played with Play() or PlayLooping();
> SoundLocation -> this is a property, telling us the path and the file name of the file.
So, the application looks like this:
The idea is as mentioned above – you can select which file through which method you would like to play. The button of the method “PlaySync()” is colored, because once you run it you cannot stop it :). Even with the red “X” on the top right corner of the window. If you want to hear a system sound, you may press the button “System Sound”. For a reason, the only play method the system sounds have is “Play()”.
Long story short, here is the XAML code of the application:
<Window x:Class="AVSound.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="VitoshAcademy" Height="200" Width="300">
<StackPanel>
<ComboBox x:Name="cbwav" Width="160" Margin="3">
<ComboBoxItem Selector.IsSelected="True">SpanishGuitar.wav</ComboBoxItem>
<ComboBoxItem>Tadaan.wav</ComboBoxItem>
</ComboBox>
<WrapPanel HorizontalAlignment="Center">
<Button Width="80" Margin="3" Click="asyncPlay">Play()</Button>
<Button Width="80" Margin="3" Click="syncPlay" Background="Aquamarine">PlaySync()</Button>
<Button Width="80" Margin="3" Click="contPlay">PlayLooping()</Button>
</WrapPanel>
<Button Width="80" Margin="3" Click="stopMe">Stop()</Button>
<ComboBox x:Name="cbsys" Width="160" Margin="3">
<ComboBoxItem Selector.IsSelected="True">Asterisk</ComboBoxItem>
<ComboBoxItem>Beep</ComboBoxItem>
<ComboBoxItem>Exclamation</ComboBoxItem>
<ComboBoxItem>Hand</ComboBoxItem>
<ComboBoxItem>Question</ComboBoxItem>
</ComboBox>
<Button Width="80" Margin="3" Click="systemSounds">System Sound</Button>
</StackPanel>
</Window>
Here is the C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Media;
using System.IO;
namespace AVSound
{
public partial class MainWindow : Window
{
SoundPlayer sp;
public MainWindow()
{
InitializeComponent();
sp = new SoundPlayer();
}
private void asyncPlay(object sender, RoutedEventArgs e)
{
change();
sp.Play();
}
private void syncPlay(object sender, RoutedEventArgs e)
{
change();
sp.PlaySync();
}
private void contPlay(object sender, RoutedEventArgs e)
{
change();
sp.PlayLooping();
}
private void change()
{
if (!File.Exists(cbwav.Text)) return;
sp.SoundLocation = cbwav.Text;
}
private void stopMe(object sender, RoutedEventArgs e)
{
sp.Stop();
}
private void systemSounds(object sender, RoutedEventArgs e)
{
switch (cbsys.Text)
{
case "Asterisk":
SystemSounds.Asterisk.Play();
break;
case "Beep":
SystemSounds.Beep.Play();
break;
case "Exclamation":
SystemSounds.Exclamation.Play();
break;
case "Hand":
SystemSounds.Hand.Play();
break;
case "Question":
SystemSounds.Question.Play();
break;
}
}
}
}
In the C# code, it is a good practice to not the small “change()” method, which actually tells the compiler to look for a file, named with the value of the drop-down menu in the folder of the application. If such file is not found:
if (!File.Exists(cbwav.Text)) return;
then the application does not do anything. Last but not least, here is the application.
Enjoy the code 🙂
