Created
May 6, 2022 01:28
-
-
Save rmcdongit/0faa84efd0e903545ad88862b9e6f333 to your computer and use it in GitHub Desktop.
Adding Sound to WPF (.NET Framework)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="SoundDemo.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:local="clr-namespace:MiddleEarth" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="500" Width="800"> | |
<Grid> | |
<Button Content="Play Sound" HorizontalAlignment="Left" Margin="343,225,0,0" VerticalAlignment="Top" Height="49" Width="131" Background="#FF3D7D95" Foreground="#FFF9F9F9" Click="PlayMusic"/> | |
</Grid> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
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; | |
namespace SoundDemo | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
// This SoundPlayer object is used to save the location of your wav file | |
SoundPlayer Player = new SoundPlayer("TemptingTime.wav"); | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
private void PlayMusic(object sender, RoutedEventArgs e) | |
{ | |
// These two statements are added to whichever method you wish | |
// In my example I am using a Button Click event to fire off this method | |
Player.Load(); | |
Player.Play(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment